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