annotate Objects/object.c @ 25135:0f0016632be4 legacy-trunk

_formatparam(), set_param(): RFC 2231 encoding support by Oleg Broytmann in SF patch #600096. Specifically, the former function now encodes the triplets, while the latter adds optional charset and language arguments.
author Barry Warsaw <barry@python.org>
date Fri, 06 Sep 2002 03:55:04 +0000
parents 24e80d96bd0b
children b0fa5f6a8bb9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 95
diff changeset
1
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
2 /* Generic object operations; and implementation of None (NoObject) */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5006
diff changeset
4 #include "Python.h"
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
24184
c4dcb80f48ea object.h special-build macro minefield: renamed all the new lexical
Tim Peters <tim.peters@gmail.com>
parents: 24155
diff changeset
10 #ifdef Py_REF_DEBUG
24420
36e2fd3f08f7 Excise DL_IMPORT/EXPORT from object.h, and related files. This patch
Mark Hammond <mhammond@skippinet.com.au>
parents: 24184
diff changeset
11 long _Py_RefTotal;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
12 #endif
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
13
24420
36e2fd3f08f7 Excise DL_IMPORT/EXPORT from object.h, and related files. This patch
Mark Hammond <mhammond@skippinet.com.au>
parents: 24184
diff changeset
14 int Py_DivisionWarningFlag;
19633
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19620
diff changeset
15
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
16 /* Object allocation routines used by NEWOBJ and NEWVAROBJ macros.
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
17 These are used by the individual routines for object creation.
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
18 Do not call them otherwise, they do not initialize the object! */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
19
1347
a22a350d3a5d * Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents: 1324
diff changeset
20 #ifdef COUNT_ALLOCS
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5006
diff changeset
21 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
22 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
23 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
24 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
25 void
13473
31b1f691a7d9 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
26 dump_counts(void)
1347
a22a350d3a5d * Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents: 1324
diff changeset
27 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5006
diff changeset
28 PyTypeObject *tp;
1347
a22a350d3a5d * Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents: 1324
diff changeset
29
a22a350d3a5d * Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents: 1324
diff changeset
30 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
31 fprintf(stderr, "%s alloc'd: %d, freed: %d, max in use: %d\n",
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
32 tp->tp_name, tp->tp_allocs, tp->tp_frees,
1361
7cd533cf194a import.c: When something is wrong with the .pyc, properly open the .py
Sjoerd Mullender <sjoerd@acm.org>
parents: 1356
diff changeset
33 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
34 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
35 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
36 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
37 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
38 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
39 null_strings, one_strings);
1347
a22a350d3a5d * Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents: 1324
diff changeset
40 }
a22a350d3a5d * Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents: 1324
diff changeset
41
3024
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
42 PyObject *
13473
31b1f691a7d9 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
43 get_counts(void)
3024
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
44 {
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
45 PyTypeObject *tp;
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
46 PyObject *result;
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
47 PyObject *v;
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
48
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
49 result = PyList_New(0);
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
50 if (result == NULL)
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
51 return NULL;
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
52 for (tp = type_list; tp; tp = tp->tp_next) {
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
53 v = Py_BuildValue("(siii)", tp->tp_name, tp->tp_allocs,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
54 tp->tp_frees, tp->tp_maxalloc);
3024
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
55 if (v == NULL) {
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
56 Py_DECREF(result);
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
57 return NULL;
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
58 }
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
59 if (PyList_Append(result, v) < 0) {
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
60 Py_DECREF(v);
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
61 Py_DECREF(result);
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
62 return NULL;
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
63 }
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
64 Py_DECREF(v);
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
65 }
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
66 return result;
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
67 }
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
68
1347
a22a350d3a5d * Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents: 1324
diff changeset
69 void
13473
31b1f691a7d9 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
70 inc_count(PyTypeObject *tp)
1347
a22a350d3a5d * Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents: 1324
diff changeset
71 {
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
72 if (tp->tp_allocs == 0) {
2639
f11ba9cc342f change in counting freed objects
Guido van Rossum <guido@python.org>
parents: 2612
diff changeset
73 /* 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
74 if (tp->tp_next != NULL) /* sanity check */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5006
diff changeset
75 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
76 tp->tp_next = type_list;
24144
d7d816e1f522 SF bug 578752: COUNT_ALLOCS vs heap types
Tim Peters <tim.peters@gmail.com>
parents: 24126
diff changeset
77 /* Note that as of Python 2.2, heap-allocated type objects
d7d816e1f522 SF bug 578752: COUNT_ALLOCS vs heap types
Tim Peters <tim.peters@gmail.com>
parents: 24126
diff changeset
78 * can go away, but this code requires that they stay alive
d7d816e1f522 SF bug 578752: COUNT_ALLOCS vs heap types
Tim Peters <tim.peters@gmail.com>
parents: 24126
diff changeset
79 * until program exit. That's why we're careful with
d7d816e1f522 SF bug 578752: COUNT_ALLOCS vs heap types
Tim Peters <tim.peters@gmail.com>
parents: 24126
diff changeset
80 * refcounts here. type_list gets a new reference to tp,
d7d816e1f522 SF bug 578752: COUNT_ALLOCS vs heap types
Tim Peters <tim.peters@gmail.com>
parents: 24126
diff changeset
81 * while ownership of the reference type_list used to hold
d7d816e1f522 SF bug 578752: COUNT_ALLOCS vs heap types
Tim Peters <tim.peters@gmail.com>
parents: 24126
diff changeset
82 * (if any) was transferred to tp->tp_next in the line above.
d7d816e1f522 SF bug 578752: COUNT_ALLOCS vs heap types
Tim Peters <tim.peters@gmail.com>
parents: 24126
diff changeset
83 * tp is thus effectively immortal after this.
d7d816e1f522 SF bug 578752: COUNT_ALLOCS vs heap types
Tim Peters <tim.peters@gmail.com>
parents: 24126
diff changeset
84 */
d7d816e1f522 SF bug 578752: COUNT_ALLOCS vs heap types
Tim Peters <tim.peters@gmail.com>
parents: 24126
diff changeset
85 Py_INCREF(tp);
1347
a22a350d3a5d * Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents: 1324
diff changeset
86 type_list = tp;
a22a350d3a5d * Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents: 1324
diff changeset
87 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
88 tp->tp_allocs++;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
89 if (tp->tp_allocs - tp->tp_frees > tp->tp_maxalloc)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
90 tp->tp_maxalloc = tp->tp_allocs - tp->tp_frees;
1347
a22a350d3a5d * Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents: 1324
diff changeset
91 }
a22a350d3a5d * Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents: 1324
diff changeset
92 #endif
a22a350d3a5d * Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents: 1324
diff changeset
93
24155
9456a2ccda61 The Py_REF_DEBUG/COUNT_ALLOCS/Py_TRACE_REFS macro minefield: added
Tim Peters <tim.peters@gmail.com>
parents: 24144
diff changeset
94 #ifdef Py_REF_DEBUG
9456a2ccda61 The Py_REF_DEBUG/COUNT_ALLOCS/Py_TRACE_REFS macro minefield: added
Tim Peters <tim.peters@gmail.com>
parents: 24144
diff changeset
95 /* Log a fatal error; doesn't return. */
9456a2ccda61 The Py_REF_DEBUG/COUNT_ALLOCS/Py_TRACE_REFS macro minefield: added
Tim Peters <tim.peters@gmail.com>
parents: 24144
diff changeset
96 void
9456a2ccda61 The Py_REF_DEBUG/COUNT_ALLOCS/Py_TRACE_REFS macro minefield: added
Tim Peters <tim.peters@gmail.com>
parents: 24144
diff changeset
97 _Py_NegativeRefcount(const char *fname, int lineno, PyObject *op)
9456a2ccda61 The Py_REF_DEBUG/COUNT_ALLOCS/Py_TRACE_REFS macro minefield: added
Tim Peters <tim.peters@gmail.com>
parents: 24144
diff changeset
98 {
9456a2ccda61 The Py_REF_DEBUG/COUNT_ALLOCS/Py_TRACE_REFS macro minefield: added
Tim Peters <tim.peters@gmail.com>
parents: 24144
diff changeset
99 char buf[300];
9456a2ccda61 The Py_REF_DEBUG/COUNT_ALLOCS/Py_TRACE_REFS macro minefield: added
Tim Peters <tim.peters@gmail.com>
parents: 24144
diff changeset
100
9456a2ccda61 The Py_REF_DEBUG/COUNT_ALLOCS/Py_TRACE_REFS macro minefield: added
Tim Peters <tim.peters@gmail.com>
parents: 24144
diff changeset
101 PyOS_snprintf(buf, sizeof(buf),
9456a2ccda61 The Py_REF_DEBUG/COUNT_ALLOCS/Py_TRACE_REFS macro minefield: added
Tim Peters <tim.peters@gmail.com>
parents: 24144
diff changeset
102 "%s:%i object at %p has negative ref count %i",
9456a2ccda61 The Py_REF_DEBUG/COUNT_ALLOCS/Py_TRACE_REFS macro minefield: added
Tim Peters <tim.peters@gmail.com>
parents: 24144
diff changeset
103 fname, lineno, op, op->ob_refcnt);
9456a2ccda61 The Py_REF_DEBUG/COUNT_ALLOCS/Py_TRACE_REFS macro minefield: added
Tim Peters <tim.peters@gmail.com>
parents: 24144
diff changeset
104 Py_FatalError(buf);
9456a2ccda61 The Py_REF_DEBUG/COUNT_ALLOCS/Py_TRACE_REFS macro minefield: added
Tim Peters <tim.peters@gmail.com>
parents: 24144
diff changeset
105 }
9456a2ccda61 The Py_REF_DEBUG/COUNT_ALLOCS/Py_TRACE_REFS macro minefield: added
Tim Peters <tim.peters@gmail.com>
parents: 24144
diff changeset
106
9456a2ccda61 The Py_REF_DEBUG/COUNT_ALLOCS/Py_TRACE_REFS macro minefield: added
Tim Peters <tim.peters@gmail.com>
parents: 24144
diff changeset
107 #endif /* Py_REF_DEBUG */
9456a2ccda61 The Py_REF_DEBUG/COUNT_ALLOCS/Py_TRACE_REFS macro minefield: added
Tim Peters <tim.peters@gmail.com>
parents: 24144
diff changeset
108
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5006
diff changeset
109 PyObject *
13473
31b1f691a7d9 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
110 PyObject_Init(PyObject *op, PyTypeObject *tp)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
111 {
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12481
diff changeset
112 if (op == NULL) {
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12481
diff changeset
113 PyErr_SetString(PyExc_SystemError,
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12481
diff changeset
114 "NULL object passed to PyObject_Init");
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12481
diff changeset
115 return op;
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12481
diff changeset
116 }
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12481
diff changeset
117 /* 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
118 op->ob_type = tp;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5006
diff changeset
119 _Py_NewReference(op);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 return op;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
122
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12481
diff changeset
123 PyVarObject *
13473
31b1f691a7d9 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
124 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
125 {
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12481
diff changeset
126 if (op == NULL) {
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12481
diff changeset
127 PyErr_SetString(PyExc_SystemError,
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12481
diff changeset
128 "NULL object passed to PyObject_InitVar");
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12481
diff changeset
129 return op;
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12481
diff changeset
130 }
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12481
diff changeset
131 /* 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
132 op->ob_size = size;
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12481
diff changeset
133 op->ob_type = tp;
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12481
diff changeset
134 _Py_NewReference((PyObject *)op);
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12481
diff changeset
135 return op;
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12481
diff changeset
136 }
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12481
diff changeset
137
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12481
diff changeset
138 PyObject *
13473
31b1f691a7d9 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
139 _PyObject_New(PyTypeObject *tp)
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12481
diff changeset
140 {
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12481
diff changeset
141 PyObject *op;
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12481
diff changeset
142 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
143 if (op == NULL)
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12481
diff changeset
144 return PyErr_NoMemory();
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12481
diff changeset
145 return PyObject_INIT(op, tp);
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12481
diff changeset
146 }
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12481
diff changeset
147
5535
20c2b715188e Oops, another forgotten renaming: varobject -> PyVarObject.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
148 PyVarObject *
20294
426b379a138b _PyObject_VAR_SIZE: always round up to a multiple-of-pointer-size value.
Tim Peters <tim.peters@gmail.com>
parents: 20292
diff changeset
149 _PyObject_NewVar(PyTypeObject *tp, int nitems)
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12481
diff changeset
150 {
5535
20c2b715188e Oops, another forgotten renaming: varobject -> PyVarObject.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
151 PyVarObject *op;
20296
6cc0b48049ec Guido suggests, and I agree, to insist that SIZEOF_VOID_P be a power of 2.
Tim Peters <tim.peters@gmail.com>
parents: 20294
diff changeset
152 const size_t size = _PyObject_VAR_SIZE(tp, nitems);
20294
426b379a138b _PyObject_VAR_SIZE: always round up to a multiple-of-pointer-size value.
Tim Peters <tim.peters@gmail.com>
parents: 20292
diff changeset
153 op = (PyVarObject *) PyObject_MALLOC(size);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
154 if (op == NULL)
5535
20c2b715188e Oops, another forgotten renaming: varobject -> PyVarObject.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
155 return (PyVarObject *)PyErr_NoMemory();
20294
426b379a138b _PyObject_VAR_SIZE: always round up to a multiple-of-pointer-size value.
Tim Peters <tim.peters@gmail.com>
parents: 20292
diff changeset
156 return PyObject_INIT_VAR(op, tp, nitems);
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12481
diff changeset
157 }
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12481
diff changeset
158
22982
d09740b2e03d Move PyObject_Malloc and PyObject_Free to obmalloc.c.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22785
diff changeset
159 /* for binary compatibility with 2.2 */
d09740b2e03d Move PyObject_Malloc and PyObject_Free to obmalloc.c.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22785
diff changeset
160 #undef _PyObject_Del
12576
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 {
13265
e8bb0d536633 Neil Schemenauer: small fixes for GC
Guido van Rossum <guido@python.org>
parents: 13256
diff changeset
164 PyObject_FREE(op);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
165 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
166
284
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 231
diff changeset
167 int
13473
31b1f691a7d9 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
168 PyObject_Print(PyObject *op, FILE *fp, int flags)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
169 {
322
1a6cf1f8ccec Check for write errors after printing a value
Guido van Rossum <guido@python.org>
parents: 307
diff changeset
170 int ret = 0;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5006
diff changeset
171 if (PyErr_CheckSignals())
284
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 231
diff changeset
172 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
173 #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
174 if (PyOS_CheckStack()) {
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14869
diff changeset
175 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
176 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
177 }
c69ad566aeb1 If USE_STACKCHECK is defined use PyOS_CheckStack() in the repr and str
Guido van Rossum <guido@python.org>
parents: 8111
diff changeset
178 #endif
11649
38560a3a3249 On Linux, one sometimes sees spurious errors after interrupting
Guido van Rossum <guido@python.org>
parents: 8695
diff changeset
179 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
180 if (op == NULL) {
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 231
diff changeset
181 fprintf(fp, "<nil>");
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
182 }
284
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 231
diff changeset
183 else {
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 231
diff changeset
184 if (op->ob_refcnt <= 0)
13210
ff243b0f16ee Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 13183
diff changeset
185 fprintf(fp, "<refcnt %u at %p>",
ff243b0f16ee Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 13183
diff changeset
186 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
187 else if (op->ob_type->tp_print == NULL) {
17646
d1ec679baa3d Printing objects to a real file still wasn't done right: if the
Guido van Rossum <guido@python.org>
parents: 17639
diff changeset
188 PyObject *s;
d1ec679baa3d Printing objects to a real file still wasn't done right: if the
Guido van Rossum <guido@python.org>
parents: 17639
diff changeset
189 if (flags & Py_PRINT_RAW)
d1ec679baa3d Printing objects to a real file still wasn't done right: if the
Guido van Rossum <guido@python.org>
parents: 17639
diff changeset
190 s = PyObject_Str(op);
d1ec679baa3d Printing objects to a real file still wasn't done right: if the
Guido van Rossum <guido@python.org>
parents: 17639
diff changeset
191 else
d1ec679baa3d Printing objects to a real file still wasn't done right: if the
Guido van Rossum <guido@python.org>
parents: 17639
diff changeset
192 s = PyObject_Repr(op);
d1ec679baa3d Printing objects to a real file still wasn't done right: if the
Guido van Rossum <guido@python.org>
parents: 17639
diff changeset
193 if (s == NULL)
d1ec679baa3d Printing objects to a real file still wasn't done right: if the
Guido van Rossum <guido@python.org>
parents: 17639
diff changeset
194 ret = -1;
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
195 else {
17646
d1ec679baa3d Printing objects to a real file still wasn't done right: if the
Guido van Rossum <guido@python.org>
parents: 17639
diff changeset
196 ret = PyObject_Print(s, fp, 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
197 }
17646
d1ec679baa3d Printing objects to a real file still wasn't done right: if the
Guido van Rossum <guido@python.org>
parents: 17639
diff changeset
198 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
199 }
284
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 231
diff changeset
200 else
322
1a6cf1f8ccec Check for write errors after printing a value
Guido van Rossum <guido@python.org>
parents: 307
diff changeset
201 ret = (*op->ob_type->tp_print)(op, fp, flags);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
202 }
322
1a6cf1f8ccec Check for write errors after printing a value
Guido van Rossum <guido@python.org>
parents: 307
diff changeset
203 if (ret == 0) {
1a6cf1f8ccec Check for write errors after printing a value
Guido van Rossum <guido@python.org>
parents: 307
diff changeset
204 if (ferror(fp)) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5006
diff changeset
205 PyErr_SetFromErrno(PyExc_IOError);
322
1a6cf1f8ccec Check for write errors after printing a value
Guido van Rossum <guido@python.org>
parents: 307
diff changeset
206 clearerr(fp);
1a6cf1f8ccec Check for write errors after printing a value
Guido van Rossum <guido@python.org>
parents: 307
diff changeset
207 ret = -1;
1a6cf1f8ccec Check for write errors after printing a value
Guido van Rossum <guido@python.org>
parents: 307
diff changeset
208 }
1a6cf1f8ccec Check for write errors after printing a value
Guido van Rossum <guido@python.org>
parents: 307
diff changeset
209 }
1a6cf1f8ccec Check for write errors after printing a value
Guido van Rossum <guido@python.org>
parents: 307
diff changeset
210 return ret;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
211 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
212
16165
0a72070eab6e A few miscellaneous helpers.
Barry Warsaw <barry@python.org>
parents: 16123
diff changeset
213 /* For debugging convenience. See Misc/gdbinit for some useful gdb hooks */
24122
d332484e394a Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents: 24121
diff changeset
214 void _PyObject_Dump(PyObject* op)
16165
0a72070eab6e A few miscellaneous helpers.
Barry Warsaw <barry@python.org>
parents: 16123
diff changeset
215 {
16742
db61894b0d6f _PyObject_Dump(): If argument is NULL, print "NULL" instead of
Barry Warsaw <barry@python.org>
parents: 16741
diff changeset
216 if (op == NULL)
db61894b0d6f _PyObject_Dump(): If argument is NULL, print "NULL" instead of
Barry Warsaw <barry@python.org>
parents: 16741
diff changeset
217 fprintf(stderr, "NULL\n");
db61894b0d6f _PyObject_Dump(): If argument is NULL, print "NULL" instead of
Barry Warsaw <barry@python.org>
parents: 16741
diff changeset
218 else {
19906
f39a9be7c9af _PyObject_Dump(): print the type of the object. This is by far the
Guido van Rossum <guido@python.org>
parents: 19846
diff changeset
219 fprintf(stderr, "object : ");
16742
db61894b0d6f _PyObject_Dump(): If argument is NULL, print "NULL" instead of
Barry Warsaw <barry@python.org>
parents: 16741
diff changeset
220 (void)PyObject_Print(op, stderr, 0);
19906
f39a9be7c9af _PyObject_Dump(): print the type of the object. This is by far the
Guido van Rossum <guido@python.org>
parents: 19846
diff changeset
221 fprintf(stderr, "\n"
f39a9be7c9af _PyObject_Dump(): print the type of the object. This is by far the
Guido van Rossum <guido@python.org>
parents: 19846
diff changeset
222 "type : %s\n"
f39a9be7c9af _PyObject_Dump(): print the type of the object. This is by far the
Guido van Rossum <guido@python.org>
parents: 19846
diff changeset
223 "refcount: %d\n"
f39a9be7c9af _PyObject_Dump(): print the type of the object. This is by far the
Guido van Rossum <guido@python.org>
parents: 19846
diff changeset
224 "address : %p\n",
f39a9be7c9af _PyObject_Dump(): print the type of the object. This is by far the
Guido van Rossum <guido@python.org>
parents: 19846
diff changeset
225 op->ob_type==NULL ? "NULL" : op->ob_type->tp_name,
f39a9be7c9af _PyObject_Dump(): print the type of the object. This is by far the
Guido van Rossum <guido@python.org>
parents: 19846
diff changeset
226 op->ob_refcnt,
f39a9be7c9af _PyObject_Dump(): print the type of the object. This is by far the
Guido van Rossum <guido@python.org>
parents: 19846
diff changeset
227 op);
16742
db61894b0d6f _PyObject_Dump(): If argument is NULL, print "NULL" instead of
Barry Warsaw <barry@python.org>
parents: 16741
diff changeset
228 }
16165
0a72070eab6e A few miscellaneous helpers.
Barry Warsaw <barry@python.org>
parents: 16123
diff changeset
229 }
16167
71ceaba3f615 PyObject_Dump(): Use %p format to print the address of the pointer.
Barry Warsaw <barry@python.org>
parents: 16165
diff changeset
230
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5006
diff changeset
231 PyObject *
13473
31b1f691a7d9 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
232 PyObject_Repr(PyObject *v)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
233 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5006
diff changeset
234 if (PyErr_CheckSignals())
284
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 231
diff changeset
235 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
236 #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
237 if (PyOS_CheckStack()) {
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14869
diff changeset
238 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
239 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
240 }
c69ad566aeb1 If USE_STACKCHECK is defined use PyOS_CheckStack() in the repr and str
Guido van Rossum <guido@python.org>
parents: 8111
diff changeset
241 #endif
284
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 231
diff changeset
242 if (v == NULL)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5006
diff changeset
243 return PyString_FromString("<NULL>");
19476
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
244 else if (v->ob_type->tp_repr == NULL)
19620
b706c027c3c0 PyObject_Repr(): add missing ">" back at end of format string: "<%s
Guido van Rossum <guido@python.org>
parents: 19580
diff changeset
245 return PyString_FromFormat("<%s object at %p>",
19476
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
246 v->ob_type->tp_name, v);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
247 else {
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
248 PyObject *res;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
249 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
250 if (res == NULL)
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
251 return NULL;
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19294
diff changeset
252 #ifdef Py_USING_UNICODE
13293
e38be8717ec0 changed repr and str to always convert unicode strings
Fredrik Lundh <fredrik@pythonware.com>
parents: 13265
diff changeset
253 if (PyUnicode_Check(res)) {
e38be8717ec0 changed repr and str to always convert unicode strings
Fredrik Lundh <fredrik@pythonware.com>
parents: 13265
diff changeset
254 PyObject* str;
13436
bc9e22ad5c46 - changed __repr__ to use "unicode escape" encoding for unicode
Fredrik Lundh <fredrik@pythonware.com>
parents: 13432
diff changeset
255 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
256 Py_DECREF(res);
390e5d51ff73 If auto-conversion fails, the Unicode codecs will return NULL.
Marc-André Lemburg <mal@egenix.com>
parents: 13293
diff changeset
257 if (str)
13293
e38be8717ec0 changed repr and str to always convert unicode strings
Fredrik Lundh <fredrik@pythonware.com>
parents: 13265
diff changeset
258 res = str;
13319
390e5d51ff73 If auto-conversion fails, the Unicode codecs will return NULL.
Marc-André Lemburg <mal@egenix.com>
parents: 13293
diff changeset
259 else
390e5d51ff73 If auto-conversion fails, the Unicode codecs will return NULL.
Marc-André Lemburg <mal@egenix.com>
parents: 13293
diff changeset
260 return NULL;
13293
e38be8717ec0 changed repr and str to always convert unicode strings
Fredrik Lundh <fredrik@pythonware.com>
parents: 13265
diff changeset
261 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19294
diff changeset
262 #endif
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
263 if (!PyString_Check(res)) {
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
264 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
265 "__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
266 res->ob_type->tp_name);
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
267 Py_DECREF(res);
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
268 return NULL;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
269 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
270 return res;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
271 }
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
272 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
273
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5006
diff changeset
274 PyObject *
13473
31b1f691a7d9 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
275 PyObject_Str(PyObject *v)
1400
aac8231a715c * mpzmodule.c: removed redundant mpz_print function.
Guido van Rossum <guido@python.org>
parents: 1361
diff changeset
276 {
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
277 PyObject *res;
24122
d332484e394a Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents: 24121
diff changeset
278
1400
aac8231a715c * mpzmodule.c: removed redundant mpz_print function.
Guido van Rossum <guido@python.org>
parents: 1361
diff changeset
279 if (v == NULL)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5006
diff changeset
280 return PyString_FromString("<NULL>");
19846
662e22f99639 More on SF bug [#460020] bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents: 19833
diff changeset
281 if (PyString_CheckExact(v)) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5006
diff changeset
282 Py_INCREF(v);
1400
aac8231a715c * mpzmodule.c: removed redundant mpz_print function.
Guido van Rossum <guido@python.org>
parents: 1361
diff changeset
283 return v;
aac8231a715c * mpzmodule.c: removed redundant mpz_print function.
Guido van Rossum <guido@python.org>
parents: 1361
diff changeset
284 }
17646
d1ec679baa3d Printing objects to a real file still wasn't done right: if the
Guido van Rossum <guido@python.org>
parents: 17639
diff changeset
285 if (v->ob_type->tp_str == NULL)
d1ec679baa3d Printing objects to a real file still wasn't done right: if the
Guido van Rossum <guido@python.org>
parents: 17639
diff changeset
286 return PyObject_Repr(v);
d1ec679baa3d Printing objects to a real file still wasn't done right: if the
Guido van Rossum <guido@python.org>
parents: 17639
diff changeset
287
d1ec679baa3d Printing objects to a real file still wasn't done right: if the
Guido van Rossum <guido@python.org>
parents: 17639
diff changeset
288 res = (*v->ob_type->tp_str)(v);
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
289 if (res == NULL)
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
290 return NULL;
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19294
diff changeset
291 #ifdef Py_USING_UNICODE
13319
390e5d51ff73 If auto-conversion fails, the Unicode codecs will return NULL.
Marc-André Lemburg <mal@egenix.com>
parents: 13293
diff changeset
292 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
293 PyObject* str;
390e5d51ff73 If auto-conversion fails, the Unicode codecs will return NULL.
Marc-André Lemburg <mal@egenix.com>
parents: 13293
diff changeset
294 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
295 Py_DECREF(res);
390e5d51ff73 If auto-conversion fails, the Unicode codecs will return NULL.
Marc-André Lemburg <mal@egenix.com>
parents: 13293
diff changeset
296 if (str)
390e5d51ff73 If auto-conversion fails, the Unicode codecs will return NULL.
Marc-André Lemburg <mal@egenix.com>
parents: 13293
diff changeset
297 res = str;
390e5d51ff73 If auto-conversion fails, the Unicode codecs will return NULL.
Marc-André Lemburg <mal@egenix.com>
parents: 13293
diff changeset
298 else
390e5d51ff73 If auto-conversion fails, the Unicode codecs will return NULL.
Marc-André Lemburg <mal@egenix.com>
parents: 13293
diff changeset
299 return NULL;
390e5d51ff73 If auto-conversion fails, the Unicode codecs will return NULL.
Marc-André Lemburg <mal@egenix.com>
parents: 13293
diff changeset
300 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19294
diff changeset
301 #endif
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
302 if (!PyString_Check(res)) {
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
303 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
304 "__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
305 res->ob_type->tp_name);
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
306 Py_DECREF(res);
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
307 return NULL;
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
308 }
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
309 return res;
1400
aac8231a715c * mpzmodule.c: removed redundant mpz_print function.
Guido van Rossum <guido@python.org>
parents: 1361
diff changeset
310 }
aac8231a715c * mpzmodule.c: removed redundant mpz_print function.
Guido van Rossum <guido@python.org>
parents: 1361
diff changeset
311
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19294
diff changeset
312 #ifdef Py_USING_UNICODE
16001
f3740674ac5b Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents: 15962
diff changeset
313 PyObject *
f3740674ac5b Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents: 15962
diff changeset
314 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
315 {
f3740674ac5b Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents: 15962
diff changeset
316 PyObject *res;
24122
d332484e394a Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents: 24121
diff changeset
317
16001
f3740674ac5b Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents: 15962
diff changeset
318 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
319 res = PyString_FromString("<NULL>");
20554
0336ee5b46d4 SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents: 20464
diff changeset
320 if (PyUnicode_CheckExact(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
321 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
322 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
323 }
20554
0336ee5b46d4 SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents: 20464
diff changeset
324 if (PyUnicode_Check(v)) {
0336ee5b46d4 SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents: 20464
diff changeset
325 /* For a Unicode subtype that's not a Unicode object,
0336ee5b46d4 SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents: 20464
diff changeset
326 return a true Unicode object with the same data. */
0336ee5b46d4 SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents: 20464
diff changeset
327 return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(v),
0336ee5b46d4 SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents: 20464
diff changeset
328 PyUnicode_GET_SIZE(v));
0336ee5b46d4 SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents: 20464
diff changeset
329 }
0336ee5b46d4 SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents: 20464
diff changeset
330 if (PyString_Check(v)) {
17220
b60096d5001a Fixed ref count bug. Patch #411191. Found by Walter Dörwald.
Marc-André Lemburg <mal@egenix.com>
parents: 16782
diff changeset
331 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
332 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
333 }
16001
f3740674ac5b Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents: 15962
diff changeset
334 else {
f3740674ac5b Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents: 15962
diff changeset
335 PyObject *func;
20554
0336ee5b46d4 SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents: 20464
diff changeset
336 static PyObject *unicodestr;
0336ee5b46d4 SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents: 20464
diff changeset
337 /* XXX As soon as we have a tp_unicode slot, we should
0336ee5b46d4 SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents: 20464
diff changeset
338 check this before trying the __unicode__
0336ee5b46d4 SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents: 20464
diff changeset
339 method. */
0336ee5b46d4 SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents: 20464
diff changeset
340 if (unicodestr == NULL) {
0336ee5b46d4 SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents: 20464
diff changeset
341 unicodestr= PyString_InternFromString(
0336ee5b46d4 SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents: 20464
diff changeset
342 "__unicode__");
0336ee5b46d4 SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents: 20464
diff changeset
343 if (unicodestr == NULL)
16001
f3740674ac5b Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents: 15962
diff changeset
344 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
345 }
20554
0336ee5b46d4 SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents: 20464
diff changeset
346 func = PyObject_GetAttr(v, unicodestr);
0336ee5b46d4 SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents: 20464
diff changeset
347 if (func != NULL) {
0336ee5b46d4 SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents: 20464
diff changeset
348 res = PyEval_CallObject(func, (PyObject *)NULL);
0336ee5b46d4 SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents: 20464
diff changeset
349 Py_DECREF(func);
16001
f3740674ac5b Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents: 15962
diff changeset
350 }
f3740674ac5b Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents: 15962
diff changeset
351 else {
20554
0336ee5b46d4 SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents: 20464
diff changeset
352 PyErr_Clear();
0336ee5b46d4 SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents: 20464
diff changeset
353 if (v->ob_type->tp_str != NULL)
0336ee5b46d4 SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents: 20464
diff changeset
354 res = (*v->ob_type->tp_str)(v);
0336ee5b46d4 SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents: 20464
diff changeset
355 else
0336ee5b46d4 SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents: 20464
diff changeset
356 res = PyObject_Repr(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
357 }
f3740674ac5b Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents: 15962
diff changeset
358 }
f3740674ac5b Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents: 15962
diff changeset
359 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
360 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
361 if (!PyUnicode_Check(res)) {
20554
0336ee5b46d4 SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents: 20464
diff changeset
362 PyObject *str;
0336ee5b46d4 SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents: 20464
diff changeset
363 str = PyUnicode_FromEncodedObject(res, NULL, "strict");
16001
f3740674ac5b Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents: 15962
diff changeset
364 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
365 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
366 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
367 else
f3740674ac5b Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents: 15962
diff changeset
368 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
369 }
f3740674ac5b Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents: 15962
diff changeset
370 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
371 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19294
diff changeset
372 #endif
16001
f3740674ac5b Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents: 15962
diff changeset
373
f3740674ac5b Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents: 15962
diff changeset
374
23598
2b1d61781d63 Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents: 23542
diff changeset
375 /* Helper to warn about deprecated tp_compare return values. Return:
2b1d61781d63 Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents: 23542
diff changeset
376 -2 for an exception;
2b1d61781d63 Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents: 23542
diff changeset
377 -1 if v < w;
2b1d61781d63 Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents: 23542
diff changeset
378 0 if v == w;
2b1d61781d63 Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents: 23542
diff changeset
379 1 if v > w.
2b1d61781d63 Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents: 23542
diff changeset
380 (This function cannot return 2.)
2b1d61781d63 Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents: 23542
diff changeset
381 */
2b1d61781d63 Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents: 23542
diff changeset
382 static int
2b1d61781d63 Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents: 23542
diff changeset
383 adjust_tp_compare(int c)
2b1d61781d63 Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents: 23542
diff changeset
384 {
2b1d61781d63 Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents: 23542
diff changeset
385 if (PyErr_Occurred()) {
2b1d61781d63 Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents: 23542
diff changeset
386 if (c != -1 && c != -2) {
2b1d61781d63 Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents: 23542
diff changeset
387 PyObject *t, *v, *tb;
2b1d61781d63 Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents: 23542
diff changeset
388 PyErr_Fetch(&t, &v, &tb);
2b1d61781d63 Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents: 23542
diff changeset
389 if (PyErr_Warn(PyExc_RuntimeWarning,
2b1d61781d63 Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents: 23542
diff changeset
390 "tp_compare didn't return -1 or -2 "
2b1d61781d63 Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents: 23542
diff changeset
391 "for exception") < 0) {
2b1d61781d63 Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents: 23542
diff changeset
392 Py_XDECREF(t);
2b1d61781d63 Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents: 23542
diff changeset
393 Py_XDECREF(v);
2b1d61781d63 Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents: 23542
diff changeset
394 Py_XDECREF(tb);
2b1d61781d63 Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents: 23542
diff changeset
395 }
2b1d61781d63 Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents: 23542
diff changeset
396 else
2b1d61781d63 Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents: 23542
diff changeset
397 PyErr_Restore(t, v, tb);
2b1d61781d63 Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents: 23542
diff changeset
398 }
2b1d61781d63 Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents: 23542
diff changeset
399 return -2;
2b1d61781d63 Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents: 23542
diff changeset
400 }
2b1d61781d63 Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents: 23542
diff changeset
401 else if (c < -1 || c > 1) {
2b1d61781d63 Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents: 23542
diff changeset
402 if (PyErr_Warn(PyExc_RuntimeWarning,
2b1d61781d63 Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents: 23542
diff changeset
403 "tp_compare didn't return -1, 0 or 1") < 0)
2b1d61781d63 Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents: 23542
diff changeset
404 return -2;
2b1d61781d63 Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents: 23542
diff changeset
405 else
2b1d61781d63 Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents: 23542
diff changeset
406 return c < -1 ? -1 : 1;
2b1d61781d63 Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents: 23542
diff changeset
407 }
2b1d61781d63 Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents: 23542
diff changeset
408 else {
2b1d61781d63 Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents: 23542
diff changeset
409 assert(c >= -1 && c <= 1);
2b1d61781d63 Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents: 23542
diff changeset
410 return c;
2b1d61781d63 Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents: 23542
diff changeset
411 }
2b1d61781d63 Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents: 23542
diff changeset
412 }
2b1d61781d63 Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents: 23542
diff changeset
413
2b1d61781d63 Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents: 23542
diff changeset
414
16236
e5d59a9dbfc0 Check the Py_TPFLAGS_HAVE_RICHCOMPARE flag before using the
Guido van Rossum <guido@python.org>
parents: 16197
diff changeset
415 /* 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
416 #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
417 ? (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
418
15941
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
419 /* 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
420 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
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 object. Return:
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
423 NULL for exception;
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
424 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
425 undefined;
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
426 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
427 (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
428 */
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
429 static PyObject *
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(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 richcmpfunc f;
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
433 PyObject *res;
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
434
20140
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20002
diff changeset
435 if (v->ob_type != w->ob_type &&
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20002
diff changeset
436 PyType_IsSubtype(w->ob_type, v->ob_type) &&
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20002
diff changeset
437 (f = RICHCOMPARE(w->ob_type)) != NULL) {
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20002
diff changeset
438 res = (*f)(w, v, swapped_op[op]);
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20002
diff changeset
439 if (res != Py_NotImplemented)
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20002
diff changeset
440 return res;
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20002
diff changeset
441 Py_DECREF(res);
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20002
diff changeset
442 }
16236
e5d59a9dbfc0 Check the Py_TPFLAGS_HAVE_RICHCOMPARE flag before using the
Guido van Rossum <guido@python.org>
parents: 16197
diff changeset
443 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
444 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
445 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
446 return res;
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
447 Py_DECREF(res);
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
448 }
16236
e5d59a9dbfc0 Check the Py_TPFLAGS_HAVE_RICHCOMPARE flag before using the
Guido van Rossum <guido@python.org>
parents: 16197
diff changeset
449 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
450 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
451 }
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
452 res = Py_NotImplemented;
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
453 Py_INCREF(res);
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
454 return res;
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
455 }
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
456
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
457 /* 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
458 -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
459 object that's not a Boolean);
20863
d6ff1daae398 No code change -- just trying to document the return conditions for all
Tim Peters <tim.peters@gmail.com>
parents: 20612
diff changeset
460 0 if the outcome is false;
d6ff1daae398 No code change -- just trying to document the return conditions for all
Tim Peters <tim.peters@gmail.com>
parents: 20612
diff changeset
461 1 if the outcome is true;
d6ff1daae398 No code change -- just trying to document the return conditions for all
Tim Peters <tim.peters@gmail.com>
parents: 20612
diff changeset
462 2 if this particular rich comparison is not implemented or undefined.
15941
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
463 */
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
464 static int
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
465 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
466 {
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
467 PyObject *res;
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
468 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
469
16236
e5d59a9dbfc0 Check the Py_TPFLAGS_HAVE_RICHCOMPARE flag before using the
Guido van Rossum <guido@python.org>
parents: 16197
diff changeset
470 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
471 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
472 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
473 if (res == NULL)
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
474 return -1;
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
475 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
476 Py_DECREF(res);
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
477 return 2;
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
478 }
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
479 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
480 Py_DECREF(res);
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
481 return ok;
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
482 }
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
483
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
484 /* 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
485 -2 for an exception;
20863
d6ff1daae398 No code change -- just trying to document the return conditions for all
Tim Peters <tim.peters@gmail.com>
parents: 20612
diff changeset
486 -1 if v < w;
d6ff1daae398 No code change -- just trying to document the return conditions for all
Tim Peters <tim.peters@gmail.com>
parents: 20612
diff changeset
487 0 if v == w;
d6ff1daae398 No code change -- just trying to document the return conditions for all
Tim Peters <tim.peters@gmail.com>
parents: 20612
diff changeset
488 1 if v > w;
d6ff1daae398 No code change -- just trying to document the return conditions for all
Tim Peters <tim.peters@gmail.com>
parents: 20612
diff changeset
489 2 if this particular rich comparison is not implemented or undefined.
15941
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
490 */
15726
db9f1893be01 Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
491 static int
15941
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
492 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
493 {
15962
f1751abf771f Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents: 15952
diff changeset
494 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
495 /* 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
496 {Py_EQ, 0},
f1751abf771f Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents: 15952
diff changeset
497 {Py_LT, -1},
f1751abf771f Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents: 15952
diff changeset
498 {Py_GT, 1},
f1751abf771f Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents: 15952
diff changeset
499 };
f1751abf771f Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents: 15952
diff changeset
500 int i;
f1751abf771f Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents: 15952
diff changeset
501
16236
e5d59a9dbfc0 Check the Py_TPFLAGS_HAVE_RICHCOMPARE flag before using the
Guido van Rossum <guido@python.org>
parents: 16197
diff changeset
502 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
503 return 2; /* Shortcut */
15962
f1751abf771f Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents: 15952
diff changeset
504
f1751abf771f Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents: 15952
diff changeset
505 for (i = 0; i < 3; i++) {
f1751abf771f Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents: 15952
diff changeset
506 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
507 case -1:
17698
aad0adf806ad SF bug #422108 - Error in rich comparisons.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
508 return -2;
15962
f1751abf771f Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents: 15952
diff changeset
509 case 1:
f1751abf771f Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents: 15952
diff changeset
510 return tries[i].outcome;
f1751abf771f Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents: 15952
diff changeset
511 }
15941
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
512 }
15962
f1751abf771f Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents: 15952
diff changeset
513
f1751abf771f Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents: 15952
diff changeset
514 return 2;
15941
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
515 }
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
516
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
517 /* 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
518 -2 for an exception;
20863
d6ff1daae398 No code change -- just trying to document the return conditions for all
Tim Peters <tim.peters@gmail.com>
parents: 20612
diff changeset
519 -1 if v < w;
d6ff1daae398 No code change -- just trying to document the return conditions for all
Tim Peters <tim.peters@gmail.com>
parents: 20612
diff changeset
520 0 if v == w;
d6ff1daae398 No code change -- just trying to document the return conditions for all
Tim Peters <tim.peters@gmail.com>
parents: 20612
diff changeset
521 1 if v > w;
d6ff1daae398 No code change -- just trying to document the return conditions for all
Tim Peters <tim.peters@gmail.com>
parents: 20612
diff changeset
522 2 if this particular 3-way comparison is not implemented or undefined.
15941
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
523 */
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
524 static int
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
525 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
526 {
db9f1893be01 Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
527 int c;
15941
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
528 cmpfunc f;
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 /* 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
531 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
532
19970
f761cab28c1c Hopefully fix 3-way comparisons. This unfortunately adds yet another
Guido van Rossum <guido@python.org>
parents: 19932
diff changeset
533 f = v->ob_type->tp_compare;
15941
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
534 if (PyInstance_Check(v))
19970
f761cab28c1c Hopefully fix 3-way comparisons. This unfortunately adds yet another
Guido van Rossum <guido@python.org>
parents: 19932
diff changeset
535 return (*f)(v, w);
15941
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
536 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
537 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
538
19970
f761cab28c1c Hopefully fix 3-way comparisons. This unfortunately adds yet another
Guido van Rossum <guido@python.org>
parents: 19932
diff changeset
539 /* If both have the same (non-NULL) tp_compare, use it. */
f761cab28c1c Hopefully fix 3-way comparisons. This unfortunately adds yet another
Guido van Rossum <guido@python.org>
parents: 19932
diff changeset
540 if (f != NULL && f == w->ob_type->tp_compare) {
f761cab28c1c Hopefully fix 3-way comparisons. This unfortunately adds yet another
Guido van Rossum <guido@python.org>
parents: 19932
diff changeset
541 c = (*f)(v, w);
23598
2b1d61781d63 Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents: 23542
diff changeset
542 return adjust_tp_compare(c);
19970
f761cab28c1c Hopefully fix 3-way comparisons. This unfortunately adds yet another
Guido van Rossum <guido@python.org>
parents: 19932
diff changeset
543 }
f761cab28c1c Hopefully fix 3-way comparisons. This unfortunately adds yet another
Guido van Rossum <guido@python.org>
parents: 19932
diff changeset
544
f761cab28c1c Hopefully fix 3-way comparisons. This unfortunately adds yet another
Guido van Rossum <guido@python.org>
parents: 19932
diff changeset
545 /* If either tp_compare is _PyObject_SlotCompare, that's safe. */
f761cab28c1c Hopefully fix 3-way comparisons. This unfortunately adds yet another
Guido van Rossum <guido@python.org>
parents: 19932
diff changeset
546 if (f == _PyObject_SlotCompare ||
f761cab28c1c Hopefully fix 3-way comparisons. This unfortunately adds yet another
Guido van Rossum <guido@python.org>
parents: 19932
diff changeset
547 w->ob_type->tp_compare == _PyObject_SlotCompare)
f761cab28c1c Hopefully fix 3-way comparisons. This unfortunately adds yet another
Guido van Rossum <guido@python.org>
parents: 19932
diff changeset
548 return _PyObject_SlotCompare(v, w);
f761cab28c1c Hopefully fix 3-way comparisons. This unfortunately adds yet another
Guido van Rossum <guido@python.org>
parents: 19932
diff changeset
549
15941
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
550 /* 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
551 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
552 if (c < 0)
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
553 return -2;
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
554 if (c > 0)
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
555 return 2;
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
556
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
557 /* 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
558 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
559 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
560 Py_DECREF(v);
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
561 Py_DECREF(w);
23598
2b1d61781d63 Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents: 23542
diff changeset
562 return adjust_tp_compare(c);
15941
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
563 }
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
564
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
565 /* 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
566 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
567 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
568 Py_DECREF(v);
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
569 Py_DECREF(w);
23598
2b1d61781d63 Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents: 23542
diff changeset
570 c = adjust_tp_compare(c);
2b1d61781d63 Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents: 23542
diff changeset
571 if (c >= -1)
2b1d61781d63 Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents: 23542
diff changeset
572 return -c; /* Swapped! */
2b1d61781d63 Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents: 23542
diff changeset
573 else
2b1d61781d63 Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents: 23542
diff changeset
574 return c;
15726
db9f1893be01 Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
575 }
15941
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
576
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
577 /* No comparison defined */
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
578 Py_DECREF(v);
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
579 Py_DECREF(w);
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
580 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
581 }
db9f1893be01 Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
582
15941
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
583 /* 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
584 -2 if an error occurred;
20863
d6ff1daae398 No code change -- just trying to document the return conditions for all
Tim Peters <tim.peters@gmail.com>
parents: 20612
diff changeset
585 -1 if v < w;
d6ff1daae398 No code change -- just trying to document the return conditions for all
Tim Peters <tim.peters@gmail.com>
parents: 20612
diff changeset
586 0 if v == w;
d6ff1daae398 No code change -- just trying to document the return conditions for all
Tim Peters <tim.peters@gmail.com>
parents: 20612
diff changeset
587 1 if v > w.
15941
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
588 */
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
589 static int
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
590 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
591 {
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
592 int c;
16108
28d8ccbe237f Once again, numeric-smelling objects compare smaller than non-numeric
Guido van Rossum <guido@python.org>
parents: 16079
diff changeset
593 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
594
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
595 if (v->ob_type == w->ob_type) {
16062
afc6f1137ea4 Tim chastens:
Barry Warsaw <barry@python.org>
parents: 16061
diff changeset
596 /* When comparing these pointers, they must be cast to
afc6f1137ea4 Tim chastens:
Barry Warsaw <barry@python.org>
parents: 16061
diff changeset
597 * integer types (i.e. Py_uintptr_t, our spelling of C9X's
afc6f1137ea4 Tim chastens:
Barry Warsaw <barry@python.org>
parents: 16061
diff changeset
598 * uintptr_t). ANSI specifies that pointer compares other
afc6f1137ea4 Tim chastens:
Barry Warsaw <barry@python.org>
parents: 16061
diff changeset
599 * than == and != to non-related structures are undefined.
afc6f1137ea4 Tim chastens:
Barry Warsaw <barry@python.org>
parents: 16061
diff changeset
600 */
16061
6b647a5d5b7e default_3way_compare(): When comparing the pointers, they must be cast
Barry Warsaw <barry@python.org>
parents: 16002
diff changeset
601 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
602 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
603 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
604 }
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
605
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19294
diff changeset
606 #ifdef Py_USING_UNICODE
15941
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
607 /* 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
608 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
609 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
610 if (!PyErr_Occurred())
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
611 return c;
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
612 /* 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
613 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
614 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
615 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
616 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
617 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
618 return -2;
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
619 PyErr_Clear();
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
620 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19294
diff changeset
621 #endif
15941
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
622
16123
f997ded4e219 New special case in comparisons: None is smaller than any other object
Guido van Rossum <guido@python.org>
parents: 16108
diff changeset
623 /* 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
624 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
625 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
626 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
627 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
628
15941
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
629 /* 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
630 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
631 vname = "";
28d8ccbe237f Once again, numeric-smelling objects compare smaller than non-numeric
Guido van Rossum <guido@python.org>
parents: 16079
diff changeset
632 else
28d8ccbe237f Once again, numeric-smelling objects compare smaller than non-numeric
Guido van Rossum <guido@python.org>
parents: 16079
diff changeset
633 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
634 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
635 wname = "";
28d8ccbe237f Once again, numeric-smelling objects compare smaller than non-numeric
Guido van Rossum <guido@python.org>
parents: 16079
diff changeset
636 else
28d8ccbe237f Once again, numeric-smelling objects compare smaller than non-numeric
Guido van Rossum <guido@python.org>
parents: 16079
diff changeset
637 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
638 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
639 if (c < 0)
28d8ccbe237f Once again, numeric-smelling objects compare smaller than non-numeric
Guido van Rossum <guido@python.org>
parents: 16079
diff changeset
640 return -1;
28d8ccbe237f Once again, numeric-smelling objects compare smaller than non-numeric
Guido van Rossum <guido@python.org>
parents: 16079
diff changeset
641 if (c > 0)
28d8ccbe237f Once again, numeric-smelling objects compare smaller than non-numeric
Guido van Rossum <guido@python.org>
parents: 16079
diff changeset
642 return 1;
28d8ccbe237f Once again, numeric-smelling objects compare smaller than non-numeric
Guido van Rossum <guido@python.org>
parents: 16079
diff changeset
643 /* 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
644 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
645 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
646 }
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
647
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
648 #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
649
17698
aad0adf806ad SF bug #422108 - Error in rich comparisons.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
650 /* Do a 3-way comparison, by hook or by crook. Return:
23598
2b1d61781d63 Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents: 23542
diff changeset
651 -2 for an exception (but see below);
20863
d6ff1daae398 No code change -- just trying to document the return conditions for all
Tim Peters <tim.peters@gmail.com>
parents: 20612
diff changeset
652 -1 if v < w;
17698
aad0adf806ad SF bug #422108 - Error in rich comparisons.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
653 0 if v == w;
20863
d6ff1daae398 No code change -- just trying to document the return conditions for all
Tim Peters <tim.peters@gmail.com>
parents: 20612
diff changeset
654 1 if v > w;
23598
2b1d61781d63 Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents: 23542
diff changeset
655 BUT: if the object implements a tp_compare function, it returns
18063
bbac2a6a5d16 Patch #424475: Speed-up tp_compare usage, by special-casing the common
Martin v. Löwis <martin@v.loewis.de>
parents: 17781
diff changeset
656 whatever this function returns (whether with an exception or not).
17698
aad0adf806ad SF bug #422108 - Error in rich comparisons.
Tim Peters <tim.peters@gmail.com>
parents: 17688
diff changeset
657 */
15726
db9f1893be01 Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
658 static int
13473
31b1f691a7d9 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
659 do_cmp(PyObject *v, PyObject *w)
2124
36847b8b575d properly implement cmp() for class instances
Guido van Rossum <guido@python.org>
parents: 2113
diff changeset
660 {
15726
db9f1893be01 Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
661 int c;
18063
bbac2a6a5d16 Patch #424475: Speed-up tp_compare usage, by special-casing the common
Martin v. Löwis <martin@v.loewis.de>
parents: 17781
diff changeset
662 cmpfunc f;
15726
db9f1893be01 Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
663
18063
bbac2a6a5d16 Patch #424475: Speed-up tp_compare usage, by special-casing the common
Martin v. Löwis <martin@v.loewis.de>
parents: 17781
diff changeset
664 if (v->ob_type == w->ob_type
19293
1e87db0ca248 Update to MvL's patch #424475 to avoid returning 2 when tp_compare
Guido van Rossum <guido@python.org>
parents: 19040
diff changeset
665 && (f = v->ob_type->tp_compare) != NULL) {
1e87db0ca248 Update to MvL's patch #424475 to avoid returning 2 when tp_compare
Guido van Rossum <guido@python.org>
parents: 19040
diff changeset
666 c = (*f)(v, w);
23598
2b1d61781d63 Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents: 23542
diff changeset
667 if (PyInstance_Check(v)) {
2b1d61781d63 Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents: 23542
diff changeset
668 /* Instance tp_compare has a different signature.
2b1d61781d63 Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents: 23542
diff changeset
669 But if it returns undefined we fall through. */
2b1d61781d63 Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents: 23542
diff changeset
670 if (c != 2)
2b1d61781d63 Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents: 23542
diff changeset
671 return c;
23599
197fb305fc5c Fix typo
Neal Norwitz <nnorwitz@gmail.com>
parents: 23598
diff changeset
672 /* Else fall through to try_rich_to_3way_compare() */
23598
2b1d61781d63 Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents: 23542
diff changeset
673 }
2b1d61781d63 Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents: 23542
diff changeset
674 else
2b1d61781d63 Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents: 23542
diff changeset
675 return adjust_tp_compare(c);
19293
1e87db0ca248 Update to MvL's patch #424475 to avoid returning 2 when tp_compare
Guido van Rossum <guido@python.org>
parents: 19040
diff changeset
676 }
23598
2b1d61781d63 Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents: 23542
diff changeset
677 /* We only get here if one of the following is true:
2b1d61781d63 Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents: 23542
diff changeset
678 a) v and w have different types
2b1d61781d63 Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents: 23542
diff changeset
679 b) v and w have the same type, which doesn't have tp_compare
2b1d61781d63 Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents: 23542
diff changeset
680 c) v and w are instances, and either __cmp__ is not defined or
2b1d61781d63 Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents: 23542
diff changeset
681 __cmp__ returns NotImplemented
2b1d61781d63 Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents: 23542
diff changeset
682 */
15941
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
683 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
684 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
685 return c;
15941
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
686 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
687 if (c < 2)
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
688 return c;
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
689 return default_3way_compare(v, w);
2124
36847b8b575d properly implement cmp() for class instances
Guido van Rossum <guido@python.org>
parents: 2113
diff changeset
690 }
36847b8b575d properly implement cmp() for class instances
Guido van Rossum <guido@python.org>
parents: 2113
diff changeset
691
15962
f1751abf771f Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents: 15952
diff changeset
692 /* 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
693 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
694 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
695
f3740674ac5b Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents: 15962
diff changeset
696 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
697 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
698 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
699 data structures slower.
12406
ec6b6ca5d111 Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12327
diff changeset
700 */
16001
f3740674ac5b Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents: 15962
diff changeset
701 #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
702
15962
f1751abf771f Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents: 15952
diff changeset
703 static int compare_nesting = 0;
12406
ec6b6ca5d111 Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12327
diff changeset
704
ec6b6ca5d111 Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12327
diff changeset
705 static PyObject*
13473
31b1f691a7d9 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
706 get_inprogress_dict(void)
12406
ec6b6ca5d111 Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12327
diff changeset
707 {
15962
f1751abf771f Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents: 15952
diff changeset
708 static PyObject *key;
12406
ec6b6ca5d111 Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12327
diff changeset
709 PyObject *tstate_dict, *inprogress;
ec6b6ca5d111 Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12327
diff changeset
710
15962
f1751abf771f Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents: 15952
diff changeset
711 if (key == NULL) {
f1751abf771f Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents: 15952
diff changeset
712 key = PyString_InternFromString("cmp_state");
f1751abf771f Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents: 15952
diff changeset
713 if (key == NULL)
f1751abf771f Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents: 15952
diff changeset
714 return NULL;
f1751abf771f Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents: 15952
diff changeset
715 }
f1751abf771f Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents: 15952
diff changeset
716
12406
ec6b6ca5d111 Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12327
diff changeset
717 tstate_dict = PyThreadState_GetDict();
ec6b6ca5d111 Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12327
diff changeset
718 if (tstate_dict == NULL) {
ec6b6ca5d111 Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12327
diff changeset
719 PyErr_BadInternalCall();
ec6b6ca5d111 Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12327
diff changeset
720 return NULL;
24122
d332484e394a Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents: 24121
diff changeset
721 }
16001
f3740674ac5b Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents: 15962
diff changeset
722
24122
d332484e394a Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents: 24121
diff changeset
723 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
724 if (inprogress == NULL) {
ec6b6ca5d111 Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12327
diff changeset
725 inprogress = PyDict_New();
ec6b6ca5d111 Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12327
diff changeset
726 if (inprogress == NULL)
ec6b6ca5d111 Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12327
diff changeset
727 return NULL;
15962
f1751abf771f Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents: 15952
diff changeset
728 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
729 Py_DECREF(inprogress);
ec6b6ca5d111 Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12327
diff changeset
730 return NULL;
ec6b6ca5d111 Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12327
diff changeset
731 }
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
732 Py_DECREF(inprogress);
12406
ec6b6ca5d111 Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12327
diff changeset
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
12406
ec6b6ca5d111 Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12327
diff changeset
735 return inprogress;
ec6b6ca5d111 Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12327
diff changeset
736 }
ec6b6ca5d111 Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12327
diff changeset
737
16001
f3740674ac5b Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents: 15962
diff changeset
738 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
739 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
740 {
16001
f3740674ac5b Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents: 15962
diff changeset
741 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
742 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
743 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
744 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
745 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
746
f3740674ac5b Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents: 15962
diff changeset
747 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
748 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
749 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
750
f3740674ac5b Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents: 15962
diff changeset
751 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
752 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
753 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
754
f3740674ac5b Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents: 15962
diff changeset
755 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
756 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
757 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
758 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
759 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
760 } else {
f3740674ac5b Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents: 15962
diff changeset
761 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
762 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
763 }
16001
f3740674ac5b Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents: 15962
diff changeset
764 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
765 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
766 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
767 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
768 }
16001
f3740674ac5b Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents: 15962
diff changeset
769
f3740674ac5b Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents: 15962
diff changeset
770 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
771 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
772 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
773 }
f3740674ac5b Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents: 15962
diff changeset
774
f3740674ac5b Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents: 15962
diff changeset
775 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
776 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
777 return NULL;
1f92d63b20f0 This patch adds a new builtin unistr() which behaves like str()
Marc-André Lemburg <mal@egenix.com>
parents: 15941
diff changeset
778 }
16001
f3740674ac5b Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents: 15962
diff changeset
779
f3740674ac5b Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents: 15962
diff changeset
780 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
781 }
1f92d63b20f0 This patch adds a new builtin unistr() which behaves like str()
Marc-André Lemburg <mal@egenix.com>
parents: 15941
diff changeset
782
16001
f3740674ac5b Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents: 15962
diff changeset
783 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
784 delete_token(PyObject *token)
12406
ec6b6ca5d111 Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12327
diff changeset
785 {
16001
f3740674ac5b Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents: 15962
diff changeset
786 PyObject *inprogress;
12406
ec6b6ca5d111 Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12327
diff changeset
787
16001
f3740674ac5b Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents: 15962
diff changeset
788 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
789 return;
f3740674ac5b Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents: 15962
diff changeset
790 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
791 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
792 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
793 else
f3740674ac5b Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents: 15962
diff changeset
794 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
795 Py_DECREF(token);
12406
ec6b6ca5d111 Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12327
diff changeset
796 }
ec6b6ca5d111 Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12327
diff changeset
797
20863
d6ff1daae398 No code change -- just trying to document the return conditions for all
Tim Peters <tim.peters@gmail.com>
parents: 20612
diff changeset
798 /* Compare v to w. Return
d6ff1daae398 No code change -- just trying to document the return conditions for all
Tim Peters <tim.peters@gmail.com>
parents: 20612
diff changeset
799 -1 if v < w or exception (PyErr_Occurred() true in latter case).
d6ff1daae398 No code change -- just trying to document the return conditions for all
Tim Peters <tim.peters@gmail.com>
parents: 20612
diff changeset
800 0 if v == w.
d6ff1daae398 No code change -- just trying to document the return conditions for all
Tim Peters <tim.peters@gmail.com>
parents: 20612
diff changeset
801 1 if v > w.
d6ff1daae398 No code change -- just trying to document the return conditions for all
Tim Peters <tim.peters@gmail.com>
parents: 20612
diff changeset
802 XXX The docs (C API manual) say the return value is undefined in case
d6ff1daae398 No code change -- just trying to document the return conditions for all
Tim Peters <tim.peters@gmail.com>
parents: 20612
diff changeset
803 XXX of error.
d6ff1daae398 No code change -- just trying to document the return conditions for all
Tim Peters <tim.peters@gmail.com>
parents: 20612
diff changeset
804 */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
805 int
13473
31b1f691a7d9 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
806 PyObject_Compare(PyObject *v, PyObject *w)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
807 {
16001
f3740674ac5b Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents: 15962
diff changeset
808 PyTypeObject *vtp;
12406
ec6b6ca5d111 Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12327
diff changeset
809 int result;
ec6b6ca5d111 Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12327
diff changeset
810
14126
41eb755e4ed1 Added PyOS_CheckStack call to PyObject_Compare
Jack Jansen <jack.jansen@cwi.nl>
parents: 14051
diff changeset
811 #if defined(USE_STACKCHECK)
14265
1f76fc98b4c8 PyOS_CheckStack() returns 1 when failing, not -1.
Guido van Rossum <guido@python.org>
parents: 14126
diff changeset
812 if (PyOS_CheckStack()) {
14126
41eb755e4ed1 Added PyOS_CheckStack call to PyObject_Compare
Jack Jansen <jack.jansen@cwi.nl>
parents: 14051
diff changeset
813 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
20612
bea9404ce62c cleanup indentation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20554
diff changeset
814 return -1;
14126
41eb755e4ed1 Added PyOS_CheckStack call to PyObject_Compare
Jack Jansen <jack.jansen@cwi.nl>
parents: 14051
diff changeset
815 }
41eb755e4ed1 Added PyOS_CheckStack call to PyObject_Compare
Jack Jansen <jack.jansen@cwi.nl>
parents: 14051
diff changeset
816 #endif
5610
10e9cfa010f0 PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents: 5560
diff changeset
817 if (v == NULL || w == NULL) {
10e9cfa010f0 PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents: 5560
diff changeset
818 PyErr_BadInternalCall();
10e9cfa010f0 PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents: 5560
diff changeset
819 return -1;
10e9cfa010f0 PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents: 5560
diff changeset
820 }
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
821 if (v == w)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
822 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
823 vtp = v->ob_type;
15962
f1751abf771f Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents: 15952
diff changeset
824 compare_nesting++;
f1751abf771f Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents: 15952
diff changeset
825 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
826 (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
827 || (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
828 && !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
829 && !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
830 /* 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
831 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
832
16001
f3740674ac5b Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents: 15962
diff changeset
833 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
834 result = -1;
12406
ec6b6ca5d111 Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12327
diff changeset
835 }
16001
f3740674ac5b Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents: 15962
diff changeset
836 else if (token == Py_None) {
12406
ec6b6ca5d111 Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12327
diff changeset
837 /* already comparing these objects. assume
ec6b6ca5d111 Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12327
diff changeset
838 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
839 result = 0;
12406
ec6b6ca5d111 Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12327
diff changeset
840 }
16001
f3740674ac5b Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents: 15962
diff changeset
841 else {
f3740674ac5b Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents: 15962
diff changeset
842 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
843 delete_token(token);
12406
ec6b6ca5d111 Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12327
diff changeset
844 }
ec6b6ca5d111 Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12327
diff changeset
845 }
15726
db9f1893be01 Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
846 else {
db9f1893be01 Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
847 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
848 }
15962
f1751abf771f Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents: 15952
diff changeset
849 compare_nesting--;
15941
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
850 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
851 }
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
852
20863
d6ff1daae398 No code change -- just trying to document the return conditions for all
Tim Peters <tim.peters@gmail.com>
parents: 20612
diff changeset
853 /* Return (new reference to) Py_True or Py_False. */
15941
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
854 static PyObject *
18063
bbac2a6a5d16 Patch #424475: Speed-up tp_compare usage, by special-casing the common
Martin v. Löwis <martin@v.loewis.de>
parents: 17781
diff changeset
855 convert_3way_to_object(int op, int c)
15941
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
856 {
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
857 PyObject *result;
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
858 switch (op) {
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
859 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
860 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
861 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
862 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
863 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
864 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
865 }
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
866 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
867 Py_INCREF(result);
12406
ec6b6ca5d111 Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12327
diff changeset
868 return result;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
869 }
24122
d332484e394a Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents: 24121
diff changeset
870
20863
d6ff1daae398 No code change -- just trying to document the return conditions for all
Tim Peters <tim.peters@gmail.com>
parents: 20612
diff changeset
871 /* We want a rich comparison but don't have one. Try a 3-way cmp instead.
d6ff1daae398 No code change -- just trying to document the return conditions for all
Tim Peters <tim.peters@gmail.com>
parents: 20612
diff changeset
872 Return
d6ff1daae398 No code change -- just trying to document the return conditions for all
Tim Peters <tim.peters@gmail.com>
parents: 20612
diff changeset
873 NULL if error
d6ff1daae398 No code change -- just trying to document the return conditions for all
Tim Peters <tim.peters@gmail.com>
parents: 20612
diff changeset
874 Py_True if v op w
d6ff1daae398 No code change -- just trying to document the return conditions for all
Tim Peters <tim.peters@gmail.com>
parents: 20612
diff changeset
875 Py_False if not (v op w)
d6ff1daae398 No code change -- just trying to document the return conditions for all
Tim Peters <tim.peters@gmail.com>
parents: 20612
diff changeset
876 */
18063
bbac2a6a5d16 Patch #424475: Speed-up tp_compare usage, by special-casing the common
Martin v. Löwis <martin@v.loewis.de>
parents: 17781
diff changeset
877 static PyObject *
bbac2a6a5d16 Patch #424475: Speed-up tp_compare usage, by special-casing the common
Martin v. Löwis <martin@v.loewis.de>
parents: 17781
diff changeset
878 try_3way_to_rich_compare(PyObject *v, PyObject *w, int op)
bbac2a6a5d16 Patch #424475: Speed-up tp_compare usage, by special-casing the common
Martin v. Löwis <martin@v.loewis.de>
parents: 17781
diff changeset
879 {
bbac2a6a5d16 Patch #424475: Speed-up tp_compare usage, by special-casing the common
Martin v. Löwis <martin@v.loewis.de>
parents: 17781
diff changeset
880 int c;
bbac2a6a5d16 Patch #424475: Speed-up tp_compare usage, by special-casing the common
Martin v. Löwis <martin@v.loewis.de>
parents: 17781
diff changeset
881
bbac2a6a5d16 Patch #424475: Speed-up tp_compare usage, by special-casing the common
Martin v. Löwis <martin@v.loewis.de>
parents: 17781
diff changeset
882 c = try_3way_compare(v, w);
bbac2a6a5d16 Patch #424475: Speed-up tp_compare usage, by special-casing the common
Martin v. Löwis <martin@v.loewis.de>
parents: 17781
diff changeset
883 if (c >= 2)
bbac2a6a5d16 Patch #424475: Speed-up tp_compare usage, by special-casing the common
Martin v. Löwis <martin@v.loewis.de>
parents: 17781
diff changeset
884 c = default_3way_compare(v, w);
bbac2a6a5d16 Patch #424475: Speed-up tp_compare usage, by special-casing the common
Martin v. Löwis <martin@v.loewis.de>
parents: 17781
diff changeset
885 if (c <= -2)
bbac2a6a5d16 Patch #424475: Speed-up tp_compare usage, by special-casing the common
Martin v. Löwis <martin@v.loewis.de>
parents: 17781
diff changeset
886 return NULL;
bbac2a6a5d16 Patch #424475: Speed-up tp_compare usage, by special-casing the common
Martin v. Löwis <martin@v.loewis.de>
parents: 17781
diff changeset
887 return convert_3way_to_object(op, c);
bbac2a6a5d16 Patch #424475: Speed-up tp_compare usage, by special-casing the common
Martin v. Löwis <martin@v.loewis.de>
parents: 17781
diff changeset
888 }
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
889
20863
d6ff1daae398 No code change -- just trying to document the return conditions for all
Tim Peters <tim.peters@gmail.com>
parents: 20612
diff changeset
890 /* Do rich comparison on v and w. Return
d6ff1daae398 No code change -- just trying to document the return conditions for all
Tim Peters <tim.peters@gmail.com>
parents: 20612
diff changeset
891 NULL if error
d6ff1daae398 No code change -- just trying to document the return conditions for all
Tim Peters <tim.peters@gmail.com>
parents: 20612
diff changeset
892 Else a new reference to an object other than Py_NotImplemented, usually(?):
d6ff1daae398 No code change -- just trying to document the return conditions for all
Tim Peters <tim.peters@gmail.com>
parents: 20612
diff changeset
893 Py_True if v op w
d6ff1daae398 No code change -- just trying to document the return conditions for all
Tim Peters <tim.peters@gmail.com>
parents: 20612
diff changeset
894 Py_False if not (v op w)
d6ff1daae398 No code change -- just trying to document the return conditions for all
Tim Peters <tim.peters@gmail.com>
parents: 20612
diff changeset
895 */
16079
c4355905b044 Remove a smelly export.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 16062
diff changeset
896 static PyObject *
15941
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
897 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
898 {
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
899 PyObject *res;
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
900
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
901 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
902 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
903 return res;
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
904 Py_DECREF(res);
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
905
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
906 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
907 }
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
908
20863
d6ff1daae398 No code change -- just trying to document the return conditions for all
Tim Peters <tim.peters@gmail.com>
parents: 20612
diff changeset
909 /* Return:
d6ff1daae398 No code change -- just trying to document the return conditions for all
Tim Peters <tim.peters@gmail.com>
parents: 20612
diff changeset
910 NULL for exception;
d6ff1daae398 No code change -- just trying to document the return conditions for all
Tim Peters <tim.peters@gmail.com>
parents: 20612
diff changeset
911 some object not equal to NotImplemented if it is implemented
d6ff1daae398 No code change -- just trying to document the return conditions for all
Tim Peters <tim.peters@gmail.com>
parents: 20612
diff changeset
912 (this latter object may not be a Boolean).
d6ff1daae398 No code change -- just trying to document the return conditions for all
Tim Peters <tim.peters@gmail.com>
parents: 20612
diff changeset
913 */
15941
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
914 PyObject *
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
915 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
916 {
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
917 PyObject *res;
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
918
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
919 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
920
15962
f1751abf771f Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents: 15952
diff changeset
921 compare_nesting++;
f1751abf771f Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents: 15952
diff changeset
922 if (compare_nesting > NESTING_LIMIT &&
f1751abf771f Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents: 15952
diff changeset
923 (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
924 || (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
925 && !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
926 && !PyTuple_Check(v)))) {
20865
f54828eb8e07 Rehabilitated the fast-path richcmp code, and sped it up. It wasn't
Tim Peters <tim.peters@gmail.com>
parents: 20863
diff changeset
927
15962
f1751abf771f Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents: 15952
diff changeset
928 /* 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
929 PyObject *token = check_recursion(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
930 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
931 res = NULL;
20865
f54828eb8e07 Rehabilitated the fast-path richcmp code, and sped it up. It wasn't
Tim Peters <tim.peters@gmail.com>
parents: 20863
diff changeset
932 goto Done;
15941
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
933 }
16001
f3740674ac5b Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents: 15962
diff changeset
934 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
935 /* 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
936 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
937 if (op == Py_EQ)
15962
f1751abf771f Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents: 15952
diff changeset
938 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
939 else if (op == Py_NE)
15962
f1751abf771f Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents: 15952
diff changeset
940 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
941 else {
f3740674ac5b Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents: 15962
diff changeset
942 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
943 "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
944 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
945 }
f3740674ac5b Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents: 15962
diff changeset
946 Py_XINCREF(res);
15962
f1751abf771f Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents: 15952
diff changeset
947 }
16001
f3740674ac5b Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents: 15962
diff changeset
948 else {
f3740674ac5b Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents: 15962
diff changeset
949 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
950 delete_token(token);
15962
f1751abf771f Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents: 15952
diff changeset
951 }
20865
f54828eb8e07 Rehabilitated the fast-path richcmp code, and sped it up. It wasn't
Tim Peters <tim.peters@gmail.com>
parents: 20863
diff changeset
952 goto Done;
15941
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
953 }
20865
f54828eb8e07 Rehabilitated the fast-path richcmp code, and sped it up. It wasn't
Tim Peters <tim.peters@gmail.com>
parents: 20863
diff changeset
954
f54828eb8e07 Rehabilitated the fast-path richcmp code, and sped it up. It wasn't
Tim Peters <tim.peters@gmail.com>
parents: 20863
diff changeset
955 /* No nesting extremism.
f54828eb8e07 Rehabilitated the fast-path richcmp code, and sped it up. It wasn't
Tim Peters <tim.peters@gmail.com>
parents: 20863
diff changeset
956 If the types are equal, and not old-style instances, try to
f54828eb8e07 Rehabilitated the fast-path richcmp code, and sped it up. It wasn't
Tim Peters <tim.peters@gmail.com>
parents: 20863
diff changeset
957 get out cheap (don't bother with coercions etc.). */
f54828eb8e07 Rehabilitated the fast-path richcmp code, and sped it up. It wasn't
Tim Peters <tim.peters@gmail.com>
parents: 20863
diff changeset
958 if (v->ob_type == w->ob_type && !PyInstance_Check(v)) {
f54828eb8e07 Rehabilitated the fast-path richcmp code, and sped it up. It wasn't
Tim Peters <tim.peters@gmail.com>
parents: 20863
diff changeset
959 cmpfunc fcmp;
f54828eb8e07 Rehabilitated the fast-path richcmp code, and sped it up. It wasn't
Tim Peters <tim.peters@gmail.com>
parents: 20863
diff changeset
960 richcmpfunc frich = RICHCOMPARE(v->ob_type);
f54828eb8e07 Rehabilitated the fast-path richcmp code, and sped it up. It wasn't
Tim Peters <tim.peters@gmail.com>
parents: 20863
diff changeset
961 /* If the type has richcmp, try it first. try_rich_compare
f54828eb8e07 Rehabilitated the fast-path richcmp code, and sped it up. It wasn't
Tim Peters <tim.peters@gmail.com>
parents: 20863
diff changeset
962 tries it two-sided, which is not needed since we've a
f54828eb8e07 Rehabilitated the fast-path richcmp code, and sped it up. It wasn't
Tim Peters <tim.peters@gmail.com>
parents: 20863
diff changeset
963 single type only. */
f54828eb8e07 Rehabilitated the fast-path richcmp code, and sped it up. It wasn't
Tim Peters <tim.peters@gmail.com>
parents: 20863
diff changeset
964 if (frich != NULL) {
f54828eb8e07 Rehabilitated the fast-path richcmp code, and sped it up. It wasn't
Tim Peters <tim.peters@gmail.com>
parents: 20863
diff changeset
965 res = (*frich)(v, w, op);
f54828eb8e07 Rehabilitated the fast-path richcmp code, and sped it up. It wasn't
Tim Peters <tim.peters@gmail.com>
parents: 20863
diff changeset
966 if (res != Py_NotImplemented)
f54828eb8e07 Rehabilitated the fast-path richcmp code, and sped it up. It wasn't
Tim Peters <tim.peters@gmail.com>
parents: 20863
diff changeset
967 goto Done;
f54828eb8e07 Rehabilitated the fast-path richcmp code, and sped it up. It wasn't
Tim Peters <tim.peters@gmail.com>
parents: 20863
diff changeset
968 Py_DECREF(res);
f54828eb8e07 Rehabilitated the fast-path richcmp code, and sped it up. It wasn't
Tim Peters <tim.peters@gmail.com>
parents: 20863
diff changeset
969 }
f54828eb8e07 Rehabilitated the fast-path richcmp code, and sped it up. It wasn't
Tim Peters <tim.peters@gmail.com>
parents: 20863
diff changeset
970 /* No richcmp, or this particular richmp not implemented.
f54828eb8e07 Rehabilitated the fast-path richcmp code, and sped it up. It wasn't
Tim Peters <tim.peters@gmail.com>
parents: 20863
diff changeset
971 Try 3-way cmp. */
f54828eb8e07 Rehabilitated the fast-path richcmp code, and sped it up. It wasn't
Tim Peters <tim.peters@gmail.com>
parents: 20863
diff changeset
972 fcmp = v->ob_type->tp_compare;
f54828eb8e07 Rehabilitated the fast-path richcmp code, and sped it up. It wasn't
Tim Peters <tim.peters@gmail.com>
parents: 20863
diff changeset
973 if (fcmp != NULL) {
f54828eb8e07 Rehabilitated the fast-path richcmp code, and sped it up. It wasn't
Tim Peters <tim.peters@gmail.com>
parents: 20863
diff changeset
974 int c = (*fcmp)(v, w);
23598
2b1d61781d63 Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents: 23542
diff changeset
975 c = adjust_tp_compare(c);
2b1d61781d63 Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents: 23542
diff changeset
976 if (c == -2) {
20865
f54828eb8e07 Rehabilitated the fast-path richcmp code, and sped it up. It wasn't
Tim Peters <tim.peters@gmail.com>
parents: 20863
diff changeset
977 res = NULL;
f54828eb8e07 Rehabilitated the fast-path richcmp code, and sped it up. It wasn't
Tim Peters <tim.peters@gmail.com>
parents: 20863
diff changeset
978 goto Done;
f54828eb8e07 Rehabilitated the fast-path richcmp code, and sped it up. It wasn't
Tim Peters <tim.peters@gmail.com>
parents: 20863
diff changeset
979 }
f54828eb8e07 Rehabilitated the fast-path richcmp code, and sped it up. It wasn't
Tim Peters <tim.peters@gmail.com>
parents: 20863
diff changeset
980 res = convert_3way_to_object(op, c);
f54828eb8e07 Rehabilitated the fast-path richcmp code, and sped it up. It wasn't
Tim Peters <tim.peters@gmail.com>
parents: 20863
diff changeset
981 goto Done;
f54828eb8e07 Rehabilitated the fast-path richcmp code, and sped it up. It wasn't
Tim Peters <tim.peters@gmail.com>
parents: 20863
diff changeset
982 }
15962
f1751abf771f Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents: 15952
diff changeset
983 }
20865
f54828eb8e07 Rehabilitated the fast-path richcmp code, and sped it up. It wasn't
Tim Peters <tim.peters@gmail.com>
parents: 20863
diff changeset
984
f54828eb8e07 Rehabilitated the fast-path richcmp code, and sped it up. It wasn't
Tim Peters <tim.peters@gmail.com>
parents: 20863
diff changeset
985 /* Fast path not taken, or couldn't deliver a useful result. */
f54828eb8e07 Rehabilitated the fast-path richcmp code, and sped it up. It wasn't
Tim Peters <tim.peters@gmail.com>
parents: 20863
diff changeset
986 res = do_richcmp(v, w, op);
f54828eb8e07 Rehabilitated the fast-path richcmp code, and sped it up. It wasn't
Tim Peters <tim.peters@gmail.com>
parents: 20863
diff changeset
987 Done:
15962
f1751abf771f Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents: 15952
diff changeset
988 compare_nesting--;
15941
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
989 return res;
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
990 }
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
991
17688
1ebb4e566257 Make 'x in y' and 'x not in y' (PySequence_Contains) play nice w/ iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17673
diff changeset
992 /* Return -1 if error; 1 if v op w; 0 if not (v op w). */
15941
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
993 int
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
994 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
995 {
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
996 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
997 int ok;
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
998
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
999 if (res == NULL)
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
1000 return -1;
25013
68627e0acd15 Speedup for PyObject_RichCompareBool(): PyObject_RichCompare() almost
Guido van Rossum <guido@python.org>
parents: 24897
diff changeset
1001 if (PyBool_Check(res))
68627e0acd15 Speedup for PyObject_RichCompareBool(): PyObject_RichCompare() almost
Guido van Rossum <guido@python.org>
parents: 24897
diff changeset
1002 ok = (res == Py_True);
68627e0acd15 Speedup for PyObject_RichCompareBool(): PyObject_RichCompare() almost
Guido van Rossum <guido@python.org>
parents: 24897
diff changeset
1003 else
68627e0acd15 Speedup for PyObject_RichCompareBool(): PyObject_RichCompare() almost
Guido van Rossum <guido@python.org>
parents: 24897
diff changeset
1004 ok = PyObject_IsTrue(res);
15941
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
1005 Py_DECREF(res);
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
1006 return ok;
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
1007 }
13141
cca84745da3e This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents: 13089
diff changeset
1008
cca84745da3e This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents: 13089
diff changeset
1009 /* 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
1010 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
1011
cca84745da3e This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents: 13089
diff changeset
1012 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
1013 */
cca84745da3e This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents: 13089
diff changeset
1014
cca84745da3e This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents: 13089
diff changeset
1015 long
13473
31b1f691a7d9 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1016 _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
1017 {
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
1018 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
1019 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
1020 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
1021 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
1022 /* 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
1023 * 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
1024 * 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
1025 */
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
1026
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
1027 #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
1028 {
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
1029 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
1030 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
1031 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
1032 }
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
1033 #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
1034 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
1035 #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
1036 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
1037 /* 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
1038 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
1039 /* 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
1040 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
1041 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
1042 /* 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
1043 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
1044 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
1045 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
1046 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
1047 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
1048 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
1049 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
1050 }
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
1051 /* 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
1052 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
1053 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
1054 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
1055 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
1056 }
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
1057 /* 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
1058 * 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
1059 * 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
1060 * 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
1061 * 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
1062 * 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
1063 * 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
1064 * 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
1065 * 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
1066 * 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
1067 * 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
1068 */
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
1069 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
1070 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
1071 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
1072 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
1073 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
1074 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
1075 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
1076 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
1077 }
cca84745da3e This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents: 13089
diff changeset
1078
cca84745da3e This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents: 13089
diff changeset
1079 long
13473
31b1f691a7d9 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1080 _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
1081 {
cca84745da3e This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents: 13089
diff changeset
1082 #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
1083 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
1084 #else
cca84745da3e This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents: 13089
diff changeset
1085 /* 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
1086 PyObject* longobj;
cca84745da3e This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents: 13089
diff changeset
1087 long x;
24122
d332484e394a Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents: 24121
diff changeset
1088
13141
cca84745da3e This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents: 13089
diff changeset
1089 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
1090 x = -1;
cca84745da3e This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents: 13089
diff changeset
1091 goto finally;
cca84745da3e This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents: 13089
diff changeset
1092 }
cca84745da3e This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents: 13089
diff changeset
1093 x = PyObject_Hash(longobj);
24122
d332484e394a Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents: 24121
diff changeset
1094
13141
cca84745da3e This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents: 13089
diff changeset
1095 finally:
cca84745da3e This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents: 13089
diff changeset
1096 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
1097 return x;
cca84745da3e This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents: 13089
diff changeset
1098 #endif
cca84745da3e This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents: 13089
diff changeset
1099 }
cca84745da3e This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents: 13089
diff changeset
1100
cca84745da3e This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents: 13089
diff changeset
1101
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
1102 long
13473
31b1f691a7d9 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1103 PyObject_Hash(PyObject *v)
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
1104 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5006
diff changeset
1105 PyTypeObject *tp = v->ob_type;
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
1106 if (tp->tp_hash != NULL)
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
1107 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
1108 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
1109 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
1110 }
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
1111 /* 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
1112 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
1113 return -1;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
1114 }
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
1115
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5006
diff changeset
1116 PyObject *
13473
31b1f691a7d9 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1117 PyObject_GetAttrString(PyObject *v, char *name)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
1118 {
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1119 PyObject *w, *res;
3986
9c2cbf306864 Support for tp_getattro, tp_setattro (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 3770
diff changeset
1120
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1121 if (v->ob_type->tp_getattr != NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1122 return (*v->ob_type->tp_getattr)(v, name);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1123 w = PyString_InternFromString(name);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1124 if (w == NULL)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
1125 return NULL;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1126 res = PyObject_GetAttr(v, w);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1127 Py_XDECREF(w);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1128 return res;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
1129 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
1130
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
1131 int
13473
31b1f691a7d9 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1132 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
1133 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5006
diff changeset
1134 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
1135 if (res != NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5006
diff changeset
1136 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
1137 return 1;
716be765a79b * accessobject.c (ownercheck): allow a base class access to protected
Guido van Rossum <guido@python.org>
parents: 1282
diff changeset
1138 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5006
diff changeset
1139 PyErr_Clear();
1324
716be765a79b * accessobject.c (ownercheck): allow a base class access to protected
Guido van Rossum <guido@python.org>
parents: 1282
diff changeset
1140 return 0;
716be765a79b * accessobject.c (ownercheck): allow a base class access to protected
Guido van Rossum <guido@python.org>
parents: 1282
diff changeset
1141 }
716be765a79b * accessobject.c (ownercheck): allow a base class access to protected
Guido van Rossum <guido@python.org>
parents: 1282
diff changeset
1142
716be765a79b * accessobject.c (ownercheck): allow a base class access to protected
Guido van Rossum <guido@python.org>
parents: 1282
diff changeset
1143 int
13473
31b1f691a7d9 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1144 PyObject_SetAttrString(PyObject *v, char *name, PyObject *w)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
1145 {
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1146 PyObject *s;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1147 int res;
3986
9c2cbf306864 Support for tp_getattro, tp_setattro (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 3770
diff changeset
1148
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1149 if (v->ob_type->tp_setattr != NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1150 return (*v->ob_type->tp_setattr)(v, name, w);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1151 s = PyString_InternFromString(name);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1152 if (s == NULL)
95
a5bf7d5959ce Return -1 for errors.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
1153 return -1;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1154 res = PyObject_SetAttr(v, s, w);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1155 Py_XDECREF(s);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1156 return res;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
1157 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
1158
5560
8ec298b255a2 Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents: 5535
diff changeset
1159 PyObject *
13473
31b1f691a7d9 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1160 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
1161 {
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1162 PyTypeObject *tp = v->ob_type;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1163
22302
a0cc52735e6e Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents: 22112
diff changeset
1164 if (!PyString_Check(name)) {
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19294
diff changeset
1165 #ifdef Py_USING_UNICODE
22302
a0cc52735e6e Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents: 22112
diff changeset
1166 /* The Unicode to string conversion is done here because the
a0cc52735e6e Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents: 22112
diff changeset
1167 existing tp_getattro slots expect a string object as name
a0cc52735e6e Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents: 22112
diff changeset
1168 and we wouldn't want to break those. */
a0cc52735e6e Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents: 22112
diff changeset
1169 if (PyUnicode_Check(name)) {
a0cc52735e6e Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents: 22112
diff changeset
1170 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
a0cc52735e6e Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents: 22112
diff changeset
1171 if (name == NULL)
a0cc52735e6e Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents: 22112
diff changeset
1172 return NULL;
a0cc52735e6e Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents: 22112
diff changeset
1173 }
a0cc52735e6e Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents: 22112
diff changeset
1174 else
a0cc52735e6e Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents: 22112
diff changeset
1175 #endif
a0cc52735e6e Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents: 22112
diff changeset
1176 {
a0cc52735e6e Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents: 22112
diff changeset
1177 PyErr_SetString(PyExc_TypeError,
a0cc52735e6e Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents: 22112
diff changeset
1178 "attribute name must be string");
14660
983502b2cd43 PyObject_SetAttr() and PyObject_GetAttr() now also accept Unicode
Marc-André Lemburg <mal@egenix.com>
parents: 14375
diff changeset
1179 return NULL;
22302
a0cc52735e6e Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents: 22112
diff changeset
1180 }
12991
1a40f00a48bf raise TypeError when PyObject_Get/SetAttr called with non-string name
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12904
diff changeset
1181 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1182 if (tp->tp_getattro != NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1183 return (*tp->tp_getattro)(v, name);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1184 if (tp->tp_getattr != NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1185 return (*tp->tp_getattr)(v, PyString_AS_STRING(name));
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1186 PyErr_Format(PyExc_AttributeError,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1187 "'%.50s' object has no attribute '%.400s'",
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1188 tp->tp_name, PyString_AS_STRING(name));
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1189 return NULL;
5560
8ec298b255a2 Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents: 5535
diff changeset
1190 }
8ec298b255a2 Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents: 5535
diff changeset
1191
8ec298b255a2 Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents: 5535
diff changeset
1192 int
13473
31b1f691a7d9 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1193 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
1194 {
8ec298b255a2 Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents: 5535
diff changeset
1195 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
1196 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
1197 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
1198 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
1199 }
8ec298b255a2 Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents: 5535
diff changeset
1200 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
1201 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
1202 }
8ec298b255a2 Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents: 5535
diff changeset
1203
8ec298b255a2 Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents: 5535
diff changeset
1204 int
13473
31b1f691a7d9 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1205 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
1206 {
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1207 PyTypeObject *tp = v->ob_type;
5560
8ec298b255a2 Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents: 5535
diff changeset
1208 int err;
14660
983502b2cd43 PyObject_SetAttr() and PyObject_GetAttr() now also accept Unicode
Marc-André Lemburg <mal@egenix.com>
parents: 14375
diff changeset
1209
22302
a0cc52735e6e Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents: 22112
diff changeset
1210 if (!PyString_Check(name)){
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19294
diff changeset
1211 #ifdef Py_USING_UNICODE
22302
a0cc52735e6e Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents: 22112
diff changeset
1212 /* The Unicode to string conversion is done here because the
a0cc52735e6e Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents: 22112
diff changeset
1213 existing tp_setattro slots expect a string object as name
a0cc52735e6e Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents: 22112
diff changeset
1214 and we wouldn't want to break those. */
a0cc52735e6e Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents: 22112
diff changeset
1215 if (PyUnicode_Check(name)) {
a0cc52735e6e Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents: 22112
diff changeset
1216 name = PyUnicode_AsEncodedString(name, NULL, NULL);
a0cc52735e6e Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents: 22112
diff changeset
1217 if (name == NULL)
a0cc52735e6e Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents: 22112
diff changeset
1218 return -1;
a0cc52735e6e Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents: 22112
diff changeset
1219 }
24122
d332484e394a Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents: 24121
diff changeset
1220 else
22302
a0cc52735e6e Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents: 22112
diff changeset
1221 #endif
a0cc52735e6e Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents: 22112
diff changeset
1222 {
a0cc52735e6e Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents: 22112
diff changeset
1223 PyErr_SetString(PyExc_TypeError,
a0cc52735e6e Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents: 22112
diff changeset
1224 "attribute name must be string");
14660
983502b2cd43 PyObject_SetAttr() and PyObject_GetAttr() now also accept Unicode
Marc-André Lemburg <mal@egenix.com>
parents: 14375
diff changeset
1225 return -1;
22302
a0cc52735e6e Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents: 22112
diff changeset
1226 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1227 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1228 else
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1229 Py_INCREF(name);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1230
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1231 PyString_InternInPlace(&name);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1232 if (tp->tp_setattro != NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1233 err = (*tp->tp_setattro)(v, name, value);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1234 Py_DECREF(name);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1235 return err;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1236 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1237 if (tp->tp_setattr != NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1238 err = (*tp->tp_setattr)(v, PyString_AS_STRING(name), value);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1239 Py_DECREF(name);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1240 return err;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1241 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1242 Py_DECREF(name);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1243 if (tp->tp_getattr == NULL && tp->tp_getattro == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1244 PyErr_Format(PyExc_TypeError,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1245 "'%.100s' object has no attributes "
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1246 "(%s .%.100s)",
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1247 tp->tp_name,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1248 value==NULL ? "del" : "assign to",
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1249 PyString_AS_STRING(name));
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1250 else
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1251 PyErr_Format(PyExc_TypeError,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1252 "'%.100s' object has only read-only attributes "
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1253 "(%s .%.100s)",
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1254 tp->tp_name,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1255 value==NULL ? "del" : "assign to",
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1256 PyString_AS_STRING(name));
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1257 return -1;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1258 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1259
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1260 /* Helper to get a pointer to an object's __dict__ slot, if any */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1261
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1262 PyObject **
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1263 _PyObject_GetDictPtr(PyObject *obj)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1264 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1265 long dictoffset;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1266 PyTypeObject *tp = obj->ob_type;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1267
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1268 if (!(tp->tp_flags & Py_TPFLAGS_HAVE_CLASS))
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1269 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1270 dictoffset = tp->tp_dictoffset;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1271 if (dictoffset == 0)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1272 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1273 if (dictoffset < 0) {
22106
93eb4b61f687 SF patch 514641 (Naofumi Honda) - Negative ob_size of LongObjects
Guido van Rossum <guido@python.org>
parents: 21186
diff changeset
1274 int tsize;
93eb4b61f687 SF patch 514641 (Naofumi Honda) - Negative ob_size of LongObjects
Guido van Rossum <guido@python.org>
parents: 21186
diff changeset
1275 size_t size;
93eb4b61f687 SF patch 514641 (Naofumi Honda) - Negative ob_size of LongObjects
Guido van Rossum <guido@python.org>
parents: 21186
diff changeset
1276
93eb4b61f687 SF patch 514641 (Naofumi Honda) - Negative ob_size of LongObjects
Guido van Rossum <guido@python.org>
parents: 21186
diff changeset
1277 tsize = ((PyVarObject *)obj)->ob_size;
93eb4b61f687 SF patch 514641 (Naofumi Honda) - Negative ob_size of LongObjects
Guido van Rossum <guido@python.org>
parents: 21186
diff changeset
1278 if (tsize < 0)
93eb4b61f687 SF patch 514641 (Naofumi Honda) - Negative ob_size of LongObjects
Guido van Rossum <guido@python.org>
parents: 21186
diff changeset
1279 tsize = -tsize;
93eb4b61f687 SF patch 514641 (Naofumi Honda) - Negative ob_size of LongObjects
Guido van Rossum <guido@python.org>
parents: 21186
diff changeset
1280 size = _PyObject_VAR_SIZE(tp, tsize);
93eb4b61f687 SF patch 514641 (Naofumi Honda) - Negative ob_size of LongObjects
Guido van Rossum <guido@python.org>
parents: 21186
diff changeset
1281
20294
426b379a138b _PyObject_VAR_SIZE: always round up to a multiple-of-pointer-size value.
Tim Peters <tim.peters@gmail.com>
parents: 20292
diff changeset
1282 dictoffset += (long)size;
426b379a138b _PyObject_VAR_SIZE: always round up to a multiple-of-pointer-size value.
Tim Peters <tim.peters@gmail.com>
parents: 20292
diff changeset
1283 assert(dictoffset > 0);
426b379a138b _PyObject_VAR_SIZE: always round up to a multiple-of-pointer-size value.
Tim Peters <tim.peters@gmail.com>
parents: 20292
diff changeset
1284 assert(dictoffset % SIZEOF_VOID_P == 0);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1285 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1286 return (PyObject **) ((char *)obj + dictoffset);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1287 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1288
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1289 /* Generic GetAttr functions - put these in your tp_[gs]etattro slot */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1290
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1291 PyObject *
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1292 PyObject_GenericGetAttr(PyObject *obj, PyObject *name)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1293 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1294 PyTypeObject *tp = obj->ob_type;
24897
6310a4a21eb8 Another modest speedup in PyObject_GenericGetAttr(): inline the call
Guido van Rossum <guido@python.org>
parents: 24894
diff changeset
1295 PyObject *descr = NULL;
21186
0189186f4ed9 PyObject_Generic{Get,Set}Attr(): ensure that the attribute name is a
Guido van Rossum <guido@python.org>
parents: 20865
diff changeset
1296 PyObject *res = NULL;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1297 descrgetfunc f;
24894
8862bc59685a Inline call to _PyObject_GetDictPtr() in PyObject_GenericGetAttr().
Guido van Rossum <guido@python.org>
parents: 24644
diff changeset
1298 long dictoffset;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1299 PyObject **dictptr;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1300
22302
a0cc52735e6e Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents: 22112
diff changeset
1301 if (!PyString_Check(name)){
21186
0189186f4ed9 PyObject_Generic{Get,Set}Attr(): ensure that the attribute name is a
Guido van Rossum <guido@python.org>
parents: 20865
diff changeset
1302 #ifdef Py_USING_UNICODE
22302
a0cc52735e6e Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents: 22112
diff changeset
1303 /* The Unicode to string conversion is done here because the
a0cc52735e6e Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents: 22112
diff changeset
1304 existing tp_setattro slots expect a string object as name
a0cc52735e6e Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents: 22112
diff changeset
1305 and we wouldn't want to break those. */
a0cc52735e6e Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents: 22112
diff changeset
1306 if (PyUnicode_Check(name)) {
a0cc52735e6e Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents: 22112
diff changeset
1307 name = PyUnicode_AsEncodedString(name, NULL, NULL);
a0cc52735e6e Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents: 22112
diff changeset
1308 if (name == NULL)
a0cc52735e6e Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents: 22112
diff changeset
1309 return NULL;
a0cc52735e6e Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents: 22112
diff changeset
1310 }
24122
d332484e394a Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents: 24121
diff changeset
1311 else
22302
a0cc52735e6e Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents: 22112
diff changeset
1312 #endif
a0cc52735e6e Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents: 22112
diff changeset
1313 {
a0cc52735e6e Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents: 22112
diff changeset
1314 PyErr_SetString(PyExc_TypeError,
a0cc52735e6e Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents: 22112
diff changeset
1315 "attribute name must be string");
21186
0189186f4ed9 PyObject_Generic{Get,Set}Attr(): ensure that the attribute name is a
Guido van Rossum <guido@python.org>
parents: 20865
diff changeset
1316 return NULL;
22302
a0cc52735e6e Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents: 22112
diff changeset
1317 }
21186
0189186f4ed9 PyObject_Generic{Get,Set}Attr(): ensure that the attribute name is a
Guido van Rossum <guido@python.org>
parents: 20865
diff changeset
1318 }
0189186f4ed9 PyObject_Generic{Get,Set}Attr(): ensure that the attribute name is a
Guido van Rossum <guido@python.org>
parents: 20865
diff changeset
1319 else
0189186f4ed9 PyObject_Generic{Get,Set}Attr(): ensure that the attribute name is a
Guido van Rossum <guido@python.org>
parents: 20865
diff changeset
1320 Py_INCREF(name);
0189186f4ed9 PyObject_Generic{Get,Set}Attr(): ensure that the attribute name is a
Guido van Rossum <guido@python.org>
parents: 20865
diff changeset
1321
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1322 if (tp->tp_dict == NULL) {
19040
4df72ea78ae8 - Rename PyType_InitDict() to PyType_Ready().
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1323 if (PyType_Ready(tp) < 0)
21186
0189186f4ed9 PyObject_Generic{Get,Set}Attr(): ensure that the attribute name is a
Guido van Rossum <guido@python.org>
parents: 20865
diff changeset
1324 goto done;
12991
1a40f00a48bf raise TypeError when PyObject_Get/SetAttr called with non-string name
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12904
diff changeset
1325 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1326
24897
6310a4a21eb8 Another modest speedup in PyObject_GenericGetAttr(): inline the call
Guido van Rossum <guido@python.org>
parents: 24894
diff changeset
1327 /* Inline _PyType_Lookup */
6310a4a21eb8 Another modest speedup in PyObject_GenericGetAttr(): inline the call
Guido van Rossum <guido@python.org>
parents: 24894
diff changeset
1328 {
6310a4a21eb8 Another modest speedup in PyObject_GenericGetAttr(): inline the call
Guido van Rossum <guido@python.org>
parents: 24894
diff changeset
1329 int i, n;
6310a4a21eb8 Another modest speedup in PyObject_GenericGetAttr(): inline the call
Guido van Rossum <guido@python.org>
parents: 24894
diff changeset
1330 PyObject *mro, *base, *dict;
6310a4a21eb8 Another modest speedup in PyObject_GenericGetAttr(): inline the call
Guido van Rossum <guido@python.org>
parents: 24894
diff changeset
1331
6310a4a21eb8 Another modest speedup in PyObject_GenericGetAttr(): inline the call
Guido van Rossum <guido@python.org>
parents: 24894
diff changeset
1332 /* Look in tp_dict of types in MRO */
6310a4a21eb8 Another modest speedup in PyObject_GenericGetAttr(): inline the call
Guido van Rossum <guido@python.org>
parents: 24894
diff changeset
1333 mro = tp->tp_mro;
6310a4a21eb8 Another modest speedup in PyObject_GenericGetAttr(): inline the call
Guido van Rossum <guido@python.org>
parents: 24894
diff changeset
1334 assert(mro != NULL);
6310a4a21eb8 Another modest speedup in PyObject_GenericGetAttr(): inline the call
Guido van Rossum <guido@python.org>
parents: 24894
diff changeset
1335 assert(PyTuple_Check(mro));
6310a4a21eb8 Another modest speedup in PyObject_GenericGetAttr(): inline the call
Guido van Rossum <guido@python.org>
parents: 24894
diff changeset
1336 n = PyTuple_GET_SIZE(mro);
6310a4a21eb8 Another modest speedup in PyObject_GenericGetAttr(): inline the call
Guido van Rossum <guido@python.org>
parents: 24894
diff changeset
1337 for (i = 0; i < n; i++) {
6310a4a21eb8 Another modest speedup in PyObject_GenericGetAttr(): inline the call
Guido van Rossum <guido@python.org>
parents: 24894
diff changeset
1338 base = PyTuple_GET_ITEM(mro, i);
6310a4a21eb8 Another modest speedup in PyObject_GenericGetAttr(): inline the call
Guido van Rossum <guido@python.org>
parents: 24894
diff changeset
1339 if (PyClass_Check(base))
6310a4a21eb8 Another modest speedup in PyObject_GenericGetAttr(): inline the call
Guido van Rossum <guido@python.org>
parents: 24894
diff changeset
1340 dict = ((PyClassObject *)base)->cl_dict;
6310a4a21eb8 Another modest speedup in PyObject_GenericGetAttr(): inline the call
Guido van Rossum <guido@python.org>
parents: 24894
diff changeset
1341 else {
6310a4a21eb8 Another modest speedup in PyObject_GenericGetAttr(): inline the call
Guido van Rossum <guido@python.org>
parents: 24894
diff changeset
1342 assert(PyType_Check(base));
6310a4a21eb8 Another modest speedup in PyObject_GenericGetAttr(): inline the call
Guido van Rossum <guido@python.org>
parents: 24894
diff changeset
1343 dict = ((PyTypeObject *)base)->tp_dict;
6310a4a21eb8 Another modest speedup in PyObject_GenericGetAttr(): inline the call
Guido van Rossum <guido@python.org>
parents: 24894
diff changeset
1344 }
6310a4a21eb8 Another modest speedup in PyObject_GenericGetAttr(): inline the call
Guido van Rossum <guido@python.org>
parents: 24894
diff changeset
1345 assert(dict && PyDict_Check(dict));
6310a4a21eb8 Another modest speedup in PyObject_GenericGetAttr(): inline the call
Guido van Rossum <guido@python.org>
parents: 24894
diff changeset
1346 descr = PyDict_GetItem(dict, name);
6310a4a21eb8 Another modest speedup in PyObject_GenericGetAttr(): inline the call
Guido van Rossum <guido@python.org>
parents: 24894
diff changeset
1347 if (descr != NULL)
6310a4a21eb8 Another modest speedup in PyObject_GenericGetAttr(): inline the call
Guido van Rossum <guido@python.org>
parents: 24894
diff changeset
1348 break;
6310a4a21eb8 Another modest speedup in PyObject_GenericGetAttr(): inline the call
Guido van Rossum <guido@python.org>
parents: 24894
diff changeset
1349 }
6310a4a21eb8 Another modest speedup in PyObject_GenericGetAttr(): inline the call
Guido van Rossum <guido@python.org>
parents: 24894
diff changeset
1350 }
6310a4a21eb8 Another modest speedup in PyObject_GenericGetAttr(): inline the call
Guido van Rossum <guido@python.org>
parents: 24894
diff changeset
1351
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1352 f = NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1353 if (descr != NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1354 f = descr->ob_type->tp_descr_get;
21186
0189186f4ed9 PyObject_Generic{Get,Set}Attr(): ensure that the attribute name is a
Guido van Rossum <guido@python.org>
parents: 20865
diff changeset
1355 if (f != NULL && PyDescr_IsData(descr)) {
0189186f4ed9 PyObject_Generic{Get,Set}Attr(): ensure that the attribute name is a
Guido van Rossum <guido@python.org>
parents: 20865
diff changeset
1356 res = f(descr, obj, (PyObject *)obj->ob_type);
0189186f4ed9 PyObject_Generic{Get,Set}Attr(): ensure that the attribute name is a
Guido van Rossum <guido@python.org>
parents: 20865
diff changeset
1357 goto done;
0189186f4ed9 PyObject_Generic{Get,Set}Attr(): ensure that the attribute name is a
Guido van Rossum <guido@python.org>
parents: 20865
diff changeset
1358 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1359 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1360
24894
8862bc59685a Inline call to _PyObject_GetDictPtr() in PyObject_GenericGetAttr().
Guido van Rossum <guido@python.org>
parents: 24644
diff changeset
1361 /* Inline _PyObject_GetDictPtr */
8862bc59685a Inline call to _PyObject_GetDictPtr() in PyObject_GenericGetAttr().
Guido van Rossum <guido@python.org>
parents: 24644
diff changeset
1362 dictoffset = tp->tp_dictoffset;
8862bc59685a Inline call to _PyObject_GetDictPtr() in PyObject_GenericGetAttr().
Guido van Rossum <guido@python.org>
parents: 24644
diff changeset
1363 if (dictoffset != 0) {
8862bc59685a Inline call to _PyObject_GetDictPtr() in PyObject_GenericGetAttr().
Guido van Rossum <guido@python.org>
parents: 24644
diff changeset
1364 PyObject *dict;
8862bc59685a Inline call to _PyObject_GetDictPtr() in PyObject_GenericGetAttr().
Guido van Rossum <guido@python.org>
parents: 24644
diff changeset
1365 if (dictoffset < 0) {
8862bc59685a Inline call to _PyObject_GetDictPtr() in PyObject_GenericGetAttr().
Guido van Rossum <guido@python.org>
parents: 24644
diff changeset
1366 int tsize;
8862bc59685a Inline call to _PyObject_GetDictPtr() in PyObject_GenericGetAttr().
Guido van Rossum <guido@python.org>
parents: 24644
diff changeset
1367 size_t size;
8862bc59685a Inline call to _PyObject_GetDictPtr() in PyObject_GenericGetAttr().
Guido van Rossum <guido@python.org>
parents: 24644
diff changeset
1368
8862bc59685a Inline call to _PyObject_GetDictPtr() in PyObject_GenericGetAttr().
Guido van Rossum <guido@python.org>
parents: 24644
diff changeset
1369 tsize = ((PyVarObject *)obj)->ob_size;
8862bc59685a Inline call to _PyObject_GetDictPtr() in PyObject_GenericGetAttr().
Guido van Rossum <guido@python.org>
parents: 24644
diff changeset
1370 if (tsize < 0)
8862bc59685a Inline call to _PyObject_GetDictPtr() in PyObject_GenericGetAttr().
Guido van Rossum <guido@python.org>
parents: 24644
diff changeset
1371 tsize = -tsize;
8862bc59685a Inline call to _PyObject_GetDictPtr() in PyObject_GenericGetAttr().
Guido van Rossum <guido@python.org>
parents: 24644
diff changeset
1372 size = _PyObject_VAR_SIZE(tp, tsize);
8862bc59685a Inline call to _PyObject_GetDictPtr() in PyObject_GenericGetAttr().
Guido van Rossum <guido@python.org>
parents: 24644
diff changeset
1373
8862bc59685a Inline call to _PyObject_GetDictPtr() in PyObject_GenericGetAttr().
Guido van Rossum <guido@python.org>
parents: 24644
diff changeset
1374 dictoffset += (long)size;
8862bc59685a Inline call to _PyObject_GetDictPtr() in PyObject_GenericGetAttr().
Guido van Rossum <guido@python.org>
parents: 24644
diff changeset
1375 assert(dictoffset > 0);
8862bc59685a Inline call to _PyObject_GetDictPtr() in PyObject_GenericGetAttr().
Guido van Rossum <guido@python.org>
parents: 24644
diff changeset
1376 assert(dictoffset % SIZEOF_VOID_P == 0);
8862bc59685a Inline call to _PyObject_GetDictPtr() in PyObject_GenericGetAttr().
Guido van Rossum <guido@python.org>
parents: 24644
diff changeset
1377 }
8862bc59685a Inline call to _PyObject_GetDictPtr() in PyObject_GenericGetAttr().
Guido van Rossum <guido@python.org>
parents: 24644
diff changeset
1378 dictptr = (PyObject **) ((char *)obj + dictoffset);
8862bc59685a Inline call to _PyObject_GetDictPtr() in PyObject_GenericGetAttr().
Guido van Rossum <guido@python.org>
parents: 24644
diff changeset
1379 dict = *dictptr;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1380 if (dict != NULL) {
21186
0189186f4ed9 PyObject_Generic{Get,Set}Attr(): ensure that the attribute name is a
Guido van Rossum <guido@python.org>
parents: 20865
diff changeset
1381 res = PyDict_GetItem(dict, name);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1382 if (res != NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1383 Py_INCREF(res);
21186
0189186f4ed9 PyObject_Generic{Get,Set}Attr(): ensure that the attribute name is a
Guido van Rossum <guido@python.org>
parents: 20865
diff changeset
1384 goto done;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1385 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1386 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1387 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1388
21186
0189186f4ed9 PyObject_Generic{Get,Set}Attr(): ensure that the attribute name is a
Guido van Rossum <guido@python.org>
parents: 20865
diff changeset
1389 if (f != NULL) {
0189186f4ed9 PyObject_Generic{Get,Set}Attr(): ensure that the attribute name is a
Guido van Rossum <guido@python.org>
parents: 20865
diff changeset
1390 res = f(descr, obj, (PyObject *)obj->ob_type);
0189186f4ed9 PyObject_Generic{Get,Set}Attr(): ensure that the attribute name is a
Guido van Rossum <guido@python.org>
parents: 20865
diff changeset
1391 goto done;
0189186f4ed9 PyObject_Generic{Get,Set}Attr(): ensure that the attribute name is a
Guido van Rossum <guido@python.org>
parents: 20865
diff changeset
1392 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1393
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1394 if (descr != NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1395 Py_INCREF(descr);
21186
0189186f4ed9 PyObject_Generic{Get,Set}Attr(): ensure that the attribute name is a
Guido van Rossum <guido@python.org>
parents: 20865
diff changeset
1396 res = descr;
0189186f4ed9 PyObject_Generic{Get,Set}Attr(): ensure that the attribute name is a
Guido van Rossum <guido@python.org>
parents: 20865
diff changeset
1397 goto done;
14660
983502b2cd43 PyObject_SetAttr() and PyObject_GetAttr() now also accept Unicode
Marc-André Lemburg <mal@egenix.com>
parents: 14375
diff changeset
1398 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1399
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1400 PyErr_Format(PyExc_AttributeError,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1401 "'%.50s' object has no attribute '%.400s'",
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1402 tp->tp_name, PyString_AS_STRING(name));
21186
0189186f4ed9 PyObject_Generic{Get,Set}Attr(): ensure that the attribute name is a
Guido van Rossum <guido@python.org>
parents: 20865
diff changeset
1403 done:
0189186f4ed9 PyObject_Generic{Get,Set}Attr(): ensure that the attribute name is a
Guido van Rossum <guido@python.org>
parents: 20865
diff changeset
1404 Py_DECREF(name);
0189186f4ed9 PyObject_Generic{Get,Set}Attr(): ensure that the attribute name is a
Guido van Rossum <guido@python.org>
parents: 20865
diff changeset
1405 return res;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1406 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1407
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1408 int
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1409 PyObject_GenericSetAttr(PyObject *obj, PyObject *name, PyObject *value)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1410 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1411 PyTypeObject *tp = obj->ob_type;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1412 PyObject *descr;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1413 descrsetfunc f;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1414 PyObject **dictptr;
21186
0189186f4ed9 PyObject_Generic{Get,Set}Attr(): ensure that the attribute name is a
Guido van Rossum <guido@python.org>
parents: 20865
diff changeset
1415 int res = -1;
0189186f4ed9 PyObject_Generic{Get,Set}Attr(): ensure that the attribute name is a
Guido van Rossum <guido@python.org>
parents: 20865
diff changeset
1416
22302
a0cc52735e6e Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents: 22112
diff changeset
1417 if (!PyString_Check(name)){
21186
0189186f4ed9 PyObject_Generic{Get,Set}Attr(): ensure that the attribute name is a
Guido van Rossum <guido@python.org>
parents: 20865
diff changeset
1418 #ifdef Py_USING_UNICODE
22302
a0cc52735e6e Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents: 22112
diff changeset
1419 /* The Unicode to string conversion is done here because the
a0cc52735e6e Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents: 22112
diff changeset
1420 existing tp_setattro slots expect a string object as name
a0cc52735e6e Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents: 22112
diff changeset
1421 and we wouldn't want to break those. */
a0cc52735e6e Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents: 22112
diff changeset
1422 if (PyUnicode_Check(name)) {
a0cc52735e6e Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents: 22112
diff changeset
1423 name = PyUnicode_AsEncodedString(name, NULL, NULL);
a0cc52735e6e Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents: 22112
diff changeset
1424 if (name == NULL)
a0cc52735e6e Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents: 22112
diff changeset
1425 return -1;
a0cc52735e6e Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents: 22112
diff changeset
1426 }
24122
d332484e394a Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents: 24121
diff changeset
1427 else
22302
a0cc52735e6e Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents: 22112
diff changeset
1428 #endif
a0cc52735e6e Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents: 22112
diff changeset
1429 {
a0cc52735e6e Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents: 22112
diff changeset
1430 PyErr_SetString(PyExc_TypeError,
a0cc52735e6e Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents: 22112
diff changeset
1431 "attribute name must be string");
21186
0189186f4ed9 PyObject_Generic{Get,Set}Attr(): ensure that the attribute name is a
Guido van Rossum <guido@python.org>
parents: 20865
diff changeset
1432 return -1;
22302
a0cc52735e6e Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents: 22112
diff changeset
1433 }
21186
0189186f4ed9 PyObject_Generic{Get,Set}Attr(): ensure that the attribute name is a
Guido van Rossum <guido@python.org>
parents: 20865
diff changeset
1434 }
0189186f4ed9 PyObject_Generic{Get,Set}Attr(): ensure that the attribute name is a
Guido van Rossum <guido@python.org>
parents: 20865
diff changeset
1435 else
0189186f4ed9 PyObject_Generic{Get,Set}Attr(): ensure that the attribute name is a
Guido van Rossum <guido@python.org>
parents: 20865
diff changeset
1436 Py_INCREF(name);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1437
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1438 if (tp->tp_dict == NULL) {
19040
4df72ea78ae8 - Rename PyType_InitDict() to PyType_Ready().
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1439 if (PyType_Ready(tp) < 0)
21186
0189186f4ed9 PyObject_Generic{Get,Set}Attr(): ensure that the attribute name is a
Guido van Rossum <guido@python.org>
parents: 20865
diff changeset
1440 goto done;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1441 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1442
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1443 descr = _PyType_Lookup(tp, name);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1444 f = NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1445 if (descr != NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1446 f = descr->ob_type->tp_descr_set;
21186
0189186f4ed9 PyObject_Generic{Get,Set}Attr(): ensure that the attribute name is a
Guido van Rossum <guido@python.org>
parents: 20865
diff changeset
1447 if (f != NULL && PyDescr_IsData(descr)) {
0189186f4ed9 PyObject_Generic{Get,Set}Attr(): ensure that the attribute name is a
Guido van Rossum <guido@python.org>
parents: 20865
diff changeset
1448 res = f(descr, obj, value);
0189186f4ed9 PyObject_Generic{Get,Set}Attr(): ensure that the attribute name is a
Guido van Rossum <guido@python.org>
parents: 20865
diff changeset
1449 goto done;
0189186f4ed9 PyObject_Generic{Get,Set}Attr(): ensure that the attribute name is a
Guido van Rossum <guido@python.org>
parents: 20865
diff changeset
1450 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1451 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1452
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1453 dictptr = _PyObject_GetDictPtr(obj);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1454 if (dictptr != NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1455 PyObject *dict = *dictptr;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1456 if (dict == NULL && value != NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1457 dict = PyDict_New();
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1458 if (dict == NULL)
21186
0189186f4ed9 PyObject_Generic{Get,Set}Attr(): ensure that the attribute name is a
Guido van Rossum <guido@python.org>
parents: 20865
diff changeset
1459 goto done;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1460 *dictptr = dict;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1461 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1462 if (dict != NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1463 if (value == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1464 res = PyDict_DelItem(dict, name);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1465 else
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1466 res = PyDict_SetItem(dict, name, value);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1467 if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError))
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1468 PyErr_SetObject(PyExc_AttributeError, name);
21186
0189186f4ed9 PyObject_Generic{Get,Set}Attr(): ensure that the attribute name is a
Guido van Rossum <guido@python.org>
parents: 20865
diff changeset
1469 goto done;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1470 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1471 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1472
21186
0189186f4ed9 PyObject_Generic{Get,Set}Attr(): ensure that the attribute name is a
Guido van Rossum <guido@python.org>
parents: 20865
diff changeset
1473 if (f != NULL) {
0189186f4ed9 PyObject_Generic{Get,Set}Attr(): ensure that the attribute name is a
Guido van Rossum <guido@python.org>
parents: 20865
diff changeset
1474 res = f(descr, obj, value);
0189186f4ed9 PyObject_Generic{Get,Set}Attr(): ensure that the attribute name is a
Guido van Rossum <guido@python.org>
parents: 20865
diff changeset
1475 goto done;
0189186f4ed9 PyObject_Generic{Get,Set}Attr(): ensure that the attribute name is a
Guido van Rossum <guido@python.org>
parents: 20865
diff changeset
1476 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1477
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1478 if (descr == NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1479 PyErr_Format(PyExc_AttributeError,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1480 "'%.50s' object has no attribute '%.400s'",
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1481 tp->tp_name, PyString_AS_STRING(name));
21186
0189186f4ed9 PyObject_Generic{Get,Set}Attr(): ensure that the attribute name is a
Guido van Rossum <guido@python.org>
parents: 20865
diff changeset
1482 goto done;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1483 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1484
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1485 PyErr_Format(PyExc_AttributeError,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1486 "'%.50s' object attribute '%.400s' is read-only",
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1487 tp->tp_name, PyString_AS_STRING(name));
21186
0189186f4ed9 PyObject_Generic{Get,Set}Attr(): ensure that the attribute name is a
Guido van Rossum <guido@python.org>
parents: 20865
diff changeset
1488 done:
0189186f4ed9 PyObject_Generic{Get,Set}Attr(): ensure that the attribute name is a
Guido van Rossum <guido@python.org>
parents: 20865
diff changeset
1489 Py_DECREF(name);
0189186f4ed9 PyObject_Generic{Get,Set}Attr(): ensure that the attribute name is a
Guido van Rossum <guido@python.org>
parents: 20865
diff changeset
1490 return res;
5560
8ec298b255a2 Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents: 5535
diff changeset
1491 }
8ec298b255a2 Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents: 5535
diff changeset
1492
1282
e0464e7115ca * pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1493 /* 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
1494 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
1495
e0464e7115ca * pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1496 int
13473
31b1f691a7d9 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1497 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
1498 {
e0464e7115ca * pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1499 int res;
25015
24e80d96bd0b Speedup for PyObject_IsTrue(): check for True and False first.
Guido van Rossum <guido@python.org>
parents: 25013
diff changeset
1500 if (v == Py_True)
24e80d96bd0b Speedup for PyObject_IsTrue(): check for True and False first.
Guido van Rossum <guido@python.org>
parents: 25013
diff changeset
1501 return 1;
24e80d96bd0b Speedup for PyObject_IsTrue(): check for True and False first.
Guido van Rossum <guido@python.org>
parents: 25013
diff changeset
1502 if (v == Py_False)
24e80d96bd0b Speedup for PyObject_IsTrue(): check for True and False first.
Guido van Rossum <guido@python.org>
parents: 25013
diff changeset
1503 return 0;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5006
diff changeset
1504 if (v == Py_None)
23838
95263ccec819 SF # 561244 Micro optimizations
Neal Norwitz <nnorwitz@gmail.com>
parents: 23599
diff changeset
1505 return 0;
8404
64b88a178328 In PyObject_IsTrue(), don't call function pointers that are NULL
Guido van Rossum <guido@python.org>
parents: 8181
diff changeset
1506 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
1507 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
1508 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
1509 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
1510 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
1511 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
1512 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
1513 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
1514 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
1515 else
23838
95263ccec819 SF # 561244 Micro optimizations
Neal Norwitz <nnorwitz@gmail.com>
parents: 23599
diff changeset
1516 return 1;
95263ccec819 SF # 561244 Micro optimizations
Neal Norwitz <nnorwitz@gmail.com>
parents: 23599
diff changeset
1517 return (res > 0) ? 1 : res;
1282
e0464e7115ca * pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1518 }
e0464e7115ca * pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1519
24122
d332484e394a Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents: 24121
diff changeset
1520 /* equivalent of 'not v'
7935
aa7d69d67c81 Add PyObject_Not().
Guido van Rossum <guido@python.org>
parents: 7119
diff changeset
1521 Return -1 if an error occurred */
aa7d69d67c81 Add PyObject_Not().
Guido van Rossum <guido@python.org>
parents: 7119
diff changeset
1522
aa7d69d67c81 Add PyObject_Not().
Guido van Rossum <guido@python.org>
parents: 7119
diff changeset
1523 int
13473
31b1f691a7d9 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1524 PyObject_Not(PyObject *v)
7935
aa7d69d67c81 Add PyObject_Not().
Guido van Rossum <guido@python.org>
parents: 7119
diff changeset
1525 {
aa7d69d67c81 Add PyObject_Not().
Guido van Rossum <guido@python.org>
parents: 7119
diff changeset
1526 int res;
aa7d69d67c81 Add PyObject_Not().
Guido van Rossum <guido@python.org>
parents: 7119
diff changeset
1527 res = PyObject_IsTrue(v);
aa7d69d67c81 Add PyObject_Not().
Guido van Rossum <guido@python.org>
parents: 7119
diff changeset
1528 if (res < 0)
aa7d69d67c81 Add PyObject_Not().
Guido van Rossum <guido@python.org>
parents: 7119
diff changeset
1529 return res;
aa7d69d67c81 Add PyObject_Not().
Guido van Rossum <guido@python.org>
parents: 7119
diff changeset
1530 return res == 0;
aa7d69d67c81 Add PyObject_Not().
Guido van Rossum <guido@python.org>
parents: 7119
diff changeset
1531 }
aa7d69d67c81 Add PyObject_Not().
Guido van Rossum <guido@python.org>
parents: 7119
diff changeset
1532
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
1533 /* 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
1534 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
1535 Return value:
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
1536 -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
1537 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
1538 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
1539 */
76c860a18d82 move coerce() from bltinmodule.c to object.c and implement builtin_coerce() differently
Guido van Rossum <guido@python.org>
parents: 2079
diff changeset
1540 int
13473
31b1f691a7d9 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1541 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
1542 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5006
diff changeset
1543 register PyObject *v = *pv;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5006
diff changeset
1544 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
1545 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
1546
23282
a4e75d53d32b PyNumber_CoerceEx: this took a shortcut (not doing anything) when the
Guido van Rossum <guido@python.org>
parents: 22985
diff changeset
1547 /* Shortcut only for old-style types */
a4e75d53d32b PyNumber_CoerceEx: this took a shortcut (not doing anything) when the
Guido van Rossum <guido@python.org>
parents: 22985
diff changeset
1548 if (v->ob_type == w->ob_type &&
a4e75d53d32b PyNumber_CoerceEx: this took a shortcut (not doing anything) when the
Guido van Rossum <guido@python.org>
parents: 22985
diff changeset
1549 !PyType_HasFeature(v->ob_type, Py_TPFLAGS_CHECKTYPES))
a4e75d53d32b PyNumber_CoerceEx: this took a shortcut (not doing anything) when the
Guido van Rossum <guido@python.org>
parents: 22985
diff changeset
1550 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5006
diff changeset
1551 Py_INCREF(v);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5006
diff changeset
1552 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
1553 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
1554 }
76c860a18d82 move coerce() from bltinmodule.c to object.c and implement builtin_coerce() differently
Guido van Rossum <guido@python.org>
parents: 2079
diff changeset
1555 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
1556 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
1557 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
1558 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
1559 }
76c860a18d82 move coerce() from bltinmodule.c to object.c and implement builtin_coerce() differently
Guido van Rossum <guido@python.org>
parents: 2079
diff changeset
1560 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
1561 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
1562 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
1563 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
1564 }
6597
b88fd3894d5a Add a new function PyNumber_CoerceEx() which works just like
Guido van Rossum <guido@python.org>
parents: 5981
diff changeset
1565 return 1;
b88fd3894d5a Add a new function PyNumber_CoerceEx() which works just like
Guido van Rossum <guido@python.org>
parents: 5981
diff changeset
1566 }
b88fd3894d5a Add a new function PyNumber_CoerceEx() which works just like
Guido van Rossum <guido@python.org>
parents: 5981
diff changeset
1567
15941
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
1568 /* 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
1569 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
1570 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
1571 (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
1572 */
6597
b88fd3894d5a Add a new function PyNumber_CoerceEx() which works just like
Guido van Rossum <guido@python.org>
parents: 5981
diff changeset
1573 int
13473
31b1f691a7d9 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1574 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
1575 {
b88fd3894d5a Add a new function PyNumber_CoerceEx() which works just like
Guido van Rossum <guido@python.org>
parents: 5981
diff changeset
1576 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
1577 if (err <= 0)
b88fd3894d5a Add a new function PyNumber_CoerceEx() which works just like
Guido van Rossum <guido@python.org>
parents: 5981
diff changeset
1578 return err;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5006
diff changeset
1579 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
1580 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
1581 }
76c860a18d82 move coerce() from bltinmodule.c to object.c and implement builtin_coerce() differently
Guido van Rossum <guido@python.org>
parents: 2079
diff changeset
1582
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1583
2237
0b0b18d655d2 move callable() here
Guido van Rossum <guido@python.org>
parents: 2160
diff changeset
1584 /* Test whether an object can be called */
0b0b18d655d2 move callable() here
Guido van Rossum <guido@python.org>
parents: 2160
diff changeset
1585
0b0b18d655d2 move callable() here
Guido van Rossum <guido@python.org>
parents: 2160
diff changeset
1586 int
13473
31b1f691a7d9 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1587 PyCallable_Check(PyObject *x)
2237
0b0b18d655d2 move callable() here
Guido van Rossum <guido@python.org>
parents: 2160
diff changeset
1588 {
0b0b18d655d2 move callable() here
Guido van Rossum <guido@python.org>
parents: 2160
diff changeset
1589 if (x == NULL)
0b0b18d655d2 move callable() here
Guido van Rossum <guido@python.org>
parents: 2160
diff changeset
1590 return 0;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5006
diff changeset
1591 if (PyInstance_Check(x)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5006
diff changeset
1592 PyObject *call = PyObject_GetAttrString(x, "__call__");
2237
0b0b18d655d2 move callable() here
Guido van Rossum <guido@python.org>
parents: 2160
diff changeset
1593 if (call == NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5006
diff changeset
1594 PyErr_Clear();
2237
0b0b18d655d2 move callable() here
Guido van Rossum <guido@python.org>
parents: 2160
diff changeset
1595 return 0;
0b0b18d655d2 move callable() here
Guido van Rossum <guido@python.org>
parents: 2160
diff changeset
1596 }
0b0b18d655d2 move callable() here
Guido van Rossum <guido@python.org>
parents: 2160
diff changeset
1597 /* Could test recursively but don't, for fear of endless
0b0b18d655d2 move callable() here
Guido van Rossum <guido@python.org>
parents: 2160
diff changeset
1598 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
1599 Py_DECREF(call);
2237
0b0b18d655d2 move callable() here
Guido van Rossum <guido@python.org>
parents: 2160
diff changeset
1600 return 1;
0b0b18d655d2 move callable() here
Guido van Rossum <guido@python.org>
parents: 2160
diff changeset
1601 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1602 else {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1603 return x->ob_type->tp_call != NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1604 }
2237
0b0b18d655d2 move callable() here
Guido van Rossum <guido@python.org>
parents: 2160
diff changeset
1605 }
0b0b18d655d2 move callable() here
Guido van Rossum <guido@python.org>
parents: 2160
diff changeset
1606
19705
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1607 /* Helper for PyObject_Dir.
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1608 Merge the __dict__ of aclass into dict, and recursively also all
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1609 the __dict__s of aclass's base classes. The order of merging isn't
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1610 defined, as it's expected that only the final set of dict keys is
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1611 interesting.
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1612 Return 0 on success, -1 on error.
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1613 */
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1614
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1615 static int
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1616 merge_class_dict(PyObject* dict, PyObject* aclass)
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1617 {
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1618 PyObject *classdict;
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1619 PyObject *bases;
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1620
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1621 assert(PyDict_Check(dict));
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1622 assert(aclass);
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1623
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1624 /* Merge in the type's dict (if any). */
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1625 classdict = PyObject_GetAttrString(aclass, "__dict__");
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1626 if (classdict == NULL)
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1627 PyErr_Clear();
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1628 else {
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1629 int status = PyDict_Update(dict, classdict);
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1630 Py_DECREF(classdict);
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1631 if (status < 0)
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1632 return -1;
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1633 }
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1634
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1635 /* Recursively merge in the base types' (if any) dicts. */
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1636 bases = PyObject_GetAttrString(aclass, "__bases__");
19931
c327a8ac9f2b merge_class_dict(): Clear the error if __bases__ doesn't exist.
Tim Peters <tim.peters@gmail.com>
parents: 19906
diff changeset
1637 if (bases == NULL)
c327a8ac9f2b merge_class_dict(): Clear the error if __bases__ doesn't exist.
Tim Peters <tim.peters@gmail.com>
parents: 19906
diff changeset
1638 PyErr_Clear();
c327a8ac9f2b merge_class_dict(): Clear the error if __bases__ doesn't exist.
Tim Peters <tim.peters@gmail.com>
parents: 19906
diff changeset
1639 else {
23436
f9d5e1fb2aa6 Jim Fulton reported a segfault in dir(). A heavily proxied object
Guido van Rossum <guido@python.org>
parents: 23282
diff changeset
1640 /* We have no guarantee that bases is a real tuple */
19705
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1641 int i, n;
23436
f9d5e1fb2aa6 Jim Fulton reported a segfault in dir(). A heavily proxied object
Guido van Rossum <guido@python.org>
parents: 23282
diff changeset
1642 n = PySequence_Size(bases); /* This better be right */
f9d5e1fb2aa6 Jim Fulton reported a segfault in dir(). A heavily proxied object
Guido van Rossum <guido@python.org>
parents: 23282
diff changeset
1643 if (n < 0)
f9d5e1fb2aa6 Jim Fulton reported a segfault in dir(). A heavily proxied object
Guido van Rossum <guido@python.org>
parents: 23282
diff changeset
1644 PyErr_Clear();
f9d5e1fb2aa6 Jim Fulton reported a segfault in dir(). A heavily proxied object
Guido van Rossum <guido@python.org>
parents: 23282
diff changeset
1645 else {
f9d5e1fb2aa6 Jim Fulton reported a segfault in dir(). A heavily proxied object
Guido van Rossum <guido@python.org>
parents: 23282
diff changeset
1646 for (i = 0; i < n; i++) {
f9d5e1fb2aa6 Jim Fulton reported a segfault in dir(). A heavily proxied object
Guido van Rossum <guido@python.org>
parents: 23282
diff changeset
1647 PyObject *base = PySequence_GetItem(bases, i);
f9d5e1fb2aa6 Jim Fulton reported a segfault in dir(). A heavily proxied object
Guido van Rossum <guido@python.org>
parents: 23282
diff changeset
1648 if (base == NULL) {
f9d5e1fb2aa6 Jim Fulton reported a segfault in dir(). A heavily proxied object
Guido van Rossum <guido@python.org>
parents: 23282
diff changeset
1649 Py_DECREF(bases);
f9d5e1fb2aa6 Jim Fulton reported a segfault in dir(). A heavily proxied object
Guido van Rossum <guido@python.org>
parents: 23282
diff changeset
1650 return -1;
f9d5e1fb2aa6 Jim Fulton reported a segfault in dir(). A heavily proxied object
Guido van Rossum <guido@python.org>
parents: 23282
diff changeset
1651 }
f9d5e1fb2aa6 Jim Fulton reported a segfault in dir(). A heavily proxied object
Guido van Rossum <guido@python.org>
parents: 23282
diff changeset
1652 if (merge_class_dict(dict, base) < 0) {
f9d5e1fb2aa6 Jim Fulton reported a segfault in dir(). A heavily proxied object
Guido van Rossum <guido@python.org>
parents: 23282
diff changeset
1653 Py_DECREF(bases);
f9d5e1fb2aa6 Jim Fulton reported a segfault in dir(). A heavily proxied object
Guido van Rossum <guido@python.org>
parents: 23282
diff changeset
1654 return -1;
f9d5e1fb2aa6 Jim Fulton reported a segfault in dir(). A heavily proxied object
Guido van Rossum <guido@python.org>
parents: 23282
diff changeset
1655 }
19705
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1656 }
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1657 }
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1658 Py_DECREF(bases);
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1659 }
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1660 return 0;
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1661 }
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1662
19932
621ed13445de PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
Tim Peters <tim.peters@gmail.com>
parents: 19931
diff changeset
1663 /* Helper for PyObject_Dir.
621ed13445de PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
Tim Peters <tim.peters@gmail.com>
parents: 19931
diff changeset
1664 If obj has an attr named attrname that's a list, merge its string
621ed13445de PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
Tim Peters <tim.peters@gmail.com>
parents: 19931
diff changeset
1665 elements into keys of dict.
621ed13445de PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
Tim Peters <tim.peters@gmail.com>
parents: 19931
diff changeset
1666 Return 0 on success, -1 on error. Errors due to not finding the attr,
621ed13445de PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
Tim Peters <tim.peters@gmail.com>
parents: 19931
diff changeset
1667 or the attr not being a list, are suppressed.
621ed13445de PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
Tim Peters <tim.peters@gmail.com>
parents: 19931
diff changeset
1668 */
621ed13445de PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
Tim Peters <tim.peters@gmail.com>
parents: 19931
diff changeset
1669
621ed13445de PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
Tim Peters <tim.peters@gmail.com>
parents: 19931
diff changeset
1670 static int
621ed13445de PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
Tim Peters <tim.peters@gmail.com>
parents: 19931
diff changeset
1671 merge_list_attr(PyObject* dict, PyObject* obj, char *attrname)
621ed13445de PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
Tim Peters <tim.peters@gmail.com>
parents: 19931
diff changeset
1672 {
621ed13445de PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
Tim Peters <tim.peters@gmail.com>
parents: 19931
diff changeset
1673 PyObject *list;
621ed13445de PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
Tim Peters <tim.peters@gmail.com>
parents: 19931
diff changeset
1674 int result = 0;
621ed13445de PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
Tim Peters <tim.peters@gmail.com>
parents: 19931
diff changeset
1675
621ed13445de PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
Tim Peters <tim.peters@gmail.com>
parents: 19931
diff changeset
1676 assert(PyDict_Check(dict));
621ed13445de PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
Tim Peters <tim.peters@gmail.com>
parents: 19931
diff changeset
1677 assert(obj);
621ed13445de PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
Tim Peters <tim.peters@gmail.com>
parents: 19931
diff changeset
1678 assert(attrname);
621ed13445de PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
Tim Peters <tim.peters@gmail.com>
parents: 19931
diff changeset
1679
621ed13445de PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
Tim Peters <tim.peters@gmail.com>
parents: 19931
diff changeset
1680 list = PyObject_GetAttrString(obj, attrname);
621ed13445de PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
Tim Peters <tim.peters@gmail.com>
parents: 19931
diff changeset
1681 if (list == NULL)
621ed13445de PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
Tim Peters <tim.peters@gmail.com>
parents: 19931
diff changeset
1682 PyErr_Clear();
621ed13445de PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
Tim Peters <tim.peters@gmail.com>
parents: 19931
diff changeset
1683
621ed13445de PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
Tim Peters <tim.peters@gmail.com>
parents: 19931
diff changeset
1684 else if (PyList_Check(list)) {
621ed13445de PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
Tim Peters <tim.peters@gmail.com>
parents: 19931
diff changeset
1685 int i;
621ed13445de PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
Tim Peters <tim.peters@gmail.com>
parents: 19931
diff changeset
1686 for (i = 0; i < PyList_GET_SIZE(list); ++i) {
621ed13445de PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
Tim Peters <tim.peters@gmail.com>
parents: 19931
diff changeset
1687 PyObject *item = PyList_GET_ITEM(list, i);
621ed13445de PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
Tim Peters <tim.peters@gmail.com>
parents: 19931
diff changeset
1688 if (PyString_Check(item)) {
621ed13445de PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
Tim Peters <tim.peters@gmail.com>
parents: 19931
diff changeset
1689 result = PyDict_SetItem(dict, item, Py_None);
621ed13445de PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
Tim Peters <tim.peters@gmail.com>
parents: 19931
diff changeset
1690 if (result < 0)
621ed13445de PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
Tim Peters <tim.peters@gmail.com>
parents: 19931
diff changeset
1691 break;
621ed13445de PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
Tim Peters <tim.peters@gmail.com>
parents: 19931
diff changeset
1692 }
621ed13445de PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
Tim Peters <tim.peters@gmail.com>
parents: 19931
diff changeset
1693 }
621ed13445de PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
Tim Peters <tim.peters@gmail.com>
parents: 19931
diff changeset
1694 }
621ed13445de PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
Tim Peters <tim.peters@gmail.com>
parents: 19931
diff changeset
1695
621ed13445de PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
Tim Peters <tim.peters@gmail.com>
parents: 19931
diff changeset
1696 Py_XDECREF(list);
621ed13445de PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
Tim Peters <tim.peters@gmail.com>
parents: 19931
diff changeset
1697 return result;
621ed13445de PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
Tim Peters <tim.peters@gmail.com>
parents: 19931
diff changeset
1698 }
621ed13445de PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
Tim Peters <tim.peters@gmail.com>
parents: 19931
diff changeset
1699
19705
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1700 /* Like __builtin__.dir(arg). See bltinmodule.c's builtin_dir for the
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1701 docstring, which should be kept in synch with this implementation. */
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1702
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1703 PyObject *
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1704 PyObject_Dir(PyObject *arg)
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1705 {
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1706 /* Set exactly one of these non-NULL before the end. */
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1707 PyObject *result = NULL; /* result list */
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1708 PyObject *masterdict = NULL; /* result is masterdict.keys() */
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1709
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1710 /* If NULL arg, return the locals. */
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1711 if (arg == NULL) {
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1712 PyObject *locals = PyEval_GetLocals();
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1713 if (locals == NULL)
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1714 goto error;
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1715 result = PyDict_Keys(locals);
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1716 if (result == NULL)
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1717 goto error;
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1718 }
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1719
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1720 /* Elif this is some form of module, we only want its dict. */
19833
f44606b242c5 PyObject_Dir():
Guido van Rossum <guido@python.org>
parents: 19705
diff changeset
1721 else if (PyModule_Check(arg)) {
19705
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1722 masterdict = PyObject_GetAttrString(arg, "__dict__");
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1723 if (masterdict == NULL)
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1724 goto error;
19833
f44606b242c5 PyObject_Dir():
Guido van Rossum <guido@python.org>
parents: 19705
diff changeset
1725 if (!PyDict_Check(masterdict)) {
f44606b242c5 PyObject_Dir():
Guido van Rossum <guido@python.org>
parents: 19705
diff changeset
1726 PyErr_SetString(PyExc_TypeError,
f44606b242c5 PyObject_Dir():
Guido van Rossum <guido@python.org>
parents: 19705
diff changeset
1727 "module.__dict__ is not a dictionary");
f44606b242c5 PyObject_Dir():
Guido van Rossum <guido@python.org>
parents: 19705
diff changeset
1728 goto error;
f44606b242c5 PyObject_Dir():
Guido van Rossum <guido@python.org>
parents: 19705
diff changeset
1729 }
19705
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1730 }
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1731
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1732 /* Elif some form of type or class, grab its dict and its bases.
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1733 We deliberately don't suck up its __class__, as methods belonging
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1734 to the metaclass would probably be more confusing than helpful. */
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1735 else if (PyType_Check(arg) || PyClass_Check(arg)) {
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1736 masterdict = PyDict_New();
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1737 if (masterdict == NULL)
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1738 goto error;
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1739 if (merge_class_dict(masterdict, arg) < 0)
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1740 goto error;
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1741 }
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1742
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1743 /* Else look at its dict, and the attrs reachable from its class. */
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1744 else {
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1745 PyObject *itsclass;
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1746 /* Create a dict to start with. CAUTION: Not everything
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1747 responding to __dict__ returns a dict! */
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1748 masterdict = PyObject_GetAttrString(arg, "__dict__");
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1749 if (masterdict == NULL) {
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1750 PyErr_Clear();
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1751 masterdict = PyDict_New();
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1752 }
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1753 else if (!PyDict_Check(masterdict)) {
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1754 Py_DECREF(masterdict);
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1755 masterdict = PyDict_New();
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1756 }
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1757 else {
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1758 /* The object may have returned a reference to its
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1759 dict, so copy it to avoid mutating it. */
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1760 PyObject *temp = PyDict_Copy(masterdict);
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1761 Py_DECREF(masterdict);
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1762 masterdict = temp;
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1763 }
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1764 if (masterdict == NULL)
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1765 goto error;
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1766
19932
621ed13445de PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
Tim Peters <tim.peters@gmail.com>
parents: 19931
diff changeset
1767 /* Merge in __members__ and __methods__ (if any).
621ed13445de PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
Tim Peters <tim.peters@gmail.com>
parents: 19931
diff changeset
1768 XXX Would like this to go away someday; for now, it's
621ed13445de PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
Tim Peters <tim.peters@gmail.com>
parents: 19931
diff changeset
1769 XXX needed to get at im_self etc of method objects. */
621ed13445de PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
Tim Peters <tim.peters@gmail.com>
parents: 19931
diff changeset
1770 if (merge_list_attr(masterdict, arg, "__members__") < 0)
621ed13445de PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
Tim Peters <tim.peters@gmail.com>
parents: 19931
diff changeset
1771 goto error;
621ed13445de PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
Tim Peters <tim.peters@gmail.com>
parents: 19931
diff changeset
1772 if (merge_list_attr(masterdict, arg, "__methods__") < 0)
621ed13445de PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
Tim Peters <tim.peters@gmail.com>
parents: 19931
diff changeset
1773 goto error;
621ed13445de PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
Tim Peters <tim.peters@gmail.com>
parents: 19931
diff changeset
1774
19705
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1775 /* Merge in attrs reachable from its class.
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1776 CAUTION: Not all objects have a __class__ attr. */
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1777 itsclass = PyObject_GetAttrString(arg, "__class__");
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1778 if (itsclass == NULL)
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1779 PyErr_Clear();
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1780 else {
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1781 int status = merge_class_dict(masterdict, itsclass);
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1782 Py_DECREF(itsclass);
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1783 if (status < 0)
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1784 goto error;
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1785 }
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1786 }
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1787
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1788 assert((result == NULL) ^ (masterdict == NULL));
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1789 if (masterdict != NULL) {
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1790 /* The result comes from its keys. */
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1791 assert(result == NULL);
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1792 result = PyDict_Keys(masterdict);
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1793 if (result == NULL)
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1794 goto error;
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1795 }
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1796
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1797 assert(result);
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1798 if (PyList_Sort(result) != 0)
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1799 goto error;
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1800 else
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1801 goto normal_return;
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1802
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1803 error:
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1804 Py_XDECREF(result);
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1805 result = NULL;
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1806 /* fall through */
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1807 normal_return:
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1808 Py_XDECREF(masterdict);
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1809 return result;
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1810 }
2237
0b0b18d655d2 move callable() here
Guido van Rossum <guido@python.org>
parents: 2160
diff changeset
1811
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1812 /*
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1813 NoObject is usable as a non-NULL undefined value, used by the macro None.
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1814 There is (and should be!) no way to create other objects of this type,
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
1815 so there is exactly one (which is indestructible, by the way).
19294
1456ec735087 Add a function _Py_ReadyTypes() which initializes various and sundry
Guido van Rossum <guido@python.org>
parents: 19293
diff changeset
1816 (XXX This type and the type of NotImplemented below should be unified.)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1817 */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1818
739
ab1b3486b226 Fix lint bug
Guido van Rossum <guido@python.org>
parents: 512
diff changeset
1819 /* ARGSUSED */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5006
diff changeset
1820 static PyObject *
13473
31b1f691a7d9 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1821 none_repr(PyObject *op)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
1822 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5006
diff changeset
1823 return PyString_FromString("None");
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1824 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1825
16165
0a72070eab6e A few miscellaneous helpers.
Barry Warsaw <barry@python.org>
parents: 16123
diff changeset
1826 /* ARGUSED */
0a72070eab6e A few miscellaneous helpers.
Barry Warsaw <barry@python.org>
parents: 16123
diff changeset
1827 static void
24122
d332484e394a Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents: 24121
diff changeset
1828 none_dealloc(PyObject* ignore)
16165
0a72070eab6e A few miscellaneous helpers.
Barry Warsaw <barry@python.org>
parents: 16123
diff changeset
1829 {
0a72070eab6e A few miscellaneous helpers.
Barry Warsaw <barry@python.org>
parents: 16123
diff changeset
1830 /* 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
1831 * we accidently decref None out of existance.
0a72070eab6e A few miscellaneous helpers.
Barry Warsaw <barry@python.org>
parents: 16123
diff changeset
1832 */
24644
7ba3e5fbaad8 Replace abort with Py_FatalError.
Martin v. Löwis <martin@v.loewis.de>
parents: 24420
diff changeset
1833 Py_FatalError("deallocating None");
16165
0a72070eab6e A few miscellaneous helpers.
Barry Warsaw <barry@python.org>
parents: 16123
diff changeset
1834 }
0a72070eab6e A few miscellaneous helpers.
Barry Warsaw <barry@python.org>
parents: 16123
diff changeset
1835
0a72070eab6e A few miscellaneous helpers.
Barry Warsaw <barry@python.org>
parents: 16123
diff changeset
1836
19294
1456ec735087 Add a function _Py_ReadyTypes() which initializes various and sundry
Guido van Rossum <guido@python.org>
parents: 19293
diff changeset
1837 static PyTypeObject PyNone_Type = {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5006
diff changeset
1838 PyObject_HEAD_INIT(&PyType_Type)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1839 0,
19294
1456ec735087 Add a function _Py_ReadyTypes() which initializes various and sundry
Guido van Rossum <guido@python.org>
parents: 19293
diff changeset
1840 "NoneType",
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1841 0,
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1842 0,
16165
0a72070eab6e A few miscellaneous helpers.
Barry Warsaw <barry@python.org>
parents: 16123
diff changeset
1843 (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
1844 0, /*tp_print*/
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
1845 0, /*tp_getattr*/
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
1846 0, /*tp_setattr*/
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
1847 0, /*tp_compare*/
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1400
diff changeset
1848 (reprfunc)none_repr, /*tp_repr*/
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
1849 0, /*tp_as_number*/
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
1850 0, /*tp_as_sequence*/
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
1851 0, /*tp_as_mapping*/
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
1852 0, /*tp_hash */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1853 };
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1854
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5006
diff changeset
1855 PyObject _Py_NoneStruct = {
19294
1456ec735087 Add a function _Py_ReadyTypes() which initializes various and sundry
Guido van Rossum <guido@python.org>
parents: 19293
diff changeset
1856 PyObject_HEAD_INIT(&PyNone_Type)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1857 };
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1858
15726
db9f1893be01 Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1859 /* 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
1860 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
1861
db9f1893be01 Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1862 static PyObject *
db9f1893be01 Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1863 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
1864 {
db9f1893be01 Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1865 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
1866 }
db9f1893be01 Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1867
db9f1893be01 Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1868 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
1869 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
1870 0,
19294
1456ec735087 Add a function _Py_ReadyTypes() which initializes various and sundry
Guido van Rossum <guido@python.org>
parents: 19293
diff changeset
1871 "NotImplementedType",
15726
db9f1893be01 Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1872 0,
db9f1893be01 Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1873 0,
16165
0a72070eab6e A few miscellaneous helpers.
Barry Warsaw <barry@python.org>
parents: 16123
diff changeset
1874 (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
1875 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
1876 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
1877 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
1878 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
1879 (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
1880 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
1881 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
1882 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
1883 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
1884 };
db9f1893be01 Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1885
db9f1893be01 Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1886 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
1887 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
1888 };
db9f1893be01 Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1889
19294
1456ec735087 Add a function _Py_ReadyTypes() which initializes various and sundry
Guido van Rossum <guido@python.org>
parents: 19293
diff changeset
1890 void
1456ec735087 Add a function _Py_ReadyTypes() which initializes various and sundry
Guido van Rossum <guido@python.org>
parents: 19293
diff changeset
1891 _Py_ReadyTypes(void)
1456ec735087 Add a function _Py_ReadyTypes() which initializes various and sundry
Guido van Rossum <guido@python.org>
parents: 19293
diff changeset
1892 {
1456ec735087 Add a function _Py_ReadyTypes() which initializes various and sundry
Guido van Rossum <guido@python.org>
parents: 19293
diff changeset
1893 if (PyType_Ready(&PyType_Type) < 0)
1456ec735087 Add a function _Py_ReadyTypes() which initializes various and sundry
Guido van Rossum <guido@python.org>
parents: 19293
diff changeset
1894 Py_FatalError("Can't initialize 'type'");
1456ec735087 Add a function _Py_ReadyTypes() which initializes various and sundry
Guido van Rossum <guido@python.org>
parents: 19293
diff changeset
1895
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22649
diff changeset
1896 if (PyType_Ready(&PyBool_Type) < 0)
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22649
diff changeset
1897 Py_FatalError("Can't initialize 'bool'");
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22649
diff changeset
1898
23542
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23436
diff changeset
1899 if (PyType_Ready(&PyString_Type) < 0)
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23436
diff changeset
1900 Py_FatalError("Can't initialize 'str'");
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23436
diff changeset
1901
19294
1456ec735087 Add a function _Py_ReadyTypes() which initializes various and sundry
Guido van Rossum <guido@python.org>
parents: 19293
diff changeset
1902 if (PyType_Ready(&PyList_Type) < 0)
1456ec735087 Add a function _Py_ReadyTypes() which initializes various and sundry
Guido van Rossum <guido@python.org>
parents: 19293
diff changeset
1903 Py_FatalError("Can't initialize 'list'");
1456ec735087 Add a function _Py_ReadyTypes() which initializes various and sundry
Guido van Rossum <guido@python.org>
parents: 19293
diff changeset
1904
1456ec735087 Add a function _Py_ReadyTypes() which initializes various and sundry
Guido van Rossum <guido@python.org>
parents: 19293
diff changeset
1905 if (PyType_Ready(&PyNone_Type) < 0)
1456ec735087 Add a function _Py_ReadyTypes() which initializes various and sundry
Guido van Rossum <guido@python.org>
parents: 19293
diff changeset
1906 Py_FatalError("Can't initialize type(None)");
1456ec735087 Add a function _Py_ReadyTypes() which initializes various and sundry
Guido van Rossum <guido@python.org>
parents: 19293
diff changeset
1907
1456ec735087 Add a function _Py_ReadyTypes() which initializes various and sundry
Guido van Rossum <guido@python.org>
parents: 19293
diff changeset
1908 if (PyType_Ready(&PyNotImplemented_Type) < 0)
1456ec735087 Add a function _Py_ReadyTypes() which initializes various and sundry
Guido van Rossum <guido@python.org>
parents: 19293
diff changeset
1909 Py_FatalError("Can't initialize type(NotImplemented)");
1456ec735087 Add a function _Py_ReadyTypes() which initializes various and sundry
Guido van Rossum <guido@python.org>
parents: 19293
diff changeset
1910 }
1456ec735087 Add a function _Py_ReadyTypes() which initializes various and sundry
Guido van Rossum <guido@python.org>
parents: 19293
diff changeset
1911
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1912
3574
1ec154241747 TRACE_REFS -> Py_TRACE_REFS.
Guido van Rossum <guido@python.org>
parents: 3330
diff changeset
1913 #ifdef Py_TRACE_REFS
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1914
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5006
diff changeset
1915 static PyObject refchain = {&refchain, &refchain};
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1916
4002
4241582636ad Define reference count admin debug functions to return void.
Guido van Rossum <guido@python.org>
parents: 3986
diff changeset
1917 void
13473
31b1f691a7d9 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1918 _Py_ResetReferences(void)
5931
12fe5cc1516d Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
1919 {
12fe5cc1516d Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
1920 refchain._ob_prev = refchain._ob_next = &refchain;
12fe5cc1516d Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
1921 _Py_RefTotal = 0;
12fe5cc1516d Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
1922 }
12fe5cc1516d Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
1923
12fe5cc1516d Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
1924 void
13473
31b1f691a7d9 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1925 _Py_NewReference(PyObject *op)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1926 {
24184
c4dcb80f48ea object.h special-build macro minefield: renamed all the new lexical
Tim Peters <tim.peters@gmail.com>
parents: 24155
diff changeset
1927 _Py_INC_REFTOTAL;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1928 op->ob_refcnt = 1;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1929 op->_ob_next = refchain._ob_next;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1930 op->_ob_prev = &refchain;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1931 refchain._ob_next->_ob_prev = op;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1932 refchain._ob_next = op;
24184
c4dcb80f48ea object.h special-build macro minefield: renamed all the new lexical
Tim Peters <tim.peters@gmail.com>
parents: 24155
diff changeset
1933 _Py_INC_TPALLOCS(op);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1934 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1935
4002
4241582636ad Define reference count admin debug functions to return void.
Guido van Rossum <guido@python.org>
parents: 3986
diff changeset
1936 void
13473
31b1f691a7d9 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1937 _Py_ForgetReference(register PyObject *op)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1938 {
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
1939 #ifdef SLOW_UNREF_CHECK
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
1940 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
1941 #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
1942 if (op->ob_refcnt < 0)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5006
diff changeset
1943 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
1944 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
1945 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
1946 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
1947 #ifdef SLOW_UNREF_CHECK
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
1948 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
1949 if (p == op)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
1950 break;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
1951 }
2053
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1869
diff changeset
1952 if (p == &refchain) /* Not found */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5006
diff changeset
1953 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
1954 #endif
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1955 op->_ob_next->_ob_prev = op->_ob_prev;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1956 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
1957 op->_ob_next = op->_ob_prev = NULL;
24184
c4dcb80f48ea object.h special-build macro minefield: renamed all the new lexical
Tim Peters <tim.peters@gmail.com>
parents: 24155
diff changeset
1958 _Py_INC_TPFREES(op);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
1959 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
1960
4002
4241582636ad Define reference count admin debug functions to return void.
Guido van Rossum <guido@python.org>
parents: 3986
diff changeset
1961 void
13473
31b1f691a7d9 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1962 _Py_Dealloc(PyObject *op)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
1963 {
1869
2cede61e9b1b rearranged code in debugging version of DELREF to avoid touching data
Guido van Rossum <guido@python.org>
parents: 1848
diff changeset
1964 destructor dealloc = op->ob_type->tp_dealloc;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5006
diff changeset
1965 _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
1966 (*dealloc)(op);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1967 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1968
4002
4241582636ad Define reference count admin debug functions to return void.
Guido van Rossum <guido@python.org>
parents: 3986
diff changeset
1969 void
13473
31b1f691a7d9 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1970 _Py_PrintReferences(FILE *fp)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1971 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5006
diff changeset
1972 PyObject *op;
5931
12fe5cc1516d Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
1973 fprintf(fp, "Remaining objects:\n");
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1974 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1975 fprintf(fp, "[%d] ", op->ob_refcnt);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5006
diff changeset
1976 if (PyObject_Print(op, fp, 0) != 0)
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5006
diff changeset
1977 PyErr_Clear();
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1978 putc('\n', fp);
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1979 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1980 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1981
3024
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
1982 PyObject *
13473
31b1f691a7d9 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1983 _Py_GetObjects(PyObject *self, PyObject *args)
3024
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
1984 {
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
1985 int i, n;
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
1986 PyObject *t = NULL;
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
1987 PyObject *res, *op;
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
1988
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
1989 if (!PyArg_ParseTuple(args, "i|O", &n, &t))
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
1990 return NULL;
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
1991 op = refchain._ob_next;
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
1992 res = PyList_New(0);
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
1993 if (res == NULL)
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
1994 return NULL;
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
1995 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
1996 while (op == self || op == args || op == res || op == t ||
18600
859505224850 _Py_GetObjects(): GCC suggests to add () around && within || for some
Guido van Rossum <guido@python.org>
parents: 18063
diff changeset
1997 (t != NULL && op->ob_type != (PyTypeObject *) t)) {
3024
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
1998 op = op->_ob_next;
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
1999 if (op == &refchain)
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
2000 return res;
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
2001 }
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
2002 if (PyList_Append(res, op) < 0) {
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
2003 Py_DECREF(res);
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
2004 return NULL;
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
2005 }
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
2006 op = op->_ob_next;
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
2007 }
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
2008 return res;
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
2009 }
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
2010
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2011 #endif
3330
9f1b6e2db8e0 Hack to force loading of cobject.o
Guido van Rossum <guido@python.org>
parents: 3024
diff changeset
2012
9f1b6e2db8e0 Hack to force loading of cobject.o
Guido van Rossum <guido@python.org>
parents: 3024
diff changeset
2013
9f1b6e2db8e0 Hack to force loading of cobject.o
Guido van Rossum <guido@python.org>
parents: 3024
diff changeset
2014 /* Hack to force loading of cobject.o */
4588
4dc67fdd9e0b Make gcc -Wall happy
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
2015 PyTypeObject *_Py_cobject_hack = &PyCObject_Type;
3574
1ec154241747 TRACE_REFS -> Py_TRACE_REFS.
Guido van Rossum <guido@python.org>
parents: 3330
diff changeset
2016
1ec154241747 TRACE_REFS -> Py_TRACE_REFS.
Guido van Rossum <guido@python.org>
parents: 3330
diff changeset
2017
1ec154241747 TRACE_REFS -> Py_TRACE_REFS.
Guido van Rossum <guido@python.org>
parents: 3330
diff changeset
2018 /* Hack to force loading of abstract.o */
23843
85a4e727b16b SF # 533070 Silence AIX C Compiler Warnings
Neal Norwitz <nnorwitz@gmail.com>
parents: 23838
diff changeset
2019 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
2020
12fe5cc1516d Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
2021
14006
ed369f0aa32b Updated comment
Andrew M. Kuchling <amk@amk.ca>
parents: 13976
diff changeset
2022 /* 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
2023
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
2024 void *
13473
31b1f691a7d9 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
2025 PyMem_Malloc(size_t nbytes)
5931
12fe5cc1516d Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
2026 {
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12481
diff changeset
2027 return PyMem_MALLOC(nbytes);
5931
12fe5cc1516d Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
2028 }
12fe5cc1516d Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
2029
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
2030 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
2031 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
2032 {
22985
d9d82c1ef864 First stab at rationalizing the PyMem_ API. Mixing PyObject_xyz with
Tim Peters <tim.peters@gmail.com>
parents: 22982
diff changeset
2033 return PyMem_REALLOC(p, nbytes);
5931
12fe5cc1516d Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
2034 }
12fe5cc1516d Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
2035
12fe5cc1516d Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
2036 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
2037 PyMem_Free(void *p)
5931
12fe5cc1516d Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
2038 {
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12481
diff changeset
2039 PyMem_FREE(p);
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12481
diff changeset
2040 }
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12481
diff changeset
2041
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12481
diff changeset
2042
7984
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
2043 /* 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
2044 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
2045 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
2046 Py_ReprLeave() to avoid infinite recursion.
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
2047
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
2048 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
2049 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
2050 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
2051
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
2052 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
2053 */
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
2054
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
2055 #define KEY "Py_Repr"
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
2056
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
2057 int
13473
31b1f691a7d9 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
2058 Py_ReprEnter(PyObject *obj)
7984
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
2059 {
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
2060 PyObject *dict;
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
2061 PyObject *list;
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
2062 int i;
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
2063
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
2064 dict = PyThreadState_GetDict();
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
2065 if (dict == NULL)
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
2066 return -1;
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
2067 list = PyDict_GetItemString(dict, KEY);
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
2068 if (list == NULL) {
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
2069 list = PyList_New(0);
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
2070 if (list == NULL)
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
2071 return -1;
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
2072 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
2073 return -1;
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
2074 Py_DECREF(list);
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
2075 }
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
2076 i = PyList_GET_SIZE(list);
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
2077 while (--i >= 0) {
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
2078 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
2079 return 1;
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
2080 }
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
2081 PyList_Append(list, obj);
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
2082 return 0;
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
2083 }
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
2084
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
2085 void
13473
31b1f691a7d9 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
2086 Py_ReprLeave(PyObject *obj)
7984
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
2087 {
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
2088 PyObject *dict;
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
2089 PyObject *list;
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
2090 int i;
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
2091
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
2092 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
2093 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
2094 return;
7984
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
2095 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
2096 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
2097 return;
7984
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
2098 i = PyList_GET_SIZE(list);
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
2099 /* 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
2100 while (--i >= 0) {
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
2101 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
2102 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
2103 break;
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
2104 }
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
2105 }
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
2106 }
11999
c94247b5c355 Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
2107
24122
d332484e394a Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents: 24121
diff changeset
2108 /* Trashcan support. */
12052
5d3165e30bce Christian Tismer:
Guido van Rossum <guido@python.org>
parents: 11999
diff changeset
2109
24122
d332484e394a Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents: 24121
diff changeset
2110 /* Current call-stack depth of tp_dealloc calls. */
11999
c94247b5c355 Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
2111 int _PyTrash_delete_nesting = 0;
12481
a8898a172fe2 Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents: 12406
diff changeset
2112
24122
d332484e394a Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents: 24121
diff changeset
2113 /* List of objects that still need to be cleaned up, singly linked via their
d332484e394a Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents: 24121
diff changeset
2114 * gc headers' gc_prev pointers.
d332484e394a Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents: 24121
diff changeset
2115 */
d332484e394a Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents: 24121
diff changeset
2116 PyObject *_PyTrash_delete_later = NULL;
11999
c94247b5c355 Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
2117
24122
d332484e394a Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents: 24121
diff changeset
2118 /* Add op to the _PyTrash_delete_later list. Called when the current
d332484e394a Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents: 24121
diff changeset
2119 * call-stack depth gets large. op must be a currently untracked gc'ed
d332484e394a Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents: 24121
diff changeset
2120 * object, with refcount 0. Py_DECREF must already have been called on it.
d332484e394a Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents: 24121
diff changeset
2121 */
11999
c94247b5c355 Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
2122 void
13473
31b1f691a7d9 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
2123 _PyTrash_deposit_object(PyObject *op)
11999
c94247b5c355 Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
2124 {
24122
d332484e394a Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents: 24121
diff changeset
2125 assert(PyObject_IS_GC(op));
d332484e394a Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents: 24121
diff changeset
2126 assert(_Py_AS_GC(op)->gc.gc_refs == _PyGC_REFS_UNTRACKED);
d332484e394a Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents: 24121
diff changeset
2127 assert(op->ob_refcnt == 0);
22649
1616bd8b31bc If the GC is enabled then don't use the ob_type pointer to create a list
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22469
diff changeset
2128 _Py_AS_GC(op)->gc.gc_prev = (PyGC_Head *)_PyTrash_delete_later;
12481
a8898a172fe2 Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents: 12406
diff changeset
2129 _PyTrash_delete_later = op;
11999
c94247b5c355 Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
2130 }
c94247b5c355 Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
2131
24122
d332484e394a Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents: 24121
diff changeset
2132 /* Dealloccate all the objects in the _PyTrash_delete_later list. Called when
d332484e394a Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents: 24121
diff changeset
2133 * the call-stack unwinds again.
d332484e394a Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents: 24121
diff changeset
2134 */
11999
c94247b5c355 Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
2135 void
13473
31b1f691a7d9 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
2136 _PyTrash_destroy_chain(void)
11999
c94247b5c355 Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
2137 {
c94247b5c355 Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
2138 while (_PyTrash_delete_later) {
24122
d332484e394a Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents: 24121
diff changeset
2139 PyObject *op = _PyTrash_delete_later;
d332484e394a Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents: 24121
diff changeset
2140 destructor dealloc = op->ob_type->tp_dealloc;
22649
1616bd8b31bc If the GC is enabled then don't use the ob_type pointer to create a list
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22469
diff changeset
2141
1616bd8b31bc If the GC is enabled then don't use the ob_type pointer to create a list
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22469
diff changeset
2142 _PyTrash_delete_later =
24122
d332484e394a Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents: 24121
diff changeset
2143 (PyObject*) _Py_AS_GC(op)->gc.gc_prev;
22649
1616bd8b31bc If the GC is enabled then don't use the ob_type pointer to create a list
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22469
diff changeset
2144
24122
d332484e394a Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents: 24121
diff changeset
2145 /* Call the deallocator directly. This used to try to
d332484e394a Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents: 24121
diff changeset
2146 * fool Py_DECREF into calling it indirectly, but
d332484e394a Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents: 24121
diff changeset
2147 * Py_DECREF was already called on this object, and in
d332484e394a Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents: 24121
diff changeset
2148 * assorted non-release builds calling Py_DECREF again ends
d332484e394a Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents: 24121
diff changeset
2149 * up distorting allocation statistics.
d332484e394a Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents: 24121
diff changeset
2150 */
d332484e394a Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents: 24121
diff changeset
2151 assert(op->ob_refcnt == 0);
11999
c94247b5c355 Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
2152 ++_PyTrash_delete_nesting;
24122
d332484e394a Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents: 24121
diff changeset
2153 (*dealloc)(op);
11999
c94247b5c355 Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
2154 --_PyTrash_delete_nesting;
c94247b5c355 Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
2155 }
c94247b5c355 Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
2156 }