Mercurial > cpython-lru_cache-weakref
annotate Objects/object.c @ 58925:eecf1bef052d 2.6
Merged revisions 78207 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r78207 | ezio.melotti | 2010-02-17 01:26:09 +0200 (Wed, 17 Feb 2010) | 1 line
#7930: fix stripid
........
| author | Ezio Melotti <ezio.melotti@gmail.com> |
|---|---|
| date | Tue, 16 Feb 2010 23:29:44 +0000 |
| parents | bc1c403054e1 |
| children | e38335d4df91 |
| 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" |
|
53273
42bf56e34825
Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
53243
diff
changeset
|
5 #include "frameobject.h" |
| 6 | 6 |
|
37655
2f3f2e32dfd9
spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37638
diff
changeset
|
7 #ifdef __cplusplus |
|
2f3f2e32dfd9
spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37638
diff
changeset
|
8 extern "C" { |
|
2f3f2e32dfd9
spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37638
diff
changeset
|
9 #endif |
|
2f3f2e32dfd9
spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37638
diff
changeset
|
10 |
|
24184
c4dcb80f48ea
object.h special-build macro minefield: renamed all the new lexical
Tim Peters <tim.peters@gmail.com>
parents:
24155
diff
changeset
|
11 #ifdef Py_REF_DEBUG |
|
36838
dedc2c94d112
Oops, forgot to include this in the last checkin.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36759
diff
changeset
|
12 Py_ssize_t _Py_RefTotal; |
|
37638
65ecb83ea7dd
Ignore the references to the dummy objects used as deleted keys
Armin Rigo <arigo@tunes.org>
parents:
37609
diff
changeset
|
13 |
|
65ecb83ea7dd
Ignore the references to the dummy objects used as deleted keys
Armin Rigo <arigo@tunes.org>
parents:
37609
diff
changeset
|
14 Py_ssize_t |
|
65ecb83ea7dd
Ignore the references to the dummy objects used as deleted keys
Armin Rigo <arigo@tunes.org>
parents:
37609
diff
changeset
|
15 _Py_GetRefTotal(void) |
|
65ecb83ea7dd
Ignore the references to the dummy objects used as deleted keys
Armin Rigo <arigo@tunes.org>
parents:
37609
diff
changeset
|
16 { |
|
65ecb83ea7dd
Ignore the references to the dummy objects used as deleted keys
Armin Rigo <arigo@tunes.org>
parents:
37609
diff
changeset
|
17 PyObject *o; |
|
65ecb83ea7dd
Ignore the references to the dummy objects used as deleted keys
Armin Rigo <arigo@tunes.org>
parents:
37609
diff
changeset
|
18 Py_ssize_t total = _Py_RefTotal; |
|
65ecb83ea7dd
Ignore the references to the dummy objects used as deleted keys
Armin Rigo <arigo@tunes.org>
parents:
37609
diff
changeset
|
19 /* ignore the references to the dummy object of the dicts and sets |
|
65ecb83ea7dd
Ignore the references to the dummy objects used as deleted keys
Armin Rigo <arigo@tunes.org>
parents:
37609
diff
changeset
|
20 because they are not reliable and not useful (now that the |
|
65ecb83ea7dd
Ignore the references to the dummy objects used as deleted keys
Armin Rigo <arigo@tunes.org>
parents:
37609
diff
changeset
|
21 hash table code is well-tested) */ |
|
65ecb83ea7dd
Ignore the references to the dummy objects used as deleted keys
Armin Rigo <arigo@tunes.org>
parents:
37609
diff
changeset
|
22 o = _PyDict_Dummy(); |
|
65ecb83ea7dd
Ignore the references to the dummy objects used as deleted keys
Armin Rigo <arigo@tunes.org>
parents:
37609
diff
changeset
|
23 if (o != NULL) |
|
65ecb83ea7dd
Ignore the references to the dummy objects used as deleted keys
Armin Rigo <arigo@tunes.org>
parents:
37609
diff
changeset
|
24 total -= o->ob_refcnt; |
|
65ecb83ea7dd
Ignore the references to the dummy objects used as deleted keys
Armin Rigo <arigo@tunes.org>
parents:
37609
diff
changeset
|
25 o = _PySet_Dummy(); |
|
65ecb83ea7dd
Ignore the references to the dummy objects used as deleted keys
Armin Rigo <arigo@tunes.org>
parents:
37609
diff
changeset
|
26 if (o != NULL) |
|
65ecb83ea7dd
Ignore the references to the dummy objects used as deleted keys
Armin Rigo <arigo@tunes.org>
parents:
37609
diff
changeset
|
27 total -= o->ob_refcnt; |
|
65ecb83ea7dd
Ignore the references to the dummy objects used as deleted keys
Armin Rigo <arigo@tunes.org>
parents:
37609
diff
changeset
|
28 return total; |
|
65ecb83ea7dd
Ignore the references to the dummy objects used as deleted keys
Armin Rigo <arigo@tunes.org>
parents:
37609
diff
changeset
|
29 } |
|
65ecb83ea7dd
Ignore the references to the dummy objects used as deleted keys
Armin Rigo <arigo@tunes.org>
parents:
37609
diff
changeset
|
30 #endif /* Py_REF_DEBUG */ |
| 6 | 31 |
|
24420
36e2fd3f08f7
Excise DL_IMPORT/EXPORT from object.h, and related files. This patch
Mark Hammond <mhammond@skippinet.com.au>
parents:
24184
diff
changeset
|
32 int Py_DivisionWarningFlag; |
|
41832
a0ee3b2ba361
Add -3 option to the interpreter to warn about features that are
Neal Norwitz <nnorwitz@gmail.com>
parents:
41214
diff
changeset
|
33 int Py_Py3kWarningFlag; |
|
19633
c0a44829bcd5
Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents:
19620
diff
changeset
|
34 |
| 86 | 35 /* Object allocation routines used by NEWOBJ and NEWVAROBJ macros. |
| 36 These are used by the individual routines for object creation. | |
| 37 Do not call them otherwise, they do not initialize the object! */ | |
| 6 | 38 |
|
28387
983e9291a7b4
When Py_TRACE_REFS is defined, a list of all live objects is maintained in
Tim Peters <tim.peters@gmail.com>
parents:
28320
diff
changeset
|
39 #ifdef Py_TRACE_REFS |
|
28396
d56282c28a1e
Improved new Py_TRACE_REFS gimmicks.
Tim Peters <tim.peters@gmail.com>
parents:
28389
diff
changeset
|
40 /* Head of circular doubly-linked list of all objects. These are linked |
|
d56282c28a1e
Improved new Py_TRACE_REFS gimmicks.
Tim Peters <tim.peters@gmail.com>
parents:
28389
diff
changeset
|
41 * together via the _ob_prev and _ob_next members of a PyObject, which |
|
d56282c28a1e
Improved new Py_TRACE_REFS gimmicks.
Tim Peters <tim.peters@gmail.com>
parents:
28389
diff
changeset
|
42 * exist only in a Py_TRACE_REFS build. |
|
d56282c28a1e
Improved new Py_TRACE_REFS gimmicks.
Tim Peters <tim.peters@gmail.com>
parents:
28389
diff
changeset
|
43 */ |
|
28387
983e9291a7b4
When Py_TRACE_REFS is defined, a list of all live objects is maintained in
Tim Peters <tim.peters@gmail.com>
parents:
28320
diff
changeset
|
44 static PyObject refchain = {&refchain, &refchain}; |
|
28389
4ede57a42c62
Refactored some of the Py_TRACE_REFS code. New private API function
Tim Peters <tim.peters@gmail.com>
parents:
28388
diff
changeset
|
45 |
|
28396
d56282c28a1e
Improved new Py_TRACE_REFS gimmicks.
Tim Peters <tim.peters@gmail.com>
parents:
28389
diff
changeset
|
46 /* Insert op at the front of the list of all objects. If force is true, |
|
d56282c28a1e
Improved new Py_TRACE_REFS gimmicks.
Tim Peters <tim.peters@gmail.com>
parents:
28389
diff
changeset
|
47 * op is added even if _ob_prev and _ob_next are non-NULL already. If |
|
d56282c28a1e
Improved new Py_TRACE_REFS gimmicks.
Tim Peters <tim.peters@gmail.com>
parents:
28389
diff
changeset
|
48 * force is false amd _ob_prev or _ob_next are non-NULL, do nothing. |
|
d56282c28a1e
Improved new Py_TRACE_REFS gimmicks.
Tim Peters <tim.peters@gmail.com>
parents:
28389
diff
changeset
|
49 * force should be true if and only if op points to freshly allocated, |
|
d56282c28a1e
Improved new Py_TRACE_REFS gimmicks.
Tim Peters <tim.peters@gmail.com>
parents:
28389
diff
changeset
|
50 * uninitialized memory, or you've unlinked op from the list and are |
| 28397 | 51 * relinking it into the front. |
|
28396
d56282c28a1e
Improved new Py_TRACE_REFS gimmicks.
Tim Peters <tim.peters@gmail.com>
parents:
28389
diff
changeset
|
52 * Note that objects are normally added to the list via _Py_NewReference, |
|
d56282c28a1e
Improved new Py_TRACE_REFS gimmicks.
Tim Peters <tim.peters@gmail.com>
parents:
28389
diff
changeset
|
53 * which is called by PyObject_Init. Not all objects are initialized that |
|
d56282c28a1e
Improved new Py_TRACE_REFS gimmicks.
Tim Peters <tim.peters@gmail.com>
parents:
28389
diff
changeset
|
54 * way, though; exceptions include statically allocated type objects, and |
|
d56282c28a1e
Improved new Py_TRACE_REFS gimmicks.
Tim Peters <tim.peters@gmail.com>
parents:
28389
diff
changeset
|
55 * statically allocated singletons (like Py_True and Py_None). |
|
d56282c28a1e
Improved new Py_TRACE_REFS gimmicks.
Tim Peters <tim.peters@gmail.com>
parents:
28389
diff
changeset
|
56 */ |
|
28389
4ede57a42c62
Refactored some of the Py_TRACE_REFS code. New private API function
Tim Peters <tim.peters@gmail.com>
parents:
28388
diff
changeset
|
57 void |
|
28396
d56282c28a1e
Improved new Py_TRACE_REFS gimmicks.
Tim Peters <tim.peters@gmail.com>
parents:
28389
diff
changeset
|
58 _Py_AddToAllObjects(PyObject *op, int force) |
|
28389
4ede57a42c62
Refactored some of the Py_TRACE_REFS code. New private API function
Tim Peters <tim.peters@gmail.com>
parents:
28388
diff
changeset
|
59 { |
|
28396
d56282c28a1e
Improved new Py_TRACE_REFS gimmicks.
Tim Peters <tim.peters@gmail.com>
parents:
28389
diff
changeset
|
60 #ifdef Py_DEBUG |
|
d56282c28a1e
Improved new Py_TRACE_REFS gimmicks.
Tim Peters <tim.peters@gmail.com>
parents:
28389
diff
changeset
|
61 if (!force) { |
|
d56282c28a1e
Improved new Py_TRACE_REFS gimmicks.
Tim Peters <tim.peters@gmail.com>
parents:
28389
diff
changeset
|
62 /* If it's initialized memory, op must be in or out of |
|
d56282c28a1e
Improved new Py_TRACE_REFS gimmicks.
Tim Peters <tim.peters@gmail.com>
parents:
28389
diff
changeset
|
63 * the list unambiguously. |
|
d56282c28a1e
Improved new Py_TRACE_REFS gimmicks.
Tim Peters <tim.peters@gmail.com>
parents:
28389
diff
changeset
|
64 */ |
|
d56282c28a1e
Improved new Py_TRACE_REFS gimmicks.
Tim Peters <tim.peters@gmail.com>
parents:
28389
diff
changeset
|
65 assert((op->_ob_prev == NULL) == (op->_ob_next == NULL)); |
|
d56282c28a1e
Improved new Py_TRACE_REFS gimmicks.
Tim Peters <tim.peters@gmail.com>
parents:
28389
diff
changeset
|
66 } |
|
d56282c28a1e
Improved new Py_TRACE_REFS gimmicks.
Tim Peters <tim.peters@gmail.com>
parents:
28389
diff
changeset
|
67 #endif |
|
d56282c28a1e
Improved new Py_TRACE_REFS gimmicks.
Tim Peters <tim.peters@gmail.com>
parents:
28389
diff
changeset
|
68 if (force || op->_ob_prev == NULL) { |
|
d56282c28a1e
Improved new Py_TRACE_REFS gimmicks.
Tim Peters <tim.peters@gmail.com>
parents:
28389
diff
changeset
|
69 op->_ob_next = refchain._ob_next; |
|
d56282c28a1e
Improved new Py_TRACE_REFS gimmicks.
Tim Peters <tim.peters@gmail.com>
parents:
28389
diff
changeset
|
70 op->_ob_prev = &refchain; |
|
d56282c28a1e
Improved new Py_TRACE_REFS gimmicks.
Tim Peters <tim.peters@gmail.com>
parents:
28389
diff
changeset
|
71 refchain._ob_next->_ob_prev = op; |
|
d56282c28a1e
Improved new Py_TRACE_REFS gimmicks.
Tim Peters <tim.peters@gmail.com>
parents:
28389
diff
changeset
|
72 refchain._ob_next = op; |
|
d56282c28a1e
Improved new Py_TRACE_REFS gimmicks.
Tim Peters <tim.peters@gmail.com>
parents:
28389
diff
changeset
|
73 } |
|
28389
4ede57a42c62
Refactored some of the Py_TRACE_REFS code. New private API function
Tim Peters <tim.peters@gmail.com>
parents:
28388
diff
changeset
|
74 } |
|
28396
d56282c28a1e
Improved new Py_TRACE_REFS gimmicks.
Tim Peters <tim.peters@gmail.com>
parents:
28389
diff
changeset
|
75 #endif /* Py_TRACE_REFS */ |
|
28387
983e9291a7b4
When Py_TRACE_REFS is defined, a list of all live objects is maintained in
Tim Peters <tim.peters@gmail.com>
parents:
28320
diff
changeset
|
76 |
|
1347
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1324
diff
changeset
|
77 #ifdef COUNT_ALLOCS |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
78 static PyTypeObject *type_list; |
| 37832 | 79 /* All types are added to type_list, at least when |
|
37831
d82dced6f1fa
Remove types from type_list if they have no objects
Martin v. Löwis <martin@v.loewis.de>
parents:
37655
diff
changeset
|
80 they get one object created. That makes them |
|
d82dced6f1fa
Remove types from type_list if they have no objects
Martin v. Löwis <martin@v.loewis.de>
parents:
37655
diff
changeset
|
81 immortal, which unfortunately contributes to |
|
d82dced6f1fa
Remove types from type_list if they have no objects
Martin v. Löwis <martin@v.loewis.de>
parents:
37655
diff
changeset
|
82 garbage itself. If unlist_types_without_objects |
|
d82dced6f1fa
Remove types from type_list if they have no objects
Martin v. Löwis <martin@v.loewis.de>
parents:
37655
diff
changeset
|
83 is set, they will be removed from the type_list |
|
d82dced6f1fa
Remove types from type_list if they have no objects
Martin v. Löwis <martin@v.loewis.de>
parents:
37655
diff
changeset
|
84 once the last object is deallocated. */ |
|
d82dced6f1fa
Remove types from type_list if they have no objects
Martin v. Löwis <martin@v.loewis.de>
parents:
37655
diff
changeset
|
85 int unlist_types_without_objects; |
|
1352
4d1a15fc4748
intobject.c: Save references to small integers, so that they can be
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
86 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
|
87 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
|
88 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
|
89 void |
|
37831
d82dced6f1fa
Remove types from type_list if they have no objects
Martin v. Löwis <martin@v.loewis.de>
parents:
37655
diff
changeset
|
90 dump_counts(FILE* f) |
|
1347
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1324
diff
changeset
|
91 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
92 PyTypeObject *tp; |
|
1347
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1324
diff
changeset
|
93 |
|
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1324
diff
changeset
|
94 for (tp = type_list; tp; tp = tp->tp_next) |
|
37831
d82dced6f1fa
Remove types from type_list if they have no objects
Martin v. Löwis <martin@v.loewis.de>
parents:
37655
diff
changeset
|
95 fprintf(f, "%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
|
96 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
|
97 tp->tp_maxalloc); |
|
37831
d82dced6f1fa
Remove types from type_list if they have no objects
Martin v. Löwis <martin@v.loewis.de>
parents:
37655
diff
changeset
|
98 fprintf(f, "fast tuple allocs: %d, empty: %d\n", |
|
1361
7cd533cf194a
import.c: When something is wrong with the .pyc, properly open the .py
Sjoerd Mullender <sjoerd@acm.org>
parents:
1356
diff
changeset
|
99 fast_tuple_allocs, tuple_zero_allocs); |
|
37831
d82dced6f1fa
Remove types from type_list if they have no objects
Martin v. Löwis <martin@v.loewis.de>
parents:
37655
diff
changeset
|
100 fprintf(f, "fast int allocs: pos: %d, neg: %d\n", |
|
1361
7cd533cf194a
import.c: When something is wrong with the .pyc, properly open the .py
Sjoerd Mullender <sjoerd@acm.org>
parents:
1356
diff
changeset
|
101 quick_int_allocs, quick_neg_int_allocs); |
|
37831
d82dced6f1fa
Remove types from type_list if they have no objects
Martin v. Löwis <martin@v.loewis.de>
parents:
37655
diff
changeset
|
102 fprintf(f, "null strings: %d, 1-strings: %d\n", |
|
1361
7cd533cf194a
import.c: When something is wrong with the .pyc, properly open the .py
Sjoerd Mullender <sjoerd@acm.org>
parents:
1356
diff
changeset
|
103 null_strings, one_strings); |
|
1347
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1324
diff
changeset
|
104 } |
|
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1324
diff
changeset
|
105 |
|
3024
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
106 PyObject * |
|
13473
31b1f691a7d9
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
107 get_counts(void) |
|
3024
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
108 { |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
109 PyTypeObject *tp; |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
110 PyObject *result; |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
111 PyObject *v; |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
112 |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
113 result = PyList_New(0); |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
114 if (result == NULL) |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
115 return NULL; |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
116 for (tp = type_list; tp; tp = tp->tp_next) { |
|
38485
dcf92dd7408a
Correct some value converting strangenesses.
Georg Brandl <georg@python.org>
parents:
37832
diff
changeset
|
117 v = Py_BuildValue("(snnn)", tp->tp_name, tp->tp_allocs, |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
118 tp->tp_frees, tp->tp_maxalloc); |
|
3024
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
119 if (v == NULL) { |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
120 Py_DECREF(result); |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
121 return NULL; |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
122 } |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
123 if (PyList_Append(result, v) < 0) { |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
124 Py_DECREF(v); |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
125 Py_DECREF(result); |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
126 return NULL; |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
127 } |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
128 Py_DECREF(v); |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
129 } |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
130 return result; |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
131 } |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
132 |
|
1347
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1324
diff
changeset
|
133 void |
|
13473
31b1f691a7d9
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
134 inc_count(PyTypeObject *tp) |
|
1347
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1324
diff
changeset
|
135 { |
|
37831
d82dced6f1fa
Remove types from type_list if they have no objects
Martin v. Löwis <martin@v.loewis.de>
parents:
37655
diff
changeset
|
136 if (tp->tp_next == NULL && tp->tp_prev == NULL) { |
|
2639
f11ba9cc342f
change in counting freed objects
Guido van Rossum <guido@python.org>
parents:
2612
diff
changeset
|
137 /* 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
|
138 if (tp->tp_next != NULL) /* sanity check */ |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
139 Py_FatalError("XXX inc_count sanity check"); |
|
37831
d82dced6f1fa
Remove types from type_list if they have no objects
Martin v. Löwis <martin@v.loewis.de>
parents:
37655
diff
changeset
|
140 if (type_list) |
|
d82dced6f1fa
Remove types from type_list if they have no objects
Martin v. Löwis <martin@v.loewis.de>
parents:
37655
diff
changeset
|
141 type_list->tp_prev = tp; |
|
1347
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1324
diff
changeset
|
142 tp->tp_next = type_list; |
|
24144
d7d816e1f522
SF bug 578752: COUNT_ALLOCS vs heap types
Tim Peters <tim.peters@gmail.com>
parents:
24126
diff
changeset
|
143 /* Note that as of Python 2.2, heap-allocated type objects |
|
d7d816e1f522
SF bug 578752: COUNT_ALLOCS vs heap types
Tim Peters <tim.peters@gmail.com>
parents:
24126
diff
changeset
|
144 * can go away, but this code requires that they stay alive |
|
d7d816e1f522
SF bug 578752: COUNT_ALLOCS vs heap types
Tim Peters <tim.peters@gmail.com>
parents:
24126
diff
changeset
|
145 * until program exit. That's why we're careful with |
|
d7d816e1f522
SF bug 578752: COUNT_ALLOCS vs heap types
Tim Peters <tim.peters@gmail.com>
parents:
24126
diff
changeset
|
146 * refcounts here. type_list gets a new reference to tp, |
|
d7d816e1f522
SF bug 578752: COUNT_ALLOCS vs heap types
Tim Peters <tim.peters@gmail.com>
parents:
24126
diff
changeset
|
147 * while ownership of the reference type_list used to hold |
|
d7d816e1f522
SF bug 578752: COUNT_ALLOCS vs heap types
Tim Peters <tim.peters@gmail.com>
parents:
24126
diff
changeset
|
148 * (if any) was transferred to tp->tp_next in the line above. |
|
d7d816e1f522
SF bug 578752: COUNT_ALLOCS vs heap types
Tim Peters <tim.peters@gmail.com>
parents:
24126
diff
changeset
|
149 * tp is thus effectively immortal after this. |
|
d7d816e1f522
SF bug 578752: COUNT_ALLOCS vs heap types
Tim Peters <tim.peters@gmail.com>
parents:
24126
diff
changeset
|
150 */ |
|
d7d816e1f522
SF bug 578752: COUNT_ALLOCS vs heap types
Tim Peters <tim.peters@gmail.com>
parents:
24126
diff
changeset
|
151 Py_INCREF(tp); |
|
1347
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1324
diff
changeset
|
152 type_list = tp; |
|
28388
f81239c12c60
Oops! Used a wrong preprocessor symbol.
Tim Peters <tim.peters@gmail.com>
parents:
28387
diff
changeset
|
153 #ifdef Py_TRACE_REFS |
|
28396
d56282c28a1e
Improved new Py_TRACE_REFS gimmicks.
Tim Peters <tim.peters@gmail.com>
parents:
28389
diff
changeset
|
154 /* Also insert in the doubly-linked list of all objects, |
|
d56282c28a1e
Improved new Py_TRACE_REFS gimmicks.
Tim Peters <tim.peters@gmail.com>
parents:
28389
diff
changeset
|
155 * if not already there. |
|
d56282c28a1e
Improved new Py_TRACE_REFS gimmicks.
Tim Peters <tim.peters@gmail.com>
parents:
28389
diff
changeset
|
156 */ |
|
d56282c28a1e
Improved new Py_TRACE_REFS gimmicks.
Tim Peters <tim.peters@gmail.com>
parents:
28389
diff
changeset
|
157 _Py_AddToAllObjects((PyObject *)tp, 0); |
|
28387
983e9291a7b4
When Py_TRACE_REFS is defined, a list of all live objects is maintained in
Tim Peters <tim.peters@gmail.com>
parents:
28320
diff
changeset
|
158 #endif |
|
1347
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1324
diff
changeset
|
159 } |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
160 tp->tp_allocs++; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
161 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
|
162 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
|
163 } |
|
37831
d82dced6f1fa
Remove types from type_list if they have no objects
Martin v. Löwis <martin@v.loewis.de>
parents:
37655
diff
changeset
|
164 |
|
d82dced6f1fa
Remove types from type_list if they have no objects
Martin v. Löwis <martin@v.loewis.de>
parents:
37655
diff
changeset
|
165 void dec_count(PyTypeObject *tp) |
|
d82dced6f1fa
Remove types from type_list if they have no objects
Martin v. Löwis <martin@v.loewis.de>
parents:
37655
diff
changeset
|
166 { |
|
d82dced6f1fa
Remove types from type_list if they have no objects
Martin v. Löwis <martin@v.loewis.de>
parents:
37655
diff
changeset
|
167 tp->tp_frees++; |
|
d82dced6f1fa
Remove types from type_list if they have no objects
Martin v. Löwis <martin@v.loewis.de>
parents:
37655
diff
changeset
|
168 if (unlist_types_without_objects && |
|
d82dced6f1fa
Remove types from type_list if they have no objects
Martin v. Löwis <martin@v.loewis.de>
parents:
37655
diff
changeset
|
169 tp->tp_allocs == tp->tp_frees) { |
|
d82dced6f1fa
Remove types from type_list if they have no objects
Martin v. Löwis <martin@v.loewis.de>
parents:
37655
diff
changeset
|
170 /* unlink the type from type_list */ |
|
d82dced6f1fa
Remove types from type_list if they have no objects
Martin v. Löwis <martin@v.loewis.de>
parents:
37655
diff
changeset
|
171 if (tp->tp_prev) |
|
d82dced6f1fa
Remove types from type_list if they have no objects
Martin v. Löwis <martin@v.loewis.de>
parents:
37655
diff
changeset
|
172 tp->tp_prev->tp_next = tp->tp_next; |
|
d82dced6f1fa
Remove types from type_list if they have no objects
Martin v. Löwis <martin@v.loewis.de>
parents:
37655
diff
changeset
|
173 else |
|
d82dced6f1fa
Remove types from type_list if they have no objects
Martin v. Löwis <martin@v.loewis.de>
parents:
37655
diff
changeset
|
174 type_list = tp->tp_next; |
|
d82dced6f1fa
Remove types from type_list if they have no objects
Martin v. Löwis <martin@v.loewis.de>
parents:
37655
diff
changeset
|
175 if (tp->tp_next) |
|
d82dced6f1fa
Remove types from type_list if they have no objects
Martin v. Löwis <martin@v.loewis.de>
parents:
37655
diff
changeset
|
176 tp->tp_next->tp_prev = tp->tp_prev; |
|
d82dced6f1fa
Remove types from type_list if they have no objects
Martin v. Löwis <martin@v.loewis.de>
parents:
37655
diff
changeset
|
177 tp->tp_next = tp->tp_prev = NULL; |
|
d82dced6f1fa
Remove types from type_list if they have no objects
Martin v. Löwis <martin@v.loewis.de>
parents:
37655
diff
changeset
|
178 Py_DECREF(tp); |
|
d82dced6f1fa
Remove types from type_list if they have no objects
Martin v. Löwis <martin@v.loewis.de>
parents:
37655
diff
changeset
|
179 } |
|
d82dced6f1fa
Remove types from type_list if they have no objects
Martin v. Löwis <martin@v.loewis.de>
parents:
37655
diff
changeset
|
180 } |
|
d82dced6f1fa
Remove types from type_list if they have no objects
Martin v. Löwis <martin@v.loewis.de>
parents:
37655
diff
changeset
|
181 |
|
1347
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1324
diff
changeset
|
182 #endif |
|
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1324
diff
changeset
|
183 |
|
24155
9456a2ccda61
The Py_REF_DEBUG/COUNT_ALLOCS/Py_TRACE_REFS macro minefield: added
Tim Peters <tim.peters@gmail.com>
parents:
24144
diff
changeset
|
184 #ifdef Py_REF_DEBUG |
|
9456a2ccda61
The Py_REF_DEBUG/COUNT_ALLOCS/Py_TRACE_REFS macro minefield: added
Tim Peters <tim.peters@gmail.com>
parents:
24144
diff
changeset
|
185 /* Log a fatal error; doesn't return. */ |
|
9456a2ccda61
The Py_REF_DEBUG/COUNT_ALLOCS/Py_TRACE_REFS macro minefield: added
Tim Peters <tim.peters@gmail.com>
parents:
24144
diff
changeset
|
186 void |
|
9456a2ccda61
The Py_REF_DEBUG/COUNT_ALLOCS/Py_TRACE_REFS macro minefield: added
Tim Peters <tim.peters@gmail.com>
parents:
24144
diff
changeset
|
187 _Py_NegativeRefcount(const char *fname, int lineno, PyObject *op) |
|
9456a2ccda61
The Py_REF_DEBUG/COUNT_ALLOCS/Py_TRACE_REFS macro minefield: added
Tim Peters <tim.peters@gmail.com>
parents:
24144
diff
changeset
|
188 { |
|
9456a2ccda61
The Py_REF_DEBUG/COUNT_ALLOCS/Py_TRACE_REFS macro minefield: added
Tim Peters <tim.peters@gmail.com>
parents:
24144
diff
changeset
|
189 char buf[300]; |
|
9456a2ccda61
The Py_REF_DEBUG/COUNT_ALLOCS/Py_TRACE_REFS macro minefield: added
Tim Peters <tim.peters@gmail.com>
parents:
24144
diff
changeset
|
190 |
|
9456a2ccda61
The Py_REF_DEBUG/COUNT_ALLOCS/Py_TRACE_REFS macro minefield: added
Tim Peters <tim.peters@gmail.com>
parents:
24144
diff
changeset
|
191 PyOS_snprintf(buf, sizeof(buf), |
|
37161
b2e6d6aa86d1
_Py_NegativeRefcount(): print the full value of ob_refcnt.
Tim Peters <tim.peters@gmail.com>
parents:
36977
diff
changeset
|
192 "%s:%i object at %p has negative ref count " |
|
b2e6d6aa86d1
_Py_NegativeRefcount(): print the full value of ob_refcnt.
Tim Peters <tim.peters@gmail.com>
parents:
36977
diff
changeset
|
193 "%" PY_FORMAT_SIZE_T "d", |
|
37163
0c602938ed8e
Heh -- used the right format for a refcount, but forgot
Tim Peters <tim.peters@gmail.com>
parents:
37161
diff
changeset
|
194 fname, lineno, op, op->ob_refcnt); |
|
24155
9456a2ccda61
The Py_REF_DEBUG/COUNT_ALLOCS/Py_TRACE_REFS macro minefield: added
Tim Peters <tim.peters@gmail.com>
parents:
24144
diff
changeset
|
195 Py_FatalError(buf); |
|
9456a2ccda61
The Py_REF_DEBUG/COUNT_ALLOCS/Py_TRACE_REFS macro minefield: added
Tim Peters <tim.peters@gmail.com>
parents:
24144
diff
changeset
|
196 } |
|
9456a2ccda61
The Py_REF_DEBUG/COUNT_ALLOCS/Py_TRACE_REFS macro minefield: added
Tim Peters <tim.peters@gmail.com>
parents:
24144
diff
changeset
|
197 |
|
9456a2ccda61
The Py_REF_DEBUG/COUNT_ALLOCS/Py_TRACE_REFS macro minefield: added
Tim Peters <tim.peters@gmail.com>
parents:
24144
diff
changeset
|
198 #endif /* Py_REF_DEBUG */ |
|
9456a2ccda61
The Py_REF_DEBUG/COUNT_ALLOCS/Py_TRACE_REFS macro minefield: added
Tim Peters <tim.peters@gmail.com>
parents:
24144
diff
changeset
|
199 |
|
31940
059740d52ae5
Two new public API functions, Py_IncRef and Py_DecRef. Useful for
Thomas Heller <theller@ctypes.org>
parents:
31738
diff
changeset
|
200 void |
|
059740d52ae5
Two new public API functions, Py_IncRef and Py_DecRef. Useful for
Thomas Heller <theller@ctypes.org>
parents:
31738
diff
changeset
|
201 Py_IncRef(PyObject *o) |
|
059740d52ae5
Two new public API functions, Py_IncRef and Py_DecRef. Useful for
Thomas Heller <theller@ctypes.org>
parents:
31738
diff
changeset
|
202 { |
|
059740d52ae5
Two new public API functions, Py_IncRef and Py_DecRef. Useful for
Thomas Heller <theller@ctypes.org>
parents:
31738
diff
changeset
|
203 Py_XINCREF(o); |
|
059740d52ae5
Two new public API functions, Py_IncRef and Py_DecRef. Useful for
Thomas Heller <theller@ctypes.org>
parents:
31738
diff
changeset
|
204 } |
|
059740d52ae5
Two new public API functions, Py_IncRef and Py_DecRef. Useful for
Thomas Heller <theller@ctypes.org>
parents:
31738
diff
changeset
|
205 |
|
059740d52ae5
Two new public API functions, Py_IncRef and Py_DecRef. Useful for
Thomas Heller <theller@ctypes.org>
parents:
31738
diff
changeset
|
206 void |
|
059740d52ae5
Two new public API functions, Py_IncRef and Py_DecRef. Useful for
Thomas Heller <theller@ctypes.org>
parents:
31738
diff
changeset
|
207 Py_DecRef(PyObject *o) |
|
059740d52ae5
Two new public API functions, Py_IncRef and Py_DecRef. Useful for
Thomas Heller <theller@ctypes.org>
parents:
31738
diff
changeset
|
208 { |
|
059740d52ae5
Two new public API functions, Py_IncRef and Py_DecRef. Useful for
Thomas Heller <theller@ctypes.org>
parents:
31738
diff
changeset
|
209 Py_XDECREF(o); |
|
059740d52ae5
Two new public API functions, Py_IncRef and Py_DecRef. Useful for
Thomas Heller <theller@ctypes.org>
parents:
31738
diff
changeset
|
210 } |
|
059740d52ae5
Two new public API functions, Py_IncRef and Py_DecRef. Useful for
Thomas Heller <theller@ctypes.org>
parents:
31738
diff
changeset
|
211 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
212 PyObject * |
|
13473
31b1f691a7d9
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
213 PyObject_Init(PyObject *op, PyTypeObject *tp) |
| 6 | 214 { |
|
25737
b0fa5f6a8bb9
PyObject_Init[Var] is almost always called from the PyObject_NEW[_VAR]
Guido van Rossum <guido@python.org>
parents:
25015
diff
changeset
|
215 if (op == NULL) |
|
b0fa5f6a8bb9
PyObject_Init[Var] is almost always called from the PyObject_NEW[_VAR]
Guido van Rossum <guido@python.org>
parents:
25015
diff
changeset
|
216 return PyErr_NoMemory(); |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
217 /* Any changes should be reflected in PyObject_INIT (objimpl.h) */ |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
43958
diff
changeset
|
218 Py_TYPE(op) = tp; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
219 _Py_NewReference(op); |
| 6 | 220 return op; |
| 221 } | |
| 222 | |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
223 PyVarObject * |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
224 PyObject_InitVar(PyVarObject *op, PyTypeObject *tp, Py_ssize_t size) |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
225 { |
|
25737
b0fa5f6a8bb9
PyObject_Init[Var] is almost always called from the PyObject_NEW[_VAR]
Guido van Rossum <guido@python.org>
parents:
25015
diff
changeset
|
226 if (op == NULL) |
|
b0fa5f6a8bb9
PyObject_Init[Var] is almost always called from the PyObject_NEW[_VAR]
Guido van Rossum <guido@python.org>
parents:
25015
diff
changeset
|
227 return (PyVarObject *) PyErr_NoMemory(); |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
228 /* 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
|
229 op->ob_size = size; |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
43958
diff
changeset
|
230 Py_TYPE(op) = tp; |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
231 _Py_NewReference((PyObject *)op); |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
232 return op; |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
233 } |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
234 |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
235 PyObject * |
|
13473
31b1f691a7d9
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
236 _PyObject_New(PyTypeObject *tp) |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
237 { |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
238 PyObject *op; |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
239 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
|
240 if (op == NULL) |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
241 return PyErr_NoMemory(); |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
242 return PyObject_INIT(op, tp); |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
243 } |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
244 |
|
5535
20c2b715188e
Oops, another forgotten renaming: varobject -> PyVarObject.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
245 PyVarObject * |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
246 _PyObject_NewVar(PyTypeObject *tp, Py_ssize_t nitems) |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
247 { |
|
5535
20c2b715188e
Oops, another forgotten renaming: varobject -> PyVarObject.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
248 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
|
249 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
|
250 op = (PyVarObject *) PyObject_MALLOC(size); |
| 6 | 251 if (op == NULL) |
|
5535
20c2b715188e
Oops, another forgotten renaming: varobject -> PyVarObject.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
252 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
|
253 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
|
254 } |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
255 |
|
22982
d09740b2e03d
Move PyObject_Malloc and PyObject_Free to obmalloc.c.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22785
diff
changeset
|
256 /* for binary compatibility with 2.2 */ |
|
d09740b2e03d
Move PyObject_Malloc and PyObject_Free to obmalloc.c.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22785
diff
changeset
|
257 #undef _PyObject_Del |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
258 void |
|
13473
31b1f691a7d9
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
259 _PyObject_Del(PyObject *op) |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
260 { |
|
13265
e8bb0d536633
Neil Schemenauer: small fixes for GC
Guido van Rossum <guido@python.org>
parents:
13256
diff
changeset
|
261 PyObject_FREE(op); |
| 6 | 262 } |
| 263 | |
|
27234
e9f45b8efd6b
Fix SF bug #667147, Segmentation fault printing str subclass
Neal Norwitz <nnorwitz@gmail.com>
parents:
26263
diff
changeset
|
264 /* Implementation of PyObject_Print with recursion checking */ |
|
e9f45b8efd6b
Fix SF bug #667147, Segmentation fault printing str subclass
Neal Norwitz <nnorwitz@gmail.com>
parents:
26263
diff
changeset
|
265 static int |
|
e9f45b8efd6b
Fix SF bug #667147, Segmentation fault printing str subclass
Neal Norwitz <nnorwitz@gmail.com>
parents:
26263
diff
changeset
|
266 internal_print(PyObject *op, FILE *fp, int flags, int nesting) |
| 6 | 267 { |
|
322
1a6cf1f8ccec
Check for write errors after printing a value
Guido van Rossum <guido@python.org>
parents:
307
diff
changeset
|
268 int ret = 0; |
|
27234
e9f45b8efd6b
Fix SF bug #667147, Segmentation fault printing str subclass
Neal Norwitz <nnorwitz@gmail.com>
parents:
26263
diff
changeset
|
269 if (nesting > 10) { |
|
e9f45b8efd6b
Fix SF bug #667147, Segmentation fault printing str subclass
Neal Norwitz <nnorwitz@gmail.com>
parents:
26263
diff
changeset
|
270 PyErr_SetString(PyExc_RuntimeError, "print recursion"); |
|
e9f45b8efd6b
Fix SF bug #667147, Segmentation fault printing str subclass
Neal Norwitz <nnorwitz@gmail.com>
parents:
26263
diff
changeset
|
271 return -1; |
|
e9f45b8efd6b
Fix SF bug #667147, Segmentation fault printing str subclass
Neal Norwitz <nnorwitz@gmail.com>
parents:
26263
diff
changeset
|
272 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
273 if (PyErr_CheckSignals()) |
|
284
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
231
diff
changeset
|
274 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
|
275 #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
|
276 if (PyOS_CheckStack()) { |
| 15318 | 277 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
|
278 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
|
279 } |
|
c69ad566aeb1
If USE_STACKCHECK is defined use PyOS_CheckStack() in the repr and str
Guido van Rossum <guido@python.org>
parents:
8111
diff
changeset
|
280 #endif |
|
11649
38560a3a3249
On Linux, one sometimes sees spurious errors after interrupting
Guido van Rossum <guido@python.org>
parents:
8695
diff
changeset
|
281 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
|
282 if (op == NULL) { |
|
43378
7630248b593c
Add a bunch of GIL release/acquire points in tp_print implementations and for
Brett Cannon <bcannon@gmail.com>
parents:
42333
diff
changeset
|
283 Py_BEGIN_ALLOW_THREADS |
|
284
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
231
diff
changeset
|
284 fprintf(fp, "<nil>"); |
|
43378
7630248b593c
Add a bunch of GIL release/acquire points in tp_print implementations and for
Brett Cannon <bcannon@gmail.com>
parents:
42333
diff
changeset
|
285 Py_END_ALLOW_THREADS |
| 6 | 286 } |
|
284
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
231
diff
changeset
|
287 else { |
|
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
231
diff
changeset
|
288 if (op->ob_refcnt <= 0) |
|
36759
83b860acfe95
Use %ld and casts to long for refcount printing, in absense of a universally
Thomas Wouters <thomas@python.org>
parents:
36675
diff
changeset
|
289 /* XXX(twouters) cast refcount to long until %zd is |
|
83b860acfe95
Use %ld and casts to long for refcount printing, in absense of a universally
Thomas Wouters <thomas@python.org>
parents:
36675
diff
changeset
|
290 universally available */ |
|
43378
7630248b593c
Add a bunch of GIL release/acquire points in tp_print implementations and for
Brett Cannon <bcannon@gmail.com>
parents:
42333
diff
changeset
|
291 Py_BEGIN_ALLOW_THREADS |
|
36759
83b860acfe95
Use %ld and casts to long for refcount printing, in absense of a universally
Thomas Wouters <thomas@python.org>
parents:
36675
diff
changeset
|
292 fprintf(fp, "<refcnt %ld at %p>", |
|
83b860acfe95
Use %ld and casts to long for refcount printing, in absense of a universally
Thomas Wouters <thomas@python.org>
parents:
36675
diff
changeset
|
293 (long)op->ob_refcnt, op); |
|
43378
7630248b593c
Add a bunch of GIL release/acquire points in tp_print implementations and for
Brett Cannon <bcannon@gmail.com>
parents:
42333
diff
changeset
|
294 Py_END_ALLOW_THREADS |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
43958
diff
changeset
|
295 else if (Py_TYPE(op)->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
|
296 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
|
297 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
|
298 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
|
299 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
|
300 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
|
301 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
|
302 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
|
303 else { |
|
27234
e9f45b8efd6b
Fix SF bug #667147, Segmentation fault printing str subclass
Neal Norwitz <nnorwitz@gmail.com>
parents:
26263
diff
changeset
|
304 ret = internal_print(s, fp, Py_PRINT_RAW, |
|
e9f45b8efd6b
Fix SF bug #667147, Segmentation fault printing str subclass
Neal Norwitz <nnorwitz@gmail.com>
parents:
26263
diff
changeset
|
305 nesting+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
|
306 } |
|
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
|
307 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
|
308 } |
|
284
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
231
diff
changeset
|
309 else |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
43958
diff
changeset
|
310 ret = (*Py_TYPE(op)->tp_print)(op, fp, flags); |
| 6 | 311 } |
|
322
1a6cf1f8ccec
Check for write errors after printing a value
Guido van Rossum <guido@python.org>
parents:
307
diff
changeset
|
312 if (ret == 0) { |
|
1a6cf1f8ccec
Check for write errors after printing a value
Guido van Rossum <guido@python.org>
parents:
307
diff
changeset
|
313 if (ferror(fp)) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
314 PyErr_SetFromErrno(PyExc_IOError); |
|
322
1a6cf1f8ccec
Check for write errors after printing a value
Guido van Rossum <guido@python.org>
parents:
307
diff
changeset
|
315 clearerr(fp); |
|
1a6cf1f8ccec
Check for write errors after printing a value
Guido van Rossum <guido@python.org>
parents:
307
diff
changeset
|
316 ret = -1; |
|
1a6cf1f8ccec
Check for write errors after printing a value
Guido van Rossum <guido@python.org>
parents:
307
diff
changeset
|
317 } |
|
1a6cf1f8ccec
Check for write errors after printing a value
Guido van Rossum <guido@python.org>
parents:
307
diff
changeset
|
318 } |
|
1a6cf1f8ccec
Check for write errors after printing a value
Guido van Rossum <guido@python.org>
parents:
307
diff
changeset
|
319 return ret; |
| 6 | 320 } |
| 321 | |
|
27234
e9f45b8efd6b
Fix SF bug #667147, Segmentation fault printing str subclass
Neal Norwitz <nnorwitz@gmail.com>
parents:
26263
diff
changeset
|
322 int |
|
e9f45b8efd6b
Fix SF bug #667147, Segmentation fault printing str subclass
Neal Norwitz <nnorwitz@gmail.com>
parents:
26263
diff
changeset
|
323 PyObject_Print(PyObject *op, FILE *fp, int flags) |
|
e9f45b8efd6b
Fix SF bug #667147, Segmentation fault printing str subclass
Neal Norwitz <nnorwitz@gmail.com>
parents:
26263
diff
changeset
|
324 { |
|
e9f45b8efd6b
Fix SF bug #667147, Segmentation fault printing str subclass
Neal Norwitz <nnorwitz@gmail.com>
parents:
26263
diff
changeset
|
325 return internal_print(op, fp, flags, 0); |
|
e9f45b8efd6b
Fix SF bug #667147, Segmentation fault printing str subclass
Neal Norwitz <nnorwitz@gmail.com>
parents:
26263
diff
changeset
|
326 } |
|
e9f45b8efd6b
Fix SF bug #667147, Segmentation fault printing str subclass
Neal Norwitz <nnorwitz@gmail.com>
parents:
26263
diff
changeset
|
327 |
|
e9f45b8efd6b
Fix SF bug #667147, Segmentation fault printing str subclass
Neal Norwitz <nnorwitz@gmail.com>
parents:
26263
diff
changeset
|
328 |
|
16165
0a72070eab6e
A few miscellaneous helpers.
Barry Warsaw <barry@python.org>
parents:
16123
diff
changeset
|
329 /* For debugging convenience. See Misc/gdbinit for some useful gdb hooks */ |
|
24122
d332484e394a
Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents:
24121
diff
changeset
|
330 void _PyObject_Dump(PyObject* op) |
|
16165
0a72070eab6e
A few miscellaneous helpers.
Barry Warsaw <barry@python.org>
parents:
16123
diff
changeset
|
331 { |
|
16742
db61894b0d6f
_PyObject_Dump(): If argument is NULL, print "NULL" instead of
Barry Warsaw <barry@python.org>
parents:
16741
diff
changeset
|
332 if (op == NULL) |
|
db61894b0d6f
_PyObject_Dump(): If argument is NULL, print "NULL" instead of
Barry Warsaw <barry@python.org>
parents:
16741
diff
changeset
|
333 fprintf(stderr, "NULL\n"); |
|
db61894b0d6f
_PyObject_Dump(): If argument is NULL, print "NULL" instead of
Barry Warsaw <barry@python.org>
parents:
16741
diff
changeset
|
334 else { |
|
52903
3889d9528601
Merged revisions 71058,71149-71150,71212,71214-71216,71222,71225,71234,71237-71238,71240-71241,71243,71249,71251 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51466
diff
changeset
|
335 #ifdef WITH_THREAD |
|
50257
e4675113d10e
Merged revisions 67654,67676-67677,67681,67692,67725,67746,67748,67761,67784-67785,67787-67788,67802,67832,67848-67849,67859,67862-67864,67880,67882,67885,67889-67892,67895 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47961
diff
changeset
|
336 PyGILState_STATE gil; |
|
52903
3889d9528601
Merged revisions 71058,71149-71150,71212,71214-71216,71222,71225,71234,71237-71238,71240-71241,71243,71249,71251 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51466
diff
changeset
|
337 #endif |
|
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
|
338 fprintf(stderr, "object : "); |
|
52903
3889d9528601
Merged revisions 71058,71149-71150,71212,71214-71216,71222,71225,71234,71237-71238,71240-71241,71243,71249,71251 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51466
diff
changeset
|
339 #ifdef WITH_THREAD |
|
50257
e4675113d10e
Merged revisions 67654,67676-67677,67681,67692,67725,67746,67748,67761,67784-67785,67787-67788,67802,67832,67848-67849,67859,67862-67864,67880,67882,67885,67889-67892,67895 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47961
diff
changeset
|
340 gil = PyGILState_Ensure(); |
|
52903
3889d9528601
Merged revisions 71058,71149-71150,71212,71214-71216,71222,71225,71234,71237-71238,71240-71241,71243,71249,71251 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51466
diff
changeset
|
341 #endif |
|
16742
db61894b0d6f
_PyObject_Dump(): If argument is NULL, print "NULL" instead of
Barry Warsaw <barry@python.org>
parents:
16741
diff
changeset
|
342 (void)PyObject_Print(op, stderr, 0); |
|
52903
3889d9528601
Merged revisions 71058,71149-71150,71212,71214-71216,71222,71225,71234,71237-71238,71240-71241,71243,71249,71251 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51466
diff
changeset
|
343 #ifdef WITH_THREAD |
|
50257
e4675113d10e
Merged revisions 67654,67676-67677,67681,67692,67725,67746,67748,67761,67784-67785,67787-67788,67802,67832,67848-67849,67859,67862-67864,67880,67882,67885,67889-67892,67895 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47961
diff
changeset
|
344 PyGILState_Release(gil); |
|
52903
3889d9528601
Merged revisions 71058,71149-71150,71212,71214-71216,71222,71225,71234,71237-71238,71240-71241,71243,71249,71251 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51466
diff
changeset
|
345 #endif |
|
36759
83b860acfe95
Use %ld and casts to long for refcount printing, in absense of a universally
Thomas Wouters <thomas@python.org>
parents:
36675
diff
changeset
|
346 /* XXX(twouters) cast refcount to long until %zd is |
|
83b860acfe95
Use %ld and casts to long for refcount printing, in absense of a universally
Thomas Wouters <thomas@python.org>
parents:
36675
diff
changeset
|
347 universally available */ |
|
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
|
348 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
|
349 "type : %s\n" |
|
36759
83b860acfe95
Use %ld and casts to long for refcount printing, in absense of a universally
Thomas Wouters <thomas@python.org>
parents:
36675
diff
changeset
|
350 "refcount: %ld\n" |
|
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
|
351 "address : %p\n", |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
43958
diff
changeset
|
352 Py_TYPE(op)==NULL ? "NULL" : Py_TYPE(op)->tp_name, |
|
36759
83b860acfe95
Use %ld and casts to long for refcount printing, in absense of a universally
Thomas Wouters <thomas@python.org>
parents:
36675
diff
changeset
|
353 (long)op->ob_refcnt, |
|
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
|
354 op); |
|
16742
db61894b0d6f
_PyObject_Dump(): If argument is NULL, print "NULL" instead of
Barry Warsaw <barry@python.org>
parents:
16741
diff
changeset
|
355 } |
|
16165
0a72070eab6e
A few miscellaneous helpers.
Barry Warsaw <barry@python.org>
parents:
16123
diff
changeset
|
356 } |
|
16167
71ceaba3f615
PyObject_Dump(): Use %p format to print the address of the pointer.
Barry Warsaw <barry@python.org>
parents:
16165
diff
changeset
|
357 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
358 PyObject * |
|
13473
31b1f691a7d9
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
359 PyObject_Repr(PyObject *v) |
| 6 | 360 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
361 if (PyErr_CheckSignals()) |
|
284
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
231
diff
changeset
|
362 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
|
363 #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
|
364 if (PyOS_CheckStack()) { |
| 15318 | 365 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
|
366 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
|
367 } |
|
c69ad566aeb1
If USE_STACKCHECK is defined use PyOS_CheckStack() in the repr and str
Guido van Rossum <guido@python.org>
parents:
8111
diff
changeset
|
368 #endif |
|
284
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
231
diff
changeset
|
369 if (v == NULL) |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
370 return PyString_FromString("<NULL>"); |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
43958
diff
changeset
|
371 else if (Py_TYPE(v)->tp_repr == NULL) |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
372 return PyString_FromFormat("<%s object at %p>", |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
43958
diff
changeset
|
373 Py_TYPE(v)->tp_name, v); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
374 else { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
375 PyObject *res; |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
43958
diff
changeset
|
376 res = (*Py_TYPE(v)->tp_repr)(v); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
377 if (res == NULL) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
378 return NULL; |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19294
diff
changeset
|
379 #ifdef Py_USING_UNICODE |
|
13293
e38be8717ec0
changed repr and str to always convert unicode strings
Fredrik Lundh <fredrik@pythonware.com>
parents:
13265
diff
changeset
|
380 if (PyUnicode_Check(res)) { |
|
e38be8717ec0
changed repr and str to always convert unicode strings
Fredrik Lundh <fredrik@pythonware.com>
parents:
13265
diff
changeset
|
381 PyObject* str; |
|
37300
e9e8098f6572
Fixed bug #1459029 - unicode reprs were double-escaped.
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37163
diff
changeset
|
382 str = PyUnicode_AsEncodedString(res, NULL, NULL); |
|
13319
390e5d51ff73
If auto-conversion fails, the Unicode codecs will return NULL.
Marc-André Lemburg <mal@egenix.com>
parents:
13293
diff
changeset
|
383 Py_DECREF(res); |
|
390e5d51ff73
If auto-conversion fails, the Unicode codecs will return NULL.
Marc-André Lemburg <mal@egenix.com>
parents:
13293
diff
changeset
|
384 if (str) |
|
13293
e38be8717ec0
changed repr and str to always convert unicode strings
Fredrik Lundh <fredrik@pythonware.com>
parents:
13265
diff
changeset
|
385 res = str; |
|
13319
390e5d51ff73
If auto-conversion fails, the Unicode codecs will return NULL.
Marc-André Lemburg <mal@egenix.com>
parents:
13293
diff
changeset
|
386 else |
|
390e5d51ff73
If auto-conversion fails, the Unicode codecs will return NULL.
Marc-André Lemburg <mal@egenix.com>
parents:
13293
diff
changeset
|
387 return NULL; |
|
13293
e38be8717ec0
changed repr and str to always convert unicode strings
Fredrik Lundh <fredrik@pythonware.com>
parents:
13265
diff
changeset
|
388 } |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19294
diff
changeset
|
389 #endif |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
390 if (!PyString_Check(res)) { |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
391 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
|
392 "__repr__ returned non-string (type %.200s)", |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
43958
diff
changeset
|
393 Py_TYPE(res)->tp_name); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
394 Py_DECREF(res); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
395 return NULL; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
396 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
397 return res; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
398 } |
| 6 | 399 } |
| 400 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
401 PyObject * |
|
35331
ef11cbc08dab
Change the %s format specifier for str objects so that it returns a
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
34931
diff
changeset
|
402 _PyObject_Str(PyObject *v) |
|
1400
aac8231a715c
* mpzmodule.c: removed redundant mpz_print function.
Guido van Rossum <guido@python.org>
parents:
1361
diff
changeset
|
403 { |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
404 PyObject *res; |
|
35331
ef11cbc08dab
Change the %s format specifier for str objects so that it returns a
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
34931
diff
changeset
|
405 int type_ok; |
|
1400
aac8231a715c
* mpzmodule.c: removed redundant mpz_print function.
Guido van Rossum <guido@python.org>
parents:
1361
diff
changeset
|
406 if (v == NULL) |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
407 return PyString_FromString("<NULL>"); |
|
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
408 if (PyString_CheckExact(v)) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
409 Py_INCREF(v); |
|
1400
aac8231a715c
* mpzmodule.c: removed redundant mpz_print function.
Guido van Rossum <guido@python.org>
parents:
1361
diff
changeset
|
410 return v; |
|
aac8231a715c
* mpzmodule.c: removed redundant mpz_print function.
Guido van Rossum <guido@python.org>
parents:
1361
diff
changeset
|
411 } |
|
35331
ef11cbc08dab
Change the %s format specifier for str objects so that it returns a
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
34931
diff
changeset
|
412 #ifdef Py_USING_UNICODE |
|
ef11cbc08dab
Change the %s format specifier for str objects so that it returns a
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
34931
diff
changeset
|
413 if (PyUnicode_CheckExact(v)) { |
|
ef11cbc08dab
Change the %s format specifier for str objects so that it returns a
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
34931
diff
changeset
|
414 Py_INCREF(v); |
|
ef11cbc08dab
Change the %s format specifier for str objects so that it returns a
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
34931
diff
changeset
|
415 return v; |
|
ef11cbc08dab
Change the %s format specifier for str objects so that it returns a
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
34931
diff
changeset
|
416 } |
|
ef11cbc08dab
Change the %s format specifier for str objects so that it returns a
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
34931
diff
changeset
|
417 #endif |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
43958
diff
changeset
|
418 if (Py_TYPE(v)->tp_str == 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
|
419 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
|
420 |
|
43451
381ed41003e7
tuple.__repr__ did not consider a reference loop as it is not possible from
Brett Cannon <bcannon@gmail.com>
parents:
43378
diff
changeset
|
421 /* It is possible for a type to have a tp_str representation that loops |
|
381ed41003e7
tuple.__repr__ did not consider a reference loop as it is not possible from
Brett Cannon <bcannon@gmail.com>
parents:
43378
diff
changeset
|
422 infinitely. */ |
|
381ed41003e7
tuple.__repr__ did not consider a reference loop as it is not possible from
Brett Cannon <bcannon@gmail.com>
parents:
43378
diff
changeset
|
423 if (Py_EnterRecursiveCall(" while getting the str of an object")) |
|
381ed41003e7
tuple.__repr__ did not consider a reference loop as it is not possible from
Brett Cannon <bcannon@gmail.com>
parents:
43378
diff
changeset
|
424 return NULL; |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
43958
diff
changeset
|
425 res = (*Py_TYPE(v)->tp_str)(v); |
|
43451
381ed41003e7
tuple.__repr__ did not consider a reference loop as it is not possible from
Brett Cannon <bcannon@gmail.com>
parents:
43378
diff
changeset
|
426 Py_LeaveRecursiveCall(); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
427 if (res == NULL) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
428 return NULL; |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
429 type_ok = PyString_Check(res); |
|
35331
ef11cbc08dab
Change the %s format specifier for str objects so that it returns a
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
34931
diff
changeset
|
430 #ifdef Py_USING_UNICODE |
|
ef11cbc08dab
Change the %s format specifier for str objects so that it returns a
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
34931
diff
changeset
|
431 type_ok = type_ok || PyUnicode_Check(res); |
|
ef11cbc08dab
Change the %s format specifier for str objects so that it returns a
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
34931
diff
changeset
|
432 #endif |
|
ef11cbc08dab
Change the %s format specifier for str objects so that it returns a
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
34931
diff
changeset
|
433 if (!type_ok) { |
|
ef11cbc08dab
Change the %s format specifier for str objects so that it returns a
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
34931
diff
changeset
|
434 PyErr_Format(PyExc_TypeError, |
|
ef11cbc08dab
Change the %s format specifier for str objects so that it returns a
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
34931
diff
changeset
|
435 "__str__ returned non-string (type %.200s)", |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
43958
diff
changeset
|
436 Py_TYPE(res)->tp_name); |
|
35331
ef11cbc08dab
Change the %s format specifier for str objects so that it returns a
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
34931
diff
changeset
|
437 Py_DECREF(res); |
|
ef11cbc08dab
Change the %s format specifier for str objects so that it returns a
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
34931
diff
changeset
|
438 return NULL; |
|
ef11cbc08dab
Change the %s format specifier for str objects so that it returns a
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
34931
diff
changeset
|
439 } |
|
ef11cbc08dab
Change the %s format specifier for str objects so that it returns a
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
34931
diff
changeset
|
440 return res; |
|
ef11cbc08dab
Change the %s format specifier for str objects so that it returns a
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
34931
diff
changeset
|
441 } |
|
ef11cbc08dab
Change the %s format specifier for str objects so that it returns a
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
34931
diff
changeset
|
442 |
|
ef11cbc08dab
Change the %s format specifier for str objects so that it returns a
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
34931
diff
changeset
|
443 PyObject * |
|
ef11cbc08dab
Change the %s format specifier for str objects so that it returns a
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
34931
diff
changeset
|
444 PyObject_Str(PyObject *v) |
|
ef11cbc08dab
Change the %s format specifier for str objects so that it returns a
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
34931
diff
changeset
|
445 { |
|
ef11cbc08dab
Change the %s format specifier for str objects so that it returns a
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
34931
diff
changeset
|
446 PyObject *res = _PyObject_Str(v); |
|
ef11cbc08dab
Change the %s format specifier for str objects so that it returns a
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
34931
diff
changeset
|
447 if (res == NULL) |
|
ef11cbc08dab
Change the %s format specifier for str objects so that it returns a
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
34931
diff
changeset
|
448 return NULL; |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19294
diff
changeset
|
449 #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
|
450 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
|
451 PyObject* str; |
|
390e5d51ff73
If auto-conversion fails, the Unicode codecs will return NULL.
Marc-André Lemburg <mal@egenix.com>
parents:
13293
diff
changeset
|
452 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
|
453 Py_DECREF(res); |
|
390e5d51ff73
If auto-conversion fails, the Unicode codecs will return NULL.
Marc-André Lemburg <mal@egenix.com>
parents:
13293
diff
changeset
|
454 if (str) |
|
390e5d51ff73
If auto-conversion fails, the Unicode codecs will return NULL.
Marc-André Lemburg <mal@egenix.com>
parents:
13293
diff
changeset
|
455 res = str; |
|
390e5d51ff73
If auto-conversion fails, the Unicode codecs will return NULL.
Marc-André Lemburg <mal@egenix.com>
parents:
13293
diff
changeset
|
456 else |
|
390e5d51ff73
If auto-conversion fails, the Unicode codecs will return NULL.
Marc-André Lemburg <mal@egenix.com>
parents:
13293
diff
changeset
|
457 return NULL; |
|
390e5d51ff73
If auto-conversion fails, the Unicode codecs will return NULL.
Marc-André Lemburg <mal@egenix.com>
parents:
13293
diff
changeset
|
458 } |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19294
diff
changeset
|
459 #endif |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
460 assert(PyString_Check(res)); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
461 return res; |
|
1400
aac8231a715c
* mpzmodule.c: removed redundant mpz_print function.
Guido van Rossum <guido@python.org>
parents:
1361
diff
changeset
|
462 } |
|
aac8231a715c
* mpzmodule.c: removed redundant mpz_print function.
Guido van Rossum <guido@python.org>
parents:
1361
diff
changeset
|
463 |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19294
diff
changeset
|
464 #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
|
465 PyObject * |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
466 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
|
467 { |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
468 PyObject *res; |
|
34931
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
34248
diff
changeset
|
469 PyObject *func; |
|
36977
95469e97fd90
Fix and test (manually w/xx module) passing NULLs to PyObject_Str() and
Neal Norwitz <nnorwitz@gmail.com>
parents:
36975
diff
changeset
|
470 PyObject *str; |
|
47914
1bc543944daa
Issue 2517: Allow unicode messages in Exceptions again by correctly bypassing the instance dictionary when looking up __unicode__ on new-style classes
Nick Coghlan <ncoghlan@gmail.com>
parents:
47527
diff
changeset
|
471 int unicode_method_found = 0; |
|
34931
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
34248
diff
changeset
|
472 static PyObject *unicodestr; |
|
24122
d332484e394a
Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents:
24121
diff
changeset
|
473 |
|
36975
bdafca2192d3
Fix bug found by Coverity: don't allow NULL argument to PyUnicode_CheckExact
Georg Brandl <georg@python.org>
parents:
36866
diff
changeset
|
474 if (v == NULL) { |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
475 res = PyString_FromString("<NULL>"); |
|
36977
95469e97fd90
Fix and test (manually w/xx module) passing NULLs to PyObject_Str() and
Neal Norwitz <nnorwitz@gmail.com>
parents:
36975
diff
changeset
|
476 if (res == NULL) |
|
95469e97fd90
Fix and test (manually w/xx module) passing NULLs to PyObject_Str() and
Neal Norwitz <nnorwitz@gmail.com>
parents:
36975
diff
changeset
|
477 return NULL; |
|
95469e97fd90
Fix and test (manually w/xx module) passing NULLs to PyObject_Str() and
Neal Norwitz <nnorwitz@gmail.com>
parents:
36975
diff
changeset
|
478 str = PyUnicode_FromEncodedObject(res, NULL, "strict"); |
|
95469e97fd90
Fix and test (manually w/xx module) passing NULLs to PyObject_Str() and
Neal Norwitz <nnorwitz@gmail.com>
parents:
36975
diff
changeset
|
479 Py_DECREF(res); |
|
95469e97fd90
Fix and test (manually w/xx module) passing NULLs to PyObject_Str() and
Neal Norwitz <nnorwitz@gmail.com>
parents:
36975
diff
changeset
|
480 return str; |
|
36975
bdafca2192d3
Fix bug found by Coverity: don't allow NULL argument to PyUnicode_CheckExact
Georg Brandl <georg@python.org>
parents:
36866
diff
changeset
|
481 } else 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
|
482 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
|
483 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
|
484 } |
|
47914
1bc543944daa
Issue 2517: Allow unicode messages in Exceptions again by correctly bypassing the instance dictionary when looking up __unicode__ on new-style classes
Nick Coghlan <ncoghlan@gmail.com>
parents:
47527
diff
changeset
|
485 |
|
1bc543944daa
Issue 2517: Allow unicode messages in Exceptions again by correctly bypassing the instance dictionary when looking up __unicode__ on new-style classes
Nick Coghlan <ncoghlan@gmail.com>
parents:
47527
diff
changeset
|
486 /* Try the __unicode__ method */ |
|
34931
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
34248
diff
changeset
|
487 if (unicodestr == NULL) { |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
488 unicodestr= PyString_InternFromString("__unicode__"); |
|
34931
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
34248
diff
changeset
|
489 if (unicodestr == NULL) |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
34248
diff
changeset
|
490 return NULL; |
|
20554
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20464
diff
changeset
|
491 } |
|
47914
1bc543944daa
Issue 2517: Allow unicode messages in Exceptions again by correctly bypassing the instance dictionary when looking up __unicode__ on new-style classes
Nick Coghlan <ncoghlan@gmail.com>
parents:
47527
diff
changeset
|
492 if (PyInstance_Check(v)) { |
|
1bc543944daa
Issue 2517: Allow unicode messages in Exceptions again by correctly bypassing the instance dictionary when looking up __unicode__ on new-style classes
Nick Coghlan <ncoghlan@gmail.com>
parents:
47527
diff
changeset
|
493 /* We're an instance of a classic class */ |
|
1bc543944daa
Issue 2517: Allow unicode messages in Exceptions again by correctly bypassing the instance dictionary when looking up __unicode__ on new-style classes
Nick Coghlan <ncoghlan@gmail.com>
parents:
47527
diff
changeset
|
494 /* Try __unicode__ from the instance -- alas we have no type */ |
|
1bc543944daa
Issue 2517: Allow unicode messages in Exceptions again by correctly bypassing the instance dictionary when looking up __unicode__ on new-style classes
Nick Coghlan <ncoghlan@gmail.com>
parents:
47527
diff
changeset
|
495 func = PyObject_GetAttr(v, unicodestr); |
|
1bc543944daa
Issue 2517: Allow unicode messages in Exceptions again by correctly bypassing the instance dictionary when looking up __unicode__ on new-style classes
Nick Coghlan <ncoghlan@gmail.com>
parents:
47527
diff
changeset
|
496 if (func != NULL) { |
|
1bc543944daa
Issue 2517: Allow unicode messages in Exceptions again by correctly bypassing the instance dictionary when looking up __unicode__ on new-style classes
Nick Coghlan <ncoghlan@gmail.com>
parents:
47527
diff
changeset
|
497 unicode_method_found = 1; |
|
1bc543944daa
Issue 2517: Allow unicode messages in Exceptions again by correctly bypassing the instance dictionary when looking up __unicode__ on new-style classes
Nick Coghlan <ncoghlan@gmail.com>
parents:
47527
diff
changeset
|
498 res = PyObject_CallFunctionObjArgs(func, NULL); |
|
1bc543944daa
Issue 2517: Allow unicode messages in Exceptions again by correctly bypassing the instance dictionary when looking up __unicode__ on new-style classes
Nick Coghlan <ncoghlan@gmail.com>
parents:
47527
diff
changeset
|
499 Py_DECREF(func); |
|
1bc543944daa
Issue 2517: Allow unicode messages in Exceptions again by correctly bypassing the instance dictionary when looking up __unicode__ on new-style classes
Nick Coghlan <ncoghlan@gmail.com>
parents:
47527
diff
changeset
|
500 } |
|
1bc543944daa
Issue 2517: Allow unicode messages in Exceptions again by correctly bypassing the instance dictionary when looking up __unicode__ on new-style classes
Nick Coghlan <ncoghlan@gmail.com>
parents:
47527
diff
changeset
|
501 else { |
|
1bc543944daa
Issue 2517: Allow unicode messages in Exceptions again by correctly bypassing the instance dictionary when looking up __unicode__ on new-style classes
Nick Coghlan <ncoghlan@gmail.com>
parents:
47527
diff
changeset
|
502 PyErr_Clear(); |
|
1bc543944daa
Issue 2517: Allow unicode messages in Exceptions again by correctly bypassing the instance dictionary when looking up __unicode__ on new-style classes
Nick Coghlan <ncoghlan@gmail.com>
parents:
47527
diff
changeset
|
503 } |
|
34931
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
34248
diff
changeset
|
504 } |
|
16001
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
505 else { |
|
47914
1bc543944daa
Issue 2517: Allow unicode messages in Exceptions again by correctly bypassing the instance dictionary when looking up __unicode__ on new-style classes
Nick Coghlan <ncoghlan@gmail.com>
parents:
47527
diff
changeset
|
506 /* Not a classic class instance, try __unicode__ from type */ |
|
1bc543944daa
Issue 2517: Allow unicode messages in Exceptions again by correctly bypassing the instance dictionary when looking up __unicode__ on new-style classes
Nick Coghlan <ncoghlan@gmail.com>
parents:
47527
diff
changeset
|
507 /* _PyType_Lookup doesn't create a reference */ |
|
1bc543944daa
Issue 2517: Allow unicode messages in Exceptions again by correctly bypassing the instance dictionary when looking up __unicode__ on new-style classes
Nick Coghlan <ncoghlan@gmail.com>
parents:
47527
diff
changeset
|
508 func = _PyType_Lookup(Py_TYPE(v), unicodestr); |
|
1bc543944daa
Issue 2517: Allow unicode messages in Exceptions again by correctly bypassing the instance dictionary when looking up __unicode__ on new-style classes
Nick Coghlan <ncoghlan@gmail.com>
parents:
47527
diff
changeset
|
509 if (func != NULL) { |
|
1bc543944daa
Issue 2517: Allow unicode messages in Exceptions again by correctly bypassing the instance dictionary when looking up __unicode__ on new-style classes
Nick Coghlan <ncoghlan@gmail.com>
parents:
47527
diff
changeset
|
510 unicode_method_found = 1; |
|
1bc543944daa
Issue 2517: Allow unicode messages in Exceptions again by correctly bypassing the instance dictionary when looking up __unicode__ on new-style classes
Nick Coghlan <ncoghlan@gmail.com>
parents:
47527
diff
changeset
|
511 res = PyObject_CallFunctionObjArgs(func, v, NULL); |
|
1bc543944daa
Issue 2517: Allow unicode messages in Exceptions again by correctly bypassing the instance dictionary when looking up __unicode__ on new-style classes
Nick Coghlan <ncoghlan@gmail.com>
parents:
47527
diff
changeset
|
512 } |
|
1bc543944daa
Issue 2517: Allow unicode messages in Exceptions again by correctly bypassing the instance dictionary when looking up __unicode__ on new-style classes
Nick Coghlan <ncoghlan@gmail.com>
parents:
47527
diff
changeset
|
513 else { |
|
1bc543944daa
Issue 2517: Allow unicode messages in Exceptions again by correctly bypassing the instance dictionary when looking up __unicode__ on new-style classes
Nick Coghlan <ncoghlan@gmail.com>
parents:
47527
diff
changeset
|
514 PyErr_Clear(); |
|
1bc543944daa
Issue 2517: Allow unicode messages in Exceptions again by correctly bypassing the instance dictionary when looking up __unicode__ on new-style classes
Nick Coghlan <ncoghlan@gmail.com>
parents:
47527
diff
changeset
|
515 } |
|
1bc543944daa
Issue 2517: Allow unicode messages in Exceptions again by correctly bypassing the instance dictionary when looking up __unicode__ on new-style classes
Nick Coghlan <ncoghlan@gmail.com>
parents:
47527
diff
changeset
|
516 } |
|
1bc543944daa
Issue 2517: Allow unicode messages in Exceptions again by correctly bypassing the instance dictionary when looking up __unicode__ on new-style classes
Nick Coghlan <ncoghlan@gmail.com>
parents:
47527
diff
changeset
|
517 |
|
1bc543944daa
Issue 2517: Allow unicode messages in Exceptions again by correctly bypassing the instance dictionary when looking up __unicode__ on new-style classes
Nick Coghlan <ncoghlan@gmail.com>
parents:
47527
diff
changeset
|
518 /* Didn't find __unicode__ */ |
|
1bc543944daa
Issue 2517: Allow unicode messages in Exceptions again by correctly bypassing the instance dictionary when looking up __unicode__ on new-style classes
Nick Coghlan <ncoghlan@gmail.com>
parents:
47527
diff
changeset
|
519 if (!unicode_method_found) { |
|
34931
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
34248
diff
changeset
|
520 if (PyUnicode_Check(v)) { |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
34248
diff
changeset
|
521 /* For a Unicode subtype that's didn't overwrite __unicode__, |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
34248
diff
changeset
|
522 return a true Unicode object with the same data. */ |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
34248
diff
changeset
|
523 return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(v), |
|
47914
1bc543944daa
Issue 2517: Allow unicode messages in Exceptions again by correctly bypassing the instance dictionary when looking up __unicode__ on new-style classes
Nick Coghlan <ncoghlan@gmail.com>
parents:
47527
diff
changeset
|
524 PyUnicode_GET_SIZE(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
|
525 } |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
526 if (PyString_CheckExact(v)) { |
|
34931
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
34248
diff
changeset
|
527 Py_INCREF(v); |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
34248
diff
changeset
|
528 res = 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
|
529 } |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
530 else { |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
43958
diff
changeset
|
531 if (Py_TYPE(v)->tp_str != NULL) |
|
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
43958
diff
changeset
|
532 res = (*Py_TYPE(v)->tp_str)(v); |
|
20554
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20464
diff
changeset
|
533 else |
|
0336ee5b46d4
SF patch #470578: Fixes to synchronize unicode() and str()
Guido van Rossum <guido@python.org>
parents:
20464
diff
changeset
|
534 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
|
535 } |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
536 } |
|
47914
1bc543944daa
Issue 2517: Allow unicode messages in Exceptions again by correctly bypassing the instance dictionary when looking up __unicode__ on new-style classes
Nick Coghlan <ncoghlan@gmail.com>
parents:
47527
diff
changeset
|
537 |
|
16001
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
538 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
|
539 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
|
540 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
|
541 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
|
542 Py_DECREF(res); |
|
36977
95469e97fd90
Fix and test (manually w/xx module) passing NULLs to PyObject_Str() and
Neal Norwitz <nnorwitz@gmail.com>
parents:
36975
diff
changeset
|
543 res = str; |
|
16001
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
544 } |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
545 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
|
546 } |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19294
diff
changeset
|
547 #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
|
548 |
|
f3740674ac5b
Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents:
15962
diff
changeset
|
549 |
|
23598
2b1d61781d63
Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents:
23542
diff
changeset
|
550 /* Helper to warn about deprecated tp_compare return values. Return: |
|
2b1d61781d63
Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents:
23542
diff
changeset
|
551 -2 for an exception; |
|
2b1d61781d63
Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents:
23542
diff
changeset
|
552 -1 if v < w; |
|
2b1d61781d63
Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents:
23542
diff
changeset
|
553 0 if v == w; |
|
2b1d61781d63
Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents:
23542
diff
changeset
|
554 1 if v > w. |
|
2b1d61781d63
Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents:
23542
diff
changeset
|
555 (This function cannot return 2.) |
|
2b1d61781d63
Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents:
23542
diff
changeset
|
556 */ |
|
2b1d61781d63
Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents:
23542
diff
changeset
|
557 static int |
|
2b1d61781d63
Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents:
23542
diff
changeset
|
558 adjust_tp_compare(int c) |
|
2b1d61781d63
Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents:
23542
diff
changeset
|
559 { |
|
2b1d61781d63
Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents:
23542
diff
changeset
|
560 if (PyErr_Occurred()) { |
|
2b1d61781d63
Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents:
23542
diff
changeset
|
561 if (c != -1 && c != -2) { |
|
2b1d61781d63
Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents:
23542
diff
changeset
|
562 PyObject *t, *v, *tb; |
|
2b1d61781d63
Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents:
23542
diff
changeset
|
563 PyErr_Fetch(&t, &v, &tb); |
|
2b1d61781d63
Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents:
23542
diff
changeset
|
564 if (PyErr_Warn(PyExc_RuntimeWarning, |
|
2b1d61781d63
Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents:
23542
diff
changeset
|
565 "tp_compare didn't return -1 or -2 " |
|
2b1d61781d63
Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents:
23542
diff
changeset
|
566 "for exception") < 0) { |
|
2b1d61781d63
Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents:
23542
diff
changeset
|
567 Py_XDECREF(t); |
|
2b1d61781d63
Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents:
23542
diff
changeset
|
568 Py_XDECREF(v); |
|
2b1d61781d63
Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents:
23542
diff
changeset
|
569 Py_XDECREF(tb); |
|
2b1d61781d63
Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents:
23542
diff
changeset
|
570 } |
|
2b1d61781d63
Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents:
23542
diff
changeset
|
571 else |
|
2b1d61781d63
Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents:
23542
diff
changeset
|
572 PyErr_Restore(t, v, tb); |
|
2b1d61781d63
Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents:
23542
diff
changeset
|
573 } |
|
2b1d61781d63
Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents:
23542
diff
changeset
|
574 return -2; |
|
2b1d61781d63
Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents:
23542
diff
changeset
|
575 } |
|
2b1d61781d63
Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents:
23542
diff
changeset
|
576 else if (c < -1 || c > 1) { |
|
2b1d61781d63
Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents:
23542
diff
changeset
|
577 if (PyErr_Warn(PyExc_RuntimeWarning, |
|
2b1d61781d63
Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents:
23542
diff
changeset
|
578 "tp_compare didn't return -1, 0 or 1") < 0) |
|
2b1d61781d63
Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents:
23542
diff
changeset
|
579 return -2; |
|
2b1d61781d63
Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents:
23542
diff
changeset
|
580 else |
|
2b1d61781d63
Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents:
23542
diff
changeset
|
581 return c < -1 ? -1 : 1; |
|
2b1d61781d63
Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents:
23542
diff
changeset
|
582 } |
|
2b1d61781d63
Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents:
23542
diff
changeset
|
583 else { |
|
2b1d61781d63
Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents:
23542
diff
changeset
|
584 assert(c >= -1 && c <= 1); |
|
2b1d61781d63
Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents:
23542
diff
changeset
|
585 return c; |
|
2b1d61781d63
Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents:
23542
diff
changeset
|
586 } |
|
2b1d61781d63
Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents:
23542
diff
changeset
|
587 } |
|
2b1d61781d63
Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents:
23542
diff
changeset
|
588 |
|
2b1d61781d63
Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents:
23542
diff
changeset
|
589 |
|
16236
e5d59a9dbfc0
Check the Py_TPFLAGS_HAVE_RICHCOMPARE flag before using the
Guido van Rossum <guido@python.org>
parents:
16197
diff
changeset
|
590 /* 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
|
591 #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
|
592 ? (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
|
593 |
|
15941
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
594 /* Map rich comparison operators to their swapped version, e.g. LT --> GT */ |
|
33571
a5682bc73e04
Remove 'extern' declaration for _Py_SwappedOp.
Brett Cannon <bcannon@gmail.com>
parents:
33541
diff
changeset
|
595 int _Py_SwappedOp[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE}; |
|
15941
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
596 |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
597 /* 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
|
598 NULL for exception; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
599 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
|
600 undefined; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
601 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
|
602 (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
|
603 */ |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
604 static PyObject * |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
605 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
|
606 { |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
607 richcmpfunc f; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
608 PyObject *res; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
609 |
|
20140
225f17ec8932
Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents:
20002
diff
changeset
|
610 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
|
611 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
|
612 (f = RICHCOMPARE(w->ob_type)) != NULL) { |
|
33541
781aef974463
A static swapped_op[] array was defined in 3 different C files, & I think
Tim Peters <tim.peters@gmail.com>
parents:
33489
diff
changeset
|
613 res = (*f)(w, v, _Py_SwappedOp[op]); |
|
20140
225f17ec8932
Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents:
20002
diff
changeset
|
614 if (res != Py_NotImplemented) |
|
225f17ec8932
Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents:
20002
diff
changeset
|
615 return res; |
|
225f17ec8932
Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents:
20002
diff
changeset
|
616 Py_DECREF(res); |
|
225f17ec8932
Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents:
20002
diff
changeset
|
617 } |
|
16236
e5d59a9dbfc0
Check the Py_TPFLAGS_HAVE_RICHCOMPARE flag before using the
Guido van Rossum <guido@python.org>
parents:
16197
diff
changeset
|
618 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
|
619 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
|
620 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
|
621 return res; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
622 Py_DECREF(res); |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
623 } |
|
16236
e5d59a9dbfc0
Check the Py_TPFLAGS_HAVE_RICHCOMPARE flag before using the
Guido van Rossum <guido@python.org>
parents:
16197
diff
changeset
|
624 if ((f = RICHCOMPARE(w->ob_type)) != NULL) { |
|
33541
781aef974463
A static swapped_op[] array was defined in 3 different C files, & I think
Tim Peters <tim.peters@gmail.com>
parents:
33489
diff
changeset
|
625 return (*f)(w, v, _Py_SwappedOp[op]); |
|
15941
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
626 } |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
627 res = Py_NotImplemented; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
628 Py_INCREF(res); |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
629 return res; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
630 } |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
631 |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
632 /* 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
|
633 -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
|
634 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
|
635 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
|
636 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
|
637 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
|
638 */ |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
639 static int |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
640 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
|
641 { |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
642 PyObject *res; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
643 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
|
644 |
|
16236
e5d59a9dbfc0
Check the Py_TPFLAGS_HAVE_RICHCOMPARE flag before using the
Guido van Rossum <guido@python.org>
parents:
16197
diff
changeset
|
645 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
|
646 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
|
647 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
|
648 if (res == NULL) |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
649 return -1; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
650 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
|
651 Py_DECREF(res); |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
652 return 2; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
653 } |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
654 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
|
655 Py_DECREF(res); |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
656 return ok; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
657 } |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
658 |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
659 /* 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
|
660 -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
|
661 -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
|
662 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
|
663 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
|
664 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
|
665 */ |
|
15726
db9f1893be01
Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
666 static int |
|
15941
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
667 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
|
668 { |
|
15962
f1751abf771f
Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents:
15952
diff
changeset
|
669 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
|
670 /* 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
|
671 {Py_EQ, 0}, |
|
f1751abf771f
Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents:
15952
diff
changeset
|
672 {Py_LT, -1}, |
|
f1751abf771f
Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents:
15952
diff
changeset
|
673 {Py_GT, 1}, |
|
f1751abf771f
Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents:
15952
diff
changeset
|
674 }; |
|
f1751abf771f
Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents:
15952
diff
changeset
|
675 int i; |
|
f1751abf771f
Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents:
15952
diff
changeset
|
676 |
|
16236
e5d59a9dbfc0
Check the Py_TPFLAGS_HAVE_RICHCOMPARE flag before using the
Guido van Rossum <guido@python.org>
parents:
16197
diff
changeset
|
677 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
|
678 return 2; /* Shortcut */ |
|
15962
f1751abf771f
Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents:
15952
diff
changeset
|
679 |
|
f1751abf771f
Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents:
15952
diff
changeset
|
680 for (i = 0; i < 3; i++) { |
|
f1751abf771f
Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents:
15952
diff
changeset
|
681 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
|
682 case -1: |
|
17698
aad0adf806ad
SF bug #422108 - Error in rich comparisons.
Tim Peters <tim.peters@gmail.com>
parents:
17688
diff
changeset
|
683 return -2; |
|
15962
f1751abf771f
Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents:
15952
diff
changeset
|
684 case 1: |
|
f1751abf771f
Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents:
15952
diff
changeset
|
685 return tries[i].outcome; |
|
f1751abf771f
Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents:
15952
diff
changeset
|
686 } |
|
15941
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
687 } |
|
15962
f1751abf771f
Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents:
15952
diff
changeset
|
688 |
|
f1751abf771f
Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents:
15952
diff
changeset
|
689 return 2; |
|
15941
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
690 } |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
691 |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
692 /* 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
|
693 -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
|
694 -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
|
695 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
|
696 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
|
697 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
|
698 */ |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
699 static int |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
700 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
|
701 { |
|
db9f1893be01
Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
702 int c; |
|
15941
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
703 cmpfunc f; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
704 |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
705 /* 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
|
706 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
|
707 |
|
19970
f761cab28c1c
Hopefully fix 3-way comparisons. This unfortunately adds yet another
Guido van Rossum <guido@python.org>
parents:
19932
diff
changeset
|
708 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
|
709 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
|
710 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
|
711 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
|
712 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
|
713 |
|
19970
f761cab28c1c
Hopefully fix 3-way comparisons. This unfortunately adds yet another
Guido van Rossum <guido@python.org>
parents:
19932
diff
changeset
|
714 /* 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
|
715 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
|
716 c = (*f)(v, w); |
|
23598
2b1d61781d63
Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents:
23542
diff
changeset
|
717 return adjust_tp_compare(c); |
|
19970
f761cab28c1c
Hopefully fix 3-way comparisons. This unfortunately adds yet another
Guido van Rossum <guido@python.org>
parents:
19932
diff
changeset
|
718 } |
|
f761cab28c1c
Hopefully fix 3-way comparisons. This unfortunately adds yet another
Guido van Rossum <guido@python.org>
parents:
19932
diff
changeset
|
719 |
|
f761cab28c1c
Hopefully fix 3-way comparisons. This unfortunately adds yet another
Guido van Rossum <guido@python.org>
parents:
19932
diff
changeset
|
720 /* 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
|
721 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
|
722 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
|
723 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
|
724 |
|
34248
86c3a7c6b00a
Dima Dorfman's patch for coercion/comparison of C types (patch #995939), with
Armin Rigo <arigo@tunes.org>
parents:
33571
diff
changeset
|
725 /* If we're here, v and w, |
|
86c3a7c6b00a
Dima Dorfman's patch for coercion/comparison of C types (patch #995939), with
Armin Rigo <arigo@tunes.org>
parents:
33571
diff
changeset
|
726 a) are not instances; |
|
86c3a7c6b00a
Dima Dorfman's patch for coercion/comparison of C types (patch #995939), with
Armin Rigo <arigo@tunes.org>
parents:
33571
diff
changeset
|
727 b) have different types or a type without tp_compare; and |
|
86c3a7c6b00a
Dima Dorfman's patch for coercion/comparison of C types (patch #995939), with
Armin Rigo <arigo@tunes.org>
parents:
33571
diff
changeset
|
728 c) don't have a user-defined tp_compare. |
|
86c3a7c6b00a
Dima Dorfman's patch for coercion/comparison of C types (patch #995939), with
Armin Rigo <arigo@tunes.org>
parents:
33571
diff
changeset
|
729 tp_compare implementations in C assume that both arguments |
|
86c3a7c6b00a
Dima Dorfman's patch for coercion/comparison of C types (patch #995939), with
Armin Rigo <arigo@tunes.org>
parents:
33571
diff
changeset
|
730 have their type, so we give up if the coercion fails or if |
|
86c3a7c6b00a
Dima Dorfman's patch for coercion/comparison of C types (patch #995939), with
Armin Rigo <arigo@tunes.org>
parents:
33571
diff
changeset
|
731 it yields types which are still incompatible (which can |
|
86c3a7c6b00a
Dima Dorfman's patch for coercion/comparison of C types (patch #995939), with
Armin Rigo <arigo@tunes.org>
parents:
33571
diff
changeset
|
732 happen with a user-defined nb_coerce). |
|
86c3a7c6b00a
Dima Dorfman's patch for coercion/comparison of C types (patch #995939), with
Armin Rigo <arigo@tunes.org>
parents:
33571
diff
changeset
|
733 */ |
|
15941
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
734 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
|
735 if (c < 0) |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
736 return -2; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
737 if (c > 0) |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
738 return 2; |
|
34248
86c3a7c6b00a
Dima Dorfman's patch for coercion/comparison of C types (patch #995939), with
Armin Rigo <arigo@tunes.org>
parents:
33571
diff
changeset
|
739 f = v->ob_type->tp_compare; |
|
86c3a7c6b00a
Dima Dorfman's patch for coercion/comparison of C types (patch #995939), with
Armin Rigo <arigo@tunes.org>
parents:
33571
diff
changeset
|
740 if (f != NULL && f == w->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
|
741 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
|
742 Py_DECREF(v); |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
743 Py_DECREF(w); |
|
23598
2b1d61781d63
Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents:
23542
diff
changeset
|
744 return adjust_tp_compare(c); |
|
15941
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
745 } |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
746 |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
747 /* No comparison defined */ |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
748 Py_DECREF(v); |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
749 Py_DECREF(w); |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
750 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
|
751 } |
|
db9f1893be01
Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
752 |
|
15941
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
753 /* 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
|
754 -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
|
755 -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
|
756 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
|
757 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
|
758 */ |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
759 static int |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
760 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
|
761 { |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
762 int c; |
|
35999
9541369b0dca
Add const to several API functions that take char *.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
35331
diff
changeset
|
763 const 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
|
764 |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
765 if (v->ob_type == w->ob_type) { |
| 16062 | 766 /* When comparing these pointers, they must be cast to |
| 767 * integer types (i.e. Py_uintptr_t, our spelling of C9X's | |
| 768 * uintptr_t). ANSI specifies that pointer compares other | |
| 769 * than == and != to non-related structures are undefined. | |
| 770 */ | |
|
16061
6b647a5d5b7e
default_3way_compare(): When comparing the pointers, they must be cast
Barry Warsaw <barry@python.org>
parents:
16002
diff
changeset
|
771 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
|
772 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
|
773 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
|
774 } |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
775 |
|
16123
f997ded4e219
New special case in comparisons: None is smaller than any other object
Guido van Rossum <guido@python.org>
parents:
16108
diff
changeset
|
776 /* 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
|
777 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
|
778 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
|
779 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
|
780 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
|
781 |
|
27938
c9401a36bae7
default_3way_compare(): use PyNumber_Check(), rather than testing for
Guido van Rossum <guido@python.org>
parents:
27702
diff
changeset
|
782 /* different type: compare type names; numbers are smaller */ |
|
c9401a36bae7
default_3way_compare(): use PyNumber_Check(), rather than testing for
Guido van Rossum <guido@python.org>
parents:
27702
diff
changeset
|
783 if (PyNumber_Check(v)) |
|
16108
28d8ccbe237f
Once again, numeric-smelling objects compare smaller than non-numeric
Guido van Rossum <guido@python.org>
parents:
16079
diff
changeset
|
784 vname = ""; |
|
28d8ccbe237f
Once again, numeric-smelling objects compare smaller than non-numeric
Guido van Rossum <guido@python.org>
parents:
16079
diff
changeset
|
785 else |
|
28d8ccbe237f
Once again, numeric-smelling objects compare smaller than non-numeric
Guido van Rossum <guido@python.org>
parents:
16079
diff
changeset
|
786 vname = v->ob_type->tp_name; |
|
27938
c9401a36bae7
default_3way_compare(): use PyNumber_Check(), rather than testing for
Guido van Rossum <guido@python.org>
parents:
27702
diff
changeset
|
787 if (PyNumber_Check(w)) |
|
16108
28d8ccbe237f
Once again, numeric-smelling objects compare smaller than non-numeric
Guido van Rossum <guido@python.org>
parents:
16079
diff
changeset
|
788 wname = ""; |
|
28d8ccbe237f
Once again, numeric-smelling objects compare smaller than non-numeric
Guido van Rossum <guido@python.org>
parents:
16079
diff
changeset
|
789 else |
|
28d8ccbe237f
Once again, numeric-smelling objects compare smaller than non-numeric
Guido van Rossum <guido@python.org>
parents:
16079
diff
changeset
|
790 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
|
791 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
|
792 if (c < 0) |
|
28d8ccbe237f
Once again, numeric-smelling objects compare smaller than non-numeric
Guido van Rossum <guido@python.org>
parents:
16079
diff
changeset
|
793 return -1; |
|
28d8ccbe237f
Once again, numeric-smelling objects compare smaller than non-numeric
Guido van Rossum <guido@python.org>
parents:
16079
diff
changeset
|
794 if (c > 0) |
|
28d8ccbe237f
Once again, numeric-smelling objects compare smaller than non-numeric
Guido van Rossum <guido@python.org>
parents:
16079
diff
changeset
|
795 return 1; |
|
28d8ccbe237f
Once again, numeric-smelling objects compare smaller than non-numeric
Guido van Rossum <guido@python.org>
parents:
16079
diff
changeset
|
796 /* 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
|
797 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
|
798 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
|
799 } |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
800 |
|
17698
aad0adf806ad
SF bug #422108 - Error in rich comparisons.
Tim Peters <tim.peters@gmail.com>
parents:
17688
diff
changeset
|
801 /* Do a 3-way comparison, by hook or by crook. Return: |
|
23598
2b1d61781d63
Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents:
23542
diff
changeset
|
802 -2 for an exception (but see below); |
|
20863
d6ff1daae398
No code change -- just trying to document the return conditions for all
Tim Peters <tim.peters@gmail.com>
parents:
20612
diff
changeset
|
803 -1 if v < w; |
|
17698
aad0adf806ad
SF bug #422108 - Error in rich comparisons.
Tim Peters <tim.peters@gmail.com>
parents:
17688
diff
changeset
|
804 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
|
805 1 if v > w; |
|
23598
2b1d61781d63
Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents:
23542
diff
changeset
|
806 BUT: if the object implements a tp_compare function, it returns |
|
18063
bbac2a6a5d16
Patch #424475: Speed-up tp_compare usage, by special-casing the common
Martin v. Löwis <martin@v.loewis.de>
parents:
17781
diff
changeset
|
807 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
|
808 */ |
|
15726
db9f1893be01
Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
809 static int |
|
13473
31b1f691a7d9
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
810 do_cmp(PyObject *v, PyObject *w) |
|
2124
36847b8b575d
properly implement cmp() for class instances
Guido van Rossum <guido@python.org>
parents:
2113
diff
changeset
|
811 { |
|
15726
db9f1893be01
Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
812 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
|
813 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
|
814 |
|
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
|
815 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
|
816 && (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
|
817 c = (*f)(v, w); |
|
23598
2b1d61781d63
Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents:
23542
diff
changeset
|
818 if (PyInstance_Check(v)) { |
|
2b1d61781d63
Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents:
23542
diff
changeset
|
819 /* Instance tp_compare has a different signature. |
|
2b1d61781d63
Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents:
23542
diff
changeset
|
820 But if it returns undefined we fall through. */ |
|
2b1d61781d63
Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents:
23542
diff
changeset
|
821 if (c != 2) |
|
2b1d61781d63
Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents:
23542
diff
changeset
|
822 return c; |
| 23599 | 823 /* Else fall through to try_rich_to_3way_compare() */ |
|
23598
2b1d61781d63
Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents:
23542
diff
changeset
|
824 } |
|
2b1d61781d63
Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents:
23542
diff
changeset
|
825 else |
|
2b1d61781d63
Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents:
23542
diff
changeset
|
826 return adjust_tp_compare(c); |
|
19293
1e87db0ca248
Update to MvL's patch #424475 to avoid returning 2 when tp_compare
Guido van Rossum <guido@python.org>
parents:
19040
diff
changeset
|
827 } |
|
23598
2b1d61781d63
Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents:
23542
diff
changeset
|
828 /* We only get here if one of the following is true: |
|
2b1d61781d63
Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents:
23542
diff
changeset
|
829 a) v and w have different types |
|
2b1d61781d63
Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents:
23542
diff
changeset
|
830 b) v and w have the same type, which doesn't have tp_compare |
|
2b1d61781d63
Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents:
23542
diff
changeset
|
831 c) v and w are instances, and either __cmp__ is not defined or |
|
2b1d61781d63
Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents:
23542
diff
changeset
|
832 __cmp__ returns NotImplemented |
|
2b1d61781d63
Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents:
23542
diff
changeset
|
833 */ |
|
15941
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
834 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
|
835 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
|
836 return c; |
|
15941
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
837 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
|
838 if (c < 2) |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
839 return c; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
840 return default_3way_compare(v, w); |
|
2124
36847b8b575d
properly implement cmp() for class instances
Guido van Rossum <guido@python.org>
parents:
2113
diff
changeset
|
841 } |
|
36847b8b575d
properly implement cmp() for class instances
Guido van Rossum <guido@python.org>
parents:
2113
diff
changeset
|
842 |
|
20863
d6ff1daae398
No code change -- just trying to document the return conditions for all
Tim Peters <tim.peters@gmail.com>
parents:
20612
diff
changeset
|
843 /* 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
|
844 -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
|
845 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
|
846 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
|
847 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
|
848 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
|
849 */ |
| 6 | 850 int |
|
13473
31b1f691a7d9
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
851 PyObject_Compare(PyObject *v, PyObject *w) |
| 6 | 852 { |
|
12406
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
853 int result; |
|
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
854 |
|
5610
10e9cfa010f0
PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents:
5560
diff
changeset
|
855 if (v == NULL || w == NULL) { |
|
10e9cfa010f0
PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents:
5560
diff
changeset
|
856 PyErr_BadInternalCall(); |
|
10e9cfa010f0
PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents:
5560
diff
changeset
|
857 return -1; |
|
10e9cfa010f0
PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents:
5560
diff
changeset
|
858 } |
| 6 | 859 if (v == w) |
| 860 return 0; | |
|
30801
c42c40034536
Deleting cyclic object comparison.
Armin Rigo <arigo@tunes.org>
parents:
30159
diff
changeset
|
861 if (Py_EnterRecursiveCall(" in cmp")) |
|
c42c40034536
Deleting cyclic object comparison.
Armin Rigo <arigo@tunes.org>
parents:
30159
diff
changeset
|
862 return -1; |
|
c42c40034536
Deleting cyclic object comparison.
Armin Rigo <arigo@tunes.org>
parents:
30159
diff
changeset
|
863 result = do_cmp(v, w); |
|
c42c40034536
Deleting cyclic object comparison.
Armin Rigo <arigo@tunes.org>
parents:
30159
diff
changeset
|
864 Py_LeaveRecursiveCall(); |
|
15941
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
865 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
|
866 } |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
867 |
|
20863
d6ff1daae398
No code change -- just trying to document the return conditions for all
Tim Peters <tim.peters@gmail.com>
parents:
20612
diff
changeset
|
868 /* 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
|
869 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
|
870 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
|
871 { |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
872 PyObject *result; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
873 switch (op) { |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
874 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
|
875 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
|
876 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
|
877 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
|
878 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
|
879 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
|
880 } |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
881 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
|
882 Py_INCREF(result); |
|
12406
ec6b6ca5d111
Fix PR#7 comparisons of recursive objects
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12327
diff
changeset
|
883 return result; |
| 6 | 884 } |
|
24122
d332484e394a
Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents:
24121
diff
changeset
|
885 |
|
20863
d6ff1daae398
No code change -- just trying to document the return conditions for all
Tim Peters <tim.peters@gmail.com>
parents:
20612
diff
changeset
|
886 /* 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
|
887 Return |
|
d6ff1daae398
No code change -- just trying to document the return conditions for all
Tim Peters <tim.peters@gmail.com>
parents:
20612
diff
changeset
|
888 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
|
889 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
|
890 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
|
891 */ |
|
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
|
892 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
|
893 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
|
894 { |
|
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
|
895 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
|
896 |
|
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
|
897 c = try_3way_compare(v, w); |
|
45860
50771a3fad2c
Add py3k warnings for object, type, cell and dict comparisons. This should resolve issue2342 and partly resolve issue2373.
Steven Bethard <steven.bethard@gmail.com>
parents:
44915
diff
changeset
|
898 if (c >= 2) { |
|
50771a3fad2c
Add py3k warnings for object, type, cell and dict comparisons. This should resolve issue2342 and partly resolve issue2373.
Steven Bethard <steven.bethard@gmail.com>
parents:
44915
diff
changeset
|
899 |
|
50771a3fad2c
Add py3k warnings for object, type, cell and dict comparisons. This should resolve issue2342 and partly resolve issue2373.
Steven Bethard <steven.bethard@gmail.com>
parents:
44915
diff
changeset
|
900 /* Py3K warning if types are not equal and comparison isn't == or != */ |
|
50771a3fad2c
Add py3k warnings for object, type, cell and dict comparisons. This should resolve issue2342 and partly resolve issue2373.
Steven Bethard <steven.bethard@gmail.com>
parents:
44915
diff
changeset
|
901 if (Py_Py3kWarningFlag && |
|
46075
84bb7701efba
Make Py3k warnings consistent w.r.t. punctuation; also respect the
Georg Brandl <georg@python.org>
parents:
45984
diff
changeset
|
902 v->ob_type != w->ob_type && op != Py_EQ && op != Py_NE && |
|
46573
aef3501d8e77
Use PyErr_WarnPy3k throughout
Benjamin Peterson <benjamin@python.org>
parents:
46107
diff
changeset
|
903 PyErr_WarnEx(PyExc_DeprecationWarning, |
|
46075
84bb7701efba
Make Py3k warnings consistent w.r.t. punctuation; also respect the
Georg Brandl <georg@python.org>
parents:
45984
diff
changeset
|
904 "comparing unequal types not supported " |
|
46573
aef3501d8e77
Use PyErr_WarnPy3k throughout
Benjamin Peterson <benjamin@python.org>
parents:
46107
diff
changeset
|
905 "in 3.x", 1) < 0) { |
|
45860
50771a3fad2c
Add py3k warnings for object, type, cell and dict comparisons. This should resolve issue2342 and partly resolve issue2373.
Steven Bethard <steven.bethard@gmail.com>
parents:
44915
diff
changeset
|
906 return NULL; |
|
50771a3fad2c
Add py3k warnings for object, type, cell and dict comparisons. This should resolve issue2342 and partly resolve issue2373.
Steven Bethard <steven.bethard@gmail.com>
parents:
44915
diff
changeset
|
907 } |
|
50771a3fad2c
Add py3k warnings for object, type, cell and dict comparisons. This should resolve issue2342 and partly resolve issue2373.
Steven Bethard <steven.bethard@gmail.com>
parents:
44915
diff
changeset
|
908 |
|
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
|
909 c = default_3way_compare(v, w); |
|
45860
50771a3fad2c
Add py3k warnings for object, type, cell and dict comparisons. This should resolve issue2342 and partly resolve issue2373.
Steven Bethard <steven.bethard@gmail.com>
parents:
44915
diff
changeset
|
910 } |
|
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
|
911 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
|
912 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
|
913 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
|
914 } |
| 6 | 915 |
|
20863
d6ff1daae398
No code change -- just trying to document the return conditions for all
Tim Peters <tim.peters@gmail.com>
parents:
20612
diff
changeset
|
916 /* 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
|
917 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
|
918 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
|
919 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
|
920 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
|
921 */ |
|
16079
c4355905b044
Remove a smelly export.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
16062
diff
changeset
|
922 static PyObject * |
|
15941
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
923 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
|
924 { |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
925 PyObject *res; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
926 |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
927 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
|
928 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
|
929 return res; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
930 Py_DECREF(res); |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
931 |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
932 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
|
933 } |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
934 |
|
20863
d6ff1daae398
No code change -- just trying to document the return conditions for all
Tim Peters <tim.peters@gmail.com>
parents:
20612
diff
changeset
|
935 /* Return: |
|
d6ff1daae398
No code change -- just trying to document the return conditions for all
Tim Peters <tim.peters@gmail.com>
parents:
20612
diff
changeset
|
936 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
|
937 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
|
938 (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
|
939 */ |
|
15941
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
940 PyObject * |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
941 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
|
942 { |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
943 PyObject *res; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
944 |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
945 assert(Py_LT <= op && op <= Py_GE); |
|
30801
c42c40034536
Deleting cyclic object comparison.
Armin Rigo <arigo@tunes.org>
parents:
30159
diff
changeset
|
946 if (Py_EnterRecursiveCall(" in cmp")) |
|
c42c40034536
Deleting cyclic object comparison.
Armin Rigo <arigo@tunes.org>
parents:
30159
diff
changeset
|
947 return NULL; |
|
15941
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
948 |
|
30801
c42c40034536
Deleting cyclic object comparison.
Armin Rigo <arigo@tunes.org>
parents:
30159
diff
changeset
|
949 /* If the types are equal, and not old-style instances, try to |
|
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
|
950 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
|
951 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
|
952 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
|
953 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
|
954 /* 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
|
955 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
|
956 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
|
957 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
|
958 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
|
959 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
|
960 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
|
961 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
|
962 } |
|
f54828eb8e07
Rehabilitated the fast-path richcmp code, and sped it up. It wasn't
Tim Peters <tim.peters@gmail.com>
parents:
20863
diff
changeset
|
963 /* 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
|
964 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
|
965 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
|
966 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
|
967 int c = (*fcmp)(v, w); |
|
23598
2b1d61781d63
Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents:
23542
diff
changeset
|
968 c = adjust_tp_compare(c); |
|
2b1d61781d63
Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents:
23542
diff
changeset
|
969 if (c == -2) { |
|
20865
f54828eb8e07
Rehabilitated the fast-path richcmp code, and sped it up. It wasn't
Tim Peters <tim.peters@gmail.com>
parents:
20863
diff
changeset
|
970 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
|
971 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
|
972 } |
|
f54828eb8e07
Rehabilitated the fast-path richcmp code, and sped it up. It wasn't
Tim Peters <tim.peters@gmail.com>
parents:
20863
diff
changeset
|
973 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
|
974 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
|
975 } |
|
15962
f1751abf771f
Deal properly (?) with comparing recursive datastructures.
Guido van Rossum <guido@python.org>
parents:
15952
diff
changeset
|
976 } |
|
20865
f54828eb8e07
Rehabilitated the fast-path richcmp code, and sped it up. It wasn't
Tim Peters <tim.peters@gmail.com>
parents:
20863
diff
changeset
|
977 |
|
f54828eb8e07
Rehabilitated the fast-path richcmp code, and sped it up. It wasn't
Tim Peters <tim.peters@gmail.com>
parents:
20863
diff
changeset
|
978 /* 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
|
979 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
|
980 Done: |
|
30801
c42c40034536
Deleting cyclic object comparison.
Armin Rigo <arigo@tunes.org>
parents:
30159
diff
changeset
|
981 Py_LeaveRecursiveCall(); |
|
15941
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
982 return res; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
983 } |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
984 |
|
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
|
985 /* 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
|
986 int |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
987 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
|
988 { |
|
31736
6fcbf6ac699a
Add identity shortcut to PyObject_RichCompareBool.
Raymond Hettinger <python@rcn.com>
parents:
31433
diff
changeset
|
989 PyObject *res; |
|
15941
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
990 int ok; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
991 |
|
31736
6fcbf6ac699a
Add identity shortcut to PyObject_RichCompareBool.
Raymond Hettinger <python@rcn.com>
parents:
31433
diff
changeset
|
992 /* Quick result when objects are the same. |
|
6fcbf6ac699a
Add identity shortcut to PyObject_RichCompareBool.
Raymond Hettinger <python@rcn.com>
parents:
31433
diff
changeset
|
993 Guarantees that identity implies equality. */ |
|
6fcbf6ac699a
Add identity shortcut to PyObject_RichCompareBool.
Raymond Hettinger <python@rcn.com>
parents:
31433
diff
changeset
|
994 if (v == w) { |
|
6fcbf6ac699a
Add identity shortcut to PyObject_RichCompareBool.
Raymond Hettinger <python@rcn.com>
parents:
31433
diff
changeset
|
995 if (op == Py_EQ) |
|
6fcbf6ac699a
Add identity shortcut to PyObject_RichCompareBool.
Raymond Hettinger <python@rcn.com>
parents:
31433
diff
changeset
|
996 return 1; |
|
6fcbf6ac699a
Add identity shortcut to PyObject_RichCompareBool.
Raymond Hettinger <python@rcn.com>
parents:
31433
diff
changeset
|
997 else if (op == Py_NE) |
|
6fcbf6ac699a
Add identity shortcut to PyObject_RichCompareBool.
Raymond Hettinger <python@rcn.com>
parents:
31433
diff
changeset
|
998 return 0; |
|
6fcbf6ac699a
Add identity shortcut to PyObject_RichCompareBool.
Raymond Hettinger <python@rcn.com>
parents:
31433
diff
changeset
|
999 } |
|
6fcbf6ac699a
Add identity shortcut to PyObject_RichCompareBool.
Raymond Hettinger <python@rcn.com>
parents:
31433
diff
changeset
|
1000 |
|
6fcbf6ac699a
Add identity shortcut to PyObject_RichCompareBool.
Raymond Hettinger <python@rcn.com>
parents:
31433
diff
changeset
|
1001 res = PyObject_RichCompare(v, w, op); |
|
15941
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
1002 if (res == NULL) |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
1003 return -1; |
|
25013
68627e0acd15
Speedup for PyObject_RichCompareBool(): PyObject_RichCompare() almost
Guido van Rossum <guido@python.org>
parents:
24897
diff
changeset
|
1004 if (PyBool_Check(res)) |
|
68627e0acd15
Speedup for PyObject_RichCompareBool(): PyObject_RichCompare() almost
Guido van Rossum <guido@python.org>
parents:
24897
diff
changeset
|
1005 ok = (res == Py_True); |
|
68627e0acd15
Speedup for PyObject_RichCompareBool(): PyObject_RichCompare() almost
Guido van Rossum <guido@python.org>
parents:
24897
diff
changeset
|
1006 else |
|
68627e0acd15
Speedup for PyObject_RichCompareBool(): PyObject_RichCompare() almost
Guido van Rossum <guido@python.org>
parents:
24897
diff
changeset
|
1007 ok = PyObject_IsTrue(res); |
|
15941
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
1008 Py_DECREF(res); |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
1009 return ok; |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
1010 } |
|
13141
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
1011 |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
1012 /* Set of hash utility functions to help maintaining the invariant that |
| 31738 | 1013 if a==b then hash(a)==hash(b) |
|
13141
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
1014 |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
1015 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
|
1016 */ |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
1017 |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
1018 long |
|
13473
31b1f691a7d9
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
1019 _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
|
1020 { |
|
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
|
1021 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
|
1022 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
|
1023 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
|
1024 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
|
1025 /* 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
|
1026 * 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
|
1027 * 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
|
1028 */ |
|
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13915
diff
changeset
|
1029 |
|
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13915
diff
changeset
|
1030 fractpart = modf(v, &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
|
1031 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
|
1032 /* This must return the same hash as an equal int or long. */ |
|
51466
5849a4f904a2
Merged revisions 69433,69436 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
50370
diff
changeset
|
1033 if (intpart > LONG_MAX/2 || -intpart > LONG_MAX/2) { |
|
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
|
1034 /* 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
|
1035 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
|
1036 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
|
1037 /* 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
|
1038 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
|
1039 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
|
1040 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
|
1041 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
|
1042 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
|
1043 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
|
1044 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
|
1045 } |
|
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13915
diff
changeset
|
1046 /* 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
|
1047 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
|
1048 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
|
1049 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
|
1050 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
|
1051 } |
|
5ada9fa54801
Fix for http://sourceforge.net/bugs/?func=detailbug&bug_id=111866&group_id=5470.
Tim Peters <tim.peters@gmail.com>
parents:
13915
diff
changeset
|
1052 /* 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
|
1053 * 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
|
1054 * 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
|
1055 * 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
|
1056 * 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
|
1057 * 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
|
1058 * 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
|
1059 * 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
|
1060 * 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
|
1061 * 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
|
1062 * 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
|
1063 */ |
|
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
|
1064 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
|
1065 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
|
1066 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
|
1067 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
|
1068 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
|
1069 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
|
1070 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
|
1071 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
|
1072 } |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
1073 |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
1074 long |
|
13473
31b1f691a7d9
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
1075 _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
|
1076 { |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
1077 #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
|
1078 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
|
1079 #else |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
1080 /* 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
|
1081 PyObject* longobj; |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
1082 long x; |
|
24122
d332484e394a
Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents:
24121
diff
changeset
|
1083 |
|
13141
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
1084 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
|
1085 x = -1; |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
1086 goto finally; |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
1087 } |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
1088 x = PyObject_Hash(longobj); |
|
24122
d332484e394a
Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents:
24121
diff
changeset
|
1089 |
|
13141
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
1090 finally: |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
1091 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
|
1092 return x; |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
1093 #endif |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
1094 } |
|
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
1095 |
|
47961
f9b80a820a25
Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents:
47914
diff
changeset
|
1096 long |
|
f9b80a820a25
Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents:
47914
diff
changeset
|
1097 PyObject_HashNotImplemented(PyObject *self) |
|
f9b80a820a25
Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents:
47914
diff
changeset
|
1098 { |
|
f9b80a820a25
Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents:
47914
diff
changeset
|
1099 PyErr_Format(PyExc_TypeError, "unhashable type: '%.200s'", |
|
f9b80a820a25
Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents:
47914
diff
changeset
|
1100 self->ob_type->tp_name); |
|
f9b80a820a25
Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents:
47914
diff
changeset
|
1101 return -1; |
|
f9b80a820a25
Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents:
47914
diff
changeset
|
1102 } |
|
13141
cca84745da3e
This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents:
13089
diff
changeset
|
1103 |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
1104 long |
|
13473
31b1f691a7d9
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
1105 PyObject_Hash(PyObject *v) |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
1106 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
1107 PyTypeObject *tp = v->ob_type; |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
1108 if (tp->tp_hash != NULL) |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
1109 return (*tp->tp_hash)(v); |
|
50370
e782f97116dc
Merged revisions 68051 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
50257
diff
changeset
|
1110 /* To keep to the general practice that inheriting |
|
e782f97116dc
Merged revisions 68051 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
50257
diff
changeset
|
1111 * solely from object in C code should work without |
|
e782f97116dc
Merged revisions 68051 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
50257
diff
changeset
|
1112 * an explicit call to PyType_Ready, we implicitly call |
|
e782f97116dc
Merged revisions 68051 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
50257
diff
changeset
|
1113 * PyType_Ready here and then check the tp_hash slot again |
|
e782f97116dc
Merged revisions 68051 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
50257
diff
changeset
|
1114 */ |
|
e782f97116dc
Merged revisions 68051 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
50257
diff
changeset
|
1115 if (tp->tp_dict == NULL) { |
|
e782f97116dc
Merged revisions 68051 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
50257
diff
changeset
|
1116 if (PyType_Ready(tp) < 0) |
|
e782f97116dc
Merged revisions 68051 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
50257
diff
changeset
|
1117 return -1; |
|
e782f97116dc
Merged revisions 68051 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
50257
diff
changeset
|
1118 if (tp->tp_hash != NULL) |
|
e782f97116dc
Merged revisions 68051 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
50257
diff
changeset
|
1119 return (*tp->tp_hash)(v); |
|
e782f97116dc
Merged revisions 68051 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
50257
diff
changeset
|
1120 } |
|
16236
e5d59a9dbfc0
Check the Py_TPFLAGS_HAVE_RICHCOMPARE flag before using the
Guido van Rossum <guido@python.org>
parents:
16197
diff
changeset
|
1121 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
|
1122 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
|
1123 } |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
1124 /* If there's a cmp but no hash defined, the object can't be hashed */ |
|
47961
f9b80a820a25
Issue 2235: __hash__ is once again inherited by default, but inheritance can be blocked explicitly so that collections.Hashable remains meaningful
Nick Coghlan <ncoghlan@gmail.com>
parents:
47914
diff
changeset
|
1125 return PyObject_HashNotImplemented(v); |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
1126 } |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
1127 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
1128 PyObject * |
|
35999
9541369b0dca
Add const to several API functions that take char *.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
35331
diff
changeset
|
1129 PyObject_GetAttrString(PyObject *v, const char *name) |
| 86 | 1130 { |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1131 PyObject *w, *res; |
|
3986
9c2cbf306864
Support for tp_getattro, tp_setattro (Sjoerd)
Guido van Rossum <guido@python.org>
parents:
3770
diff
changeset
|
1132 |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
43958
diff
changeset
|
1133 if (Py_TYPE(v)->tp_getattr != NULL) |
|
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
43958
diff
changeset
|
1134 return (*Py_TYPE(v)->tp_getattr)(v, (char*)name); |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
1135 w = PyString_InternFromString(name); |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1136 if (w == NULL) |
| 86 | 1137 return NULL; |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1138 res = PyObject_GetAttr(v, w); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1139 Py_XDECREF(w); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1140 return res; |
| 86 | 1141 } |
| 1142 | |
| 1143 int | |
|
35999
9541369b0dca
Add const to several API functions that take char *.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
35331
diff
changeset
|
1144 PyObject_HasAttrString(PyObject *v, const char *name) |
|
1324
716be765a79b
* accessobject.c (ownercheck): allow a base class access to protected
Guido van Rossum <guido@python.org>
parents:
1282
diff
changeset
|
1145 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
1146 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
|
1147 if (res != NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
1148 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
|
1149 return 1; |
|
716be765a79b
* accessobject.c (ownercheck): allow a base class access to protected
Guido van Rossum <guido@python.org>
parents:
1282
diff
changeset
|
1150 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
1151 PyErr_Clear(); |
|
1324
716be765a79b
* accessobject.c (ownercheck): allow a base class access to protected
Guido van Rossum <guido@python.org>
parents:
1282
diff
changeset
|
1152 return 0; |
|
716be765a79b
* accessobject.c (ownercheck): allow a base class access to protected
Guido van Rossum <guido@python.org>
parents:
1282
diff
changeset
|
1153 } |
|
716be765a79b
* accessobject.c (ownercheck): allow a base class access to protected
Guido van Rossum <guido@python.org>
parents:
1282
diff
changeset
|
1154 |
|
716be765a79b
* accessobject.c (ownercheck): allow a base class access to protected
Guido van Rossum <guido@python.org>
parents:
1282
diff
changeset
|
1155 int |
|
35999
9541369b0dca
Add const to several API functions that take char *.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
35331
diff
changeset
|
1156 PyObject_SetAttrString(PyObject *v, const char *name, PyObject *w) |
| 86 | 1157 { |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1158 PyObject *s; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1159 int res; |
|
3986
9c2cbf306864
Support for tp_getattro, tp_setattro (Sjoerd)
Guido van Rossum <guido@python.org>
parents:
3770
diff
changeset
|
1160 |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
43958
diff
changeset
|
1161 if (Py_TYPE(v)->tp_setattr != NULL) |
|
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
43958
diff
changeset
|
1162 return (*Py_TYPE(v)->tp_setattr)(v, (char*)name, w); |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
1163 s = PyString_InternFromString(name); |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1164 if (s == NULL) |
| 95 | 1165 return -1; |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1166 res = PyObject_SetAttr(v, s, w); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1167 Py_XDECREF(s); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1168 return res; |
| 86 | 1169 } |
| 1170 | |
|
5560
8ec298b255a2
Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents:
5535
diff
changeset
|
1171 PyObject * |
|
13473
31b1f691a7d9
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
1172 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
|
1173 { |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
43958
diff
changeset
|
1174 PyTypeObject *tp = Py_TYPE(v); |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1175 |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
1176 if (!PyString_Check(name)) { |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19294
diff
changeset
|
1177 #ifdef Py_USING_UNICODE |
|
22302
a0cc52735e6e
Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents:
22112
diff
changeset
|
1178 /* The Unicode to string conversion is done here because the |
|
a0cc52735e6e
Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents:
22112
diff
changeset
|
1179 existing tp_getattro slots expect a string object as name |
|
a0cc52735e6e
Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents:
22112
diff
changeset
|
1180 and we wouldn't want to break those. */ |
|
a0cc52735e6e
Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents:
22112
diff
changeset
|
1181 if (PyUnicode_Check(name)) { |
|
a0cc52735e6e
Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents:
22112
diff
changeset
|
1182 name = _PyUnicode_AsDefaultEncodedString(name, NULL); |
|
a0cc52735e6e
Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents:
22112
diff
changeset
|
1183 if (name == NULL) |
|
a0cc52735e6e
Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents:
22112
diff
changeset
|
1184 return NULL; |
|
a0cc52735e6e
Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents:
22112
diff
changeset
|
1185 } |
|
a0cc52735e6e
Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents:
22112
diff
changeset
|
1186 else |
|
a0cc52735e6e
Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents:
22112
diff
changeset
|
1187 #endif |
|
a0cc52735e6e
Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents:
22112
diff
changeset
|
1188 { |
|
38871
a3c61c648fc8
Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents:
38485
diff
changeset
|
1189 PyErr_Format(PyExc_TypeError, |
|
a3c61c648fc8
Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents:
38485
diff
changeset
|
1190 "attribute name must be string, not '%.200s'", |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
43958
diff
changeset
|
1191 Py_TYPE(name)->tp_name); |
|
14660
983502b2cd43
PyObject_SetAttr() and PyObject_GetAttr() now also accept Unicode
Marc-André Lemburg <mal@egenix.com>
parents:
14375
diff
changeset
|
1192 return NULL; |
|
22302
a0cc52735e6e
Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents:
22112
diff
changeset
|
1193 } |
|
12991
1a40f00a48bf
raise TypeError when PyObject_Get/SetAttr called with non-string name
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12904
diff
changeset
|
1194 } |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1195 if (tp->tp_getattro != NULL) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1196 return (*tp->tp_getattro)(v, name); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1197 if (tp->tp_getattr != NULL) |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
1198 return (*tp->tp_getattr)(v, PyString_AS_STRING(name)); |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1199 PyErr_Format(PyExc_AttributeError, |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1200 "'%.50s' object has no attribute '%.400s'", |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
1201 tp->tp_name, PyString_AS_STRING(name)); |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1202 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
|
1203 } |
|
8ec298b255a2
Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents:
5535
diff
changeset
|
1204 |
|
8ec298b255a2
Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents:
5535
diff
changeset
|
1205 int |
|
13473
31b1f691a7d9
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
1206 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
|
1207 { |
|
8ec298b255a2
Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents:
5535
diff
changeset
|
1208 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
|
1209 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
|
1210 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
|
1211 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
|
1212 } |
|
8ec298b255a2
Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents:
5535
diff
changeset
|
1213 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
|
1214 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
|
1215 } |
|
8ec298b255a2
Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents:
5535
diff
changeset
|
1216 |
|
8ec298b255a2
Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents:
5535
diff
changeset
|
1217 int |
|
13473
31b1f691a7d9
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
1218 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
|
1219 { |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
43958
diff
changeset
|
1220 PyTypeObject *tp = Py_TYPE(v); |
|
5560
8ec298b255a2
Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents:
5535
diff
changeset
|
1221 int err; |
|
14660
983502b2cd43
PyObject_SetAttr() and PyObject_GetAttr() now also accept Unicode
Marc-André Lemburg <mal@egenix.com>
parents:
14375
diff
changeset
|
1222 |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
1223 if (!PyString_Check(name)){ |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19294
diff
changeset
|
1224 #ifdef Py_USING_UNICODE |
|
22302
a0cc52735e6e
Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents:
22112
diff
changeset
|
1225 /* The Unicode to string conversion is done here because the |
|
a0cc52735e6e
Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents:
22112
diff
changeset
|
1226 existing tp_setattro slots expect a string object as name |
|
a0cc52735e6e
Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents:
22112
diff
changeset
|
1227 and we wouldn't want to break those. */ |
|
a0cc52735e6e
Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents:
22112
diff
changeset
|
1228 if (PyUnicode_Check(name)) { |
|
a0cc52735e6e
Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents:
22112
diff
changeset
|
1229 name = PyUnicode_AsEncodedString(name, NULL, NULL); |
|
a0cc52735e6e
Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents:
22112
diff
changeset
|
1230 if (name == NULL) |
|
a0cc52735e6e
Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents:
22112
diff
changeset
|
1231 return -1; |
|
a0cc52735e6e
Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents:
22112
diff
changeset
|
1232 } |
|
24122
d332484e394a
Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents:
24121
diff
changeset
|
1233 else |
|
22302
a0cc52735e6e
Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents:
22112
diff
changeset
|
1234 #endif |
|
a0cc52735e6e
Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents:
22112
diff
changeset
|
1235 { |
|
38871
a3c61c648fc8
Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents:
38485
diff
changeset
|
1236 PyErr_Format(PyExc_TypeError, |
|
a3c61c648fc8
Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents:
38485
diff
changeset
|
1237 "attribute name must be string, not '%.200s'", |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
43958
diff
changeset
|
1238 Py_TYPE(name)->tp_name); |
|
14660
983502b2cd43
PyObject_SetAttr() and PyObject_GetAttr() now also accept Unicode
Marc-André Lemburg <mal@egenix.com>
parents:
14375
diff
changeset
|
1239 return -1; |
|
22302
a0cc52735e6e
Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents:
22112
diff
changeset
|
1240 } |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1241 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1242 else |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1243 Py_INCREF(name); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1244 |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
1245 PyString_InternInPlace(&name); |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1246 if (tp->tp_setattro != NULL) { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1247 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
|
1248 Py_DECREF(name); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1249 return err; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1250 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1251 if (tp->tp_setattr != NULL) { |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
1252 err = (*tp->tp_setattr)(v, PyString_AS_STRING(name), value); |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1253 Py_DECREF(name); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1254 return err; |
|
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 Py_DECREF(name); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1257 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
|
1258 PyErr_Format(PyExc_TypeError, |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1259 "'%.100s' object has no attributes " |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1260 "(%s .%.100s)", |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1261 tp->tp_name, |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1262 value==NULL ? "del" : "assign to", |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
1263 PyString_AS_STRING(name)); |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1264 else |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1265 PyErr_Format(PyExc_TypeError, |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1266 "'%.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
|
1267 "(%s .%.100s)", |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1268 tp->tp_name, |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1269 value==NULL ? "del" : "assign to", |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
1270 PyString_AS_STRING(name)); |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1271 return -1; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1272 } |
|
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 /* 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
|
1275 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1276 PyObject ** |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1277 _PyObject_GetDictPtr(PyObject *obj) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1278 { |
|
36866
9eb24f92a784
Change int to Py_ssize_t in several places.
Martin v. Löwis <martin@v.loewis.de>
parents:
36838
diff
changeset
|
1279 Py_ssize_t dictoffset; |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
43958
diff
changeset
|
1280 PyTypeObject *tp = Py_TYPE(obj); |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1281 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1282 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
|
1283 return NULL; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1284 dictoffset = tp->tp_dictoffset; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1285 if (dictoffset == 0) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1286 return NULL; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1287 if (dictoffset < 0) { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
1288 Py_ssize_t tsize; |
|
22106
93eb4b61f687
SF patch 514641 (Naofumi Honda) - Negative ob_size of LongObjects
Guido van Rossum <guido@python.org>
parents:
21186
diff
changeset
|
1289 size_t size; |
|
93eb4b61f687
SF patch 514641 (Naofumi Honda) - Negative ob_size of LongObjects
Guido van Rossum <guido@python.org>
parents:
21186
diff
changeset
|
1290 |
|
93eb4b61f687
SF patch 514641 (Naofumi Honda) - Negative ob_size of LongObjects
Guido van Rossum <guido@python.org>
parents:
21186
diff
changeset
|
1291 tsize = ((PyVarObject *)obj)->ob_size; |
|
93eb4b61f687
SF patch 514641 (Naofumi Honda) - Negative ob_size of LongObjects
Guido van Rossum <guido@python.org>
parents:
21186
diff
changeset
|
1292 if (tsize < 0) |
|
93eb4b61f687
SF patch 514641 (Naofumi Honda) - Negative ob_size of LongObjects
Guido van Rossum <guido@python.org>
parents:
21186
diff
changeset
|
1293 tsize = -tsize; |
|
93eb4b61f687
SF patch 514641 (Naofumi Honda) - Negative ob_size of LongObjects
Guido van Rossum <guido@python.org>
parents:
21186
diff
changeset
|
1294 size = _PyObject_VAR_SIZE(tp, tsize); |
|
93eb4b61f687
SF patch 514641 (Naofumi Honda) - Negative ob_size of LongObjects
Guido van Rossum <guido@python.org>
parents:
21186
diff
changeset
|
1295 |
|
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
|
1296 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
|
1297 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
|
1298 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
|
1299 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1300 return (PyObject **) ((char *)obj + dictoffset); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1301 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1302 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1303 PyObject * |
|
28320
fd5ef7003469
Renamed PyObject_GenericGetIter to PyObject_SelfIter
Raymond Hettinger <python@rcn.com>
parents:
28308
diff
changeset
|
1304 PyObject_SelfIter(PyObject *obj) |
|
28308
9c3572a5575f
Created PyObject_GenericGetIter().
Raymond Hettinger <python@rcn.com>
parents:
27966
diff
changeset
|
1305 { |
|
9c3572a5575f
Created PyObject_GenericGetIter().
Raymond Hettinger <python@rcn.com>
parents:
27966
diff
changeset
|
1306 Py_INCREF(obj); |
|
9c3572a5575f
Created PyObject_GenericGetIter().
Raymond Hettinger <python@rcn.com>
parents:
27966
diff
changeset
|
1307 return obj; |
|
9c3572a5575f
Created PyObject_GenericGetIter().
Raymond Hettinger <python@rcn.com>
parents:
27966
diff
changeset
|
1308 } |
|
9c3572a5575f
Created PyObject_GenericGetIter().
Raymond Hettinger <python@rcn.com>
parents:
27966
diff
changeset
|
1309 |
|
33489
4cb1016525b7
Move a comment back to its rightful location.
Michael W. Hudson <mwh@python.net>
parents:
32981
diff
changeset
|
1310 /* Generic GetAttr functions - put these in your tp_[gs]etattro slot */ |
|
4cb1016525b7
Move a comment back to its rightful location.
Michael W. Hudson <mwh@python.net>
parents:
32981
diff
changeset
|
1311 |
|
28308
9c3572a5575f
Created PyObject_GenericGetIter().
Raymond Hettinger <python@rcn.com>
parents:
27966
diff
changeset
|
1312 PyObject * |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1313 PyObject_GenericGetAttr(PyObject *obj, PyObject *name) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1314 { |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
43958
diff
changeset
|
1315 PyTypeObject *tp = Py_TYPE(obj); |
|
24897
6310a4a21eb8
Another modest speedup in PyObject_GenericGetAttr(): inline the call
Guido van Rossum <guido@python.org>
parents:
24894
diff
changeset
|
1316 PyObject *descr = NULL; |
|
21186
0189186f4ed9
PyObject_Generic{Get,Set}Attr(): ensure that the attribute name is a
Guido van Rossum <guido@python.org>
parents:
20865
diff
changeset
|
1317 PyObject *res = NULL; |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1318 descrgetfunc f; |
|
36866
9eb24f92a784
Change int to Py_ssize_t in several places.
Martin v. Löwis <martin@v.loewis.de>
parents:
36838
diff
changeset
|
1319 Py_ssize_t dictoffset; |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1320 PyObject **dictptr; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1321 |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
1322 if (!PyString_Check(name)){ |
|
21186
0189186f4ed9
PyObject_Generic{Get,Set}Attr(): ensure that the attribute name is a
Guido van Rossum <guido@python.org>
parents:
20865
diff
changeset
|
1323 #ifdef Py_USING_UNICODE |
|
22302
a0cc52735e6e
Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents:
22112
diff
changeset
|
1324 /* The Unicode to string conversion is done here because the |
|
a0cc52735e6e
Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents:
22112
diff
changeset
|
1325 existing tp_setattro slots expect a string object as name |
|
a0cc52735e6e
Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents:
22112
diff
changeset
|
1326 and we wouldn't want to break those. */ |
|
a0cc52735e6e
Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents:
22112
diff
changeset
|
1327 if (PyUnicode_Check(name)) { |
|
a0cc52735e6e
Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents:
22112
diff
changeset
|
1328 name = PyUnicode_AsEncodedString(name, NULL, NULL); |
|
a0cc52735e6e
Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents:
22112
diff
changeset
|
1329 if (name == NULL) |
|
a0cc52735e6e
Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents:
22112
diff
changeset
|
1330 return NULL; |
|
a0cc52735e6e
Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents:
22112
diff
changeset
|
1331 } |
|
24122
d332484e394a
Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents:
24121
diff
changeset
|
1332 else |
|
22302
a0cc52735e6e
Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents:
22112
diff
changeset
|
1333 #endif |
|
a0cc52735e6e
Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents:
22112
diff
changeset
|
1334 { |
|
38871
a3c61c648fc8
Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents:
38485
diff
changeset
|
1335 PyErr_Format(PyExc_TypeError, |
|
a3c61c648fc8
Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents:
38485
diff
changeset
|
1336 "attribute name must be string, not '%.200s'", |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
43958
diff
changeset
|
1337 Py_TYPE(name)->tp_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
|
1338 return NULL; |
|
22302
a0cc52735e6e
Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents:
22112
diff
changeset
|
1339 } |
|
21186
0189186f4ed9
PyObject_Generic{Get,Set}Attr(): ensure that the attribute name is a
Guido van Rossum <guido@python.org>
parents:
20865
diff
changeset
|
1340 } |
|
0189186f4ed9
PyObject_Generic{Get,Set}Attr(): ensure that the attribute name is a
Guido van Rossum <guido@python.org>
parents:
20865
diff
changeset
|
1341 else |
|
0189186f4ed9
PyObject_Generic{Get,Set}Attr(): ensure that the attribute name is a
Guido van Rossum <guido@python.org>
parents:
20865
diff
changeset
|
1342 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
|
1343 |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1344 if (tp->tp_dict == NULL) { |
|
19040
4df72ea78ae8
- Rename PyType_InitDict() to PyType_Ready().
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
1345 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
|
1346 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
|
1347 } |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1348 |
|
44664
387d48c3a0b3
Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
44661
diff
changeset
|
1349 #if 0 /* XXX this is not quite _PyType_Lookup anymore */ |
|
24897
6310a4a21eb8
Another modest speedup in PyObject_GenericGetAttr(): inline the call
Guido van Rossum <guido@python.org>
parents:
24894
diff
changeset
|
1350 /* Inline _PyType_Lookup */ |
|
6310a4a21eb8
Another modest speedup in PyObject_GenericGetAttr(): inline the call
Guido van Rossum <guido@python.org>
parents:
24894
diff
changeset
|
1351 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
1352 Py_ssize_t i, n; |
|
24897
6310a4a21eb8
Another modest speedup in PyObject_GenericGetAttr(): inline the call
Guido van Rossum <guido@python.org>
parents:
24894
diff
changeset
|
1353 PyObject *mro, *base, *dict; |
|
6310a4a21eb8
Another modest speedup in PyObject_GenericGetAttr(): inline the call
Guido van Rossum <guido@python.org>
parents:
24894
diff
changeset
|
1354 |
|
6310a4a21eb8
Another modest speedup in PyObject_GenericGetAttr(): inline the call
Guido van Rossum <guido@python.org>
parents:
24894
diff
changeset
|
1355 /* Look in tp_dict of types in MRO */ |
|
6310a4a21eb8
Another modest speedup in PyObject_GenericGetAttr(): inline the call
Guido van Rossum <guido@python.org>
parents:
24894
diff
changeset
|
1356 mro = tp->tp_mro; |
|
6310a4a21eb8
Another modest speedup in PyObject_GenericGetAttr(): inline the call
Guido van Rossum <guido@python.org>
parents:
24894
diff
changeset
|
1357 assert(mro != NULL); |
|
6310a4a21eb8
Another modest speedup in PyObject_GenericGetAttr(): inline the call
Guido van Rossum <guido@python.org>
parents:
24894
diff
changeset
|
1358 assert(PyTuple_Check(mro)); |
|
6310a4a21eb8
Another modest speedup in PyObject_GenericGetAttr(): inline the call
Guido van Rossum <guido@python.org>
parents:
24894
diff
changeset
|
1359 n = PyTuple_GET_SIZE(mro); |
|
6310a4a21eb8
Another modest speedup in PyObject_GenericGetAttr(): inline the call
Guido van Rossum <guido@python.org>
parents:
24894
diff
changeset
|
1360 for (i = 0; i < n; i++) { |
|
6310a4a21eb8
Another modest speedup in PyObject_GenericGetAttr(): inline the call
Guido van Rossum <guido@python.org>
parents:
24894
diff
changeset
|
1361 base = PyTuple_GET_ITEM(mro, i); |
|
6310a4a21eb8
Another modest speedup in PyObject_GenericGetAttr(): inline the call
Guido van Rossum <guido@python.org>
parents:
24894
diff
changeset
|
1362 if (PyClass_Check(base)) |
|
6310a4a21eb8
Another modest speedup in PyObject_GenericGetAttr(): inline the call
Guido van Rossum <guido@python.org>
parents:
24894
diff
changeset
|
1363 dict = ((PyClassObject *)base)->cl_dict; |
|
6310a4a21eb8
Another modest speedup in PyObject_GenericGetAttr(): inline the call
Guido van Rossum <guido@python.org>
parents:
24894
diff
changeset
|
1364 else { |
|
6310a4a21eb8
Another modest speedup in PyObject_GenericGetAttr(): inline the call
Guido van Rossum <guido@python.org>
parents:
24894
diff
changeset
|
1365 assert(PyType_Check(base)); |
|
6310a4a21eb8
Another modest speedup in PyObject_GenericGetAttr(): inline the call
Guido van Rossum <guido@python.org>
parents:
24894
diff
changeset
|
1366 dict = ((PyTypeObject *)base)->tp_dict; |
|
6310a4a21eb8
Another modest speedup in PyObject_GenericGetAttr(): inline the call
Guido van Rossum <guido@python.org>
parents:
24894
diff
changeset
|
1367 } |
|
6310a4a21eb8
Another modest speedup in PyObject_GenericGetAttr(): inline the call
Guido van Rossum <guido@python.org>
parents:
24894
diff
changeset
|
1368 assert(dict && PyDict_Check(dict)); |
|
6310a4a21eb8
Another modest speedup in PyObject_GenericGetAttr(): inline the call
Guido van Rossum <guido@python.org>
parents:
24894
diff
changeset
|
1369 descr = PyDict_GetItem(dict, name); |
|
6310a4a21eb8
Another modest speedup in PyObject_GenericGetAttr(): inline the call
Guido van Rossum <guido@python.org>
parents:
24894
diff
changeset
|
1370 if (descr != NULL) |
|
6310a4a21eb8
Another modest speedup in PyObject_GenericGetAttr(): inline the call
Guido van Rossum <guido@python.org>
parents:
24894
diff
changeset
|
1371 break; |
|
6310a4a21eb8
Another modest speedup in PyObject_GenericGetAttr(): inline the call
Guido van Rossum <guido@python.org>
parents:
24894
diff
changeset
|
1372 } |
|
6310a4a21eb8
Another modest speedup in PyObject_GenericGetAttr(): inline the call
Guido van Rossum <guido@python.org>
parents:
24894
diff
changeset
|
1373 } |
|
44664
387d48c3a0b3
Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
44661
diff
changeset
|
1374 #else |
|
387d48c3a0b3
Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
44661
diff
changeset
|
1375 descr = _PyType_Lookup(tp, name); |
|
387d48c3a0b3
Re-apply patch #1700288 (first applied in r59931, rolled back in r59940)
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
44661
diff
changeset
|
1376 #endif |
|
24897
6310a4a21eb8
Another modest speedup in PyObject_GenericGetAttr(): inline the call
Guido van Rossum <guido@python.org>
parents:
24894
diff
changeset
|
1377 |
| 30159 | 1378 Py_XINCREF(descr); |
| 1379 | |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1380 f = NULL; |
|
27966
b28d4fde9e9f
PyObject_Generic{Get,Set}Attr:
Guido van Rossum <guido@python.org>
parents:
27938
diff
changeset
|
1381 if (descr != NULL && |
|
b28d4fde9e9f
PyObject_Generic{Get,Set}Attr:
Guido van Rossum <guido@python.org>
parents:
27938
diff
changeset
|
1382 PyType_HasFeature(descr->ob_type, Py_TPFLAGS_HAVE_CLASS)) { |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1383 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
|
1384 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
|
1385 res = f(descr, obj, (PyObject *)obj->ob_type); |
| 30159 | 1386 Py_DECREF(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
|
1387 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
|
1388 } |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1389 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1390 |
|
24894
8862bc59685a
Inline call to _PyObject_GetDictPtr() in PyObject_GenericGetAttr().
Guido van Rossum <guido@python.org>
parents:
24644
diff
changeset
|
1391 /* Inline _PyObject_GetDictPtr */ |
|
8862bc59685a
Inline call to _PyObject_GetDictPtr() in PyObject_GenericGetAttr().
Guido van Rossum <guido@python.org>
parents:
24644
diff
changeset
|
1392 dictoffset = tp->tp_dictoffset; |
|
8862bc59685a
Inline call to _PyObject_GetDictPtr() in PyObject_GenericGetAttr().
Guido van Rossum <guido@python.org>
parents:
24644
diff
changeset
|
1393 if (dictoffset != 0) { |
|
8862bc59685a
Inline call to _PyObject_GetDictPtr() in PyObject_GenericGetAttr().
Guido van Rossum <guido@python.org>
parents:
24644
diff
changeset
|
1394 PyObject *dict; |
|
8862bc59685a
Inline call to _PyObject_GetDictPtr() in PyObject_GenericGetAttr().
Guido van Rossum <guido@python.org>
parents:
24644
diff
changeset
|
1395 if (dictoffset < 0) { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
1396 Py_ssize_t tsize; |
|
24894
8862bc59685a
Inline call to _PyObject_GetDictPtr() in PyObject_GenericGetAttr().
Guido van Rossum <guido@python.org>
parents:
24644
diff
changeset
|
1397 size_t size; |
|
8862bc59685a
Inline call to _PyObject_GetDictPtr() in PyObject_GenericGetAttr().
Guido van Rossum <guido@python.org>
parents:
24644
diff
changeset
|
1398 |
|
8862bc59685a
Inline call to _PyObject_GetDictPtr() in PyObject_GenericGetAttr().
Guido van Rossum <guido@python.org>
parents:
24644
diff
changeset
|
1399 tsize = ((PyVarObject *)obj)->ob_size; |
|
8862bc59685a
Inline call to _PyObject_GetDictPtr() in PyObject_GenericGetAttr().
Guido van Rossum <guido@python.org>
parents:
24644
diff
changeset
|
1400 if (tsize < 0) |
|
8862bc59685a
Inline call to _PyObject_GetDictPtr() in PyObject_GenericGetAttr().
Guido van Rossum <guido@python.org>
parents:
24644
diff
changeset
|
1401 tsize = -tsize; |
|
8862bc59685a
Inline call to _PyObject_GetDictPtr() in PyObject_GenericGetAttr().
Guido van Rossum <guido@python.org>
parents:
24644
diff
changeset
|
1402 size = _PyObject_VAR_SIZE(tp, tsize); |
|
8862bc59685a
Inline call to _PyObject_GetDictPtr() in PyObject_GenericGetAttr().
Guido van Rossum <guido@python.org>
parents:
24644
diff
changeset
|
1403 |
|
8862bc59685a
Inline call to _PyObject_GetDictPtr() in PyObject_GenericGetAttr().
Guido van Rossum <guido@python.org>
parents:
24644
diff
changeset
|
1404 dictoffset += (long)size; |
|
8862bc59685a
Inline call to _PyObject_GetDictPtr() in PyObject_GenericGetAttr().
Guido van Rossum <guido@python.org>
parents:
24644
diff
changeset
|
1405 assert(dictoffset > 0); |
|
8862bc59685a
Inline call to _PyObject_GetDictPtr() in PyObject_GenericGetAttr().
Guido van Rossum <guido@python.org>
parents:
24644
diff
changeset
|
1406 assert(dictoffset % SIZEOF_VOID_P == 0); |
|
8862bc59685a
Inline call to _PyObject_GetDictPtr() in PyObject_GenericGetAttr().
Guido van Rossum <guido@python.org>
parents:
24644
diff
changeset
|
1407 } |
|
8862bc59685a
Inline call to _PyObject_GetDictPtr() in PyObject_GenericGetAttr().
Guido van Rossum <guido@python.org>
parents:
24644
diff
changeset
|
1408 dictptr = (PyObject **) ((char *)obj + dictoffset); |
|
8862bc59685a
Inline call to _PyObject_GetDictPtr() in PyObject_GenericGetAttr().
Guido van Rossum <guido@python.org>
parents:
24644
diff
changeset
|
1409 dict = *dictptr; |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1410 if (dict != NULL) { |
|
44915
ebbb7bb0033b
Fix test67.py from issue #1303614.
Guido van Rossum <guido@python.org>
parents:
44664
diff
changeset
|
1411 Py_INCREF(dict); |
|
21186
0189186f4ed9
PyObject_Generic{Get,Set}Attr(): ensure that the attribute name is a
Guido van Rossum <guido@python.org>
parents:
20865
diff
changeset
|
1412 res = PyDict_GetItem(dict, name); |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1413 if (res != NULL) { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1414 Py_INCREF(res); |
| 30159 | 1415 Py_XDECREF(descr); |
|
44915
ebbb7bb0033b
Fix test67.py from issue #1303614.
Guido van Rossum <guido@python.org>
parents:
44664
diff
changeset
|
1416 Py_DECREF(dict); |
|
21186
0189186f4ed9
PyObject_Generic{Get,Set}Attr(): ensure that the attribute name is a
Guido van Rossum <guido@python.org>
parents:
20865
diff
changeset
|
1417 goto done; |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1418 } |
|
44915
ebbb7bb0033b
Fix test67.py from issue #1303614.
Guido van Rossum <guido@python.org>
parents:
44664
diff
changeset
|
1419 Py_DECREF(dict); |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1420 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1421 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1422 |
|
21186
0189186f4ed9
PyObject_Generic{Get,Set}Attr(): ensure that the attribute name is a
Guido van Rossum <guido@python.org>
parents:
20865
diff
changeset
|
1423 if (f != NULL) { |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
43958
diff
changeset
|
1424 res = f(descr, obj, (PyObject *)Py_TYPE(obj)); |
| 30159 | 1425 Py_DECREF(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
|
1426 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
|
1427 } |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1428 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1429 if (descr != NULL) { |
|
21186
0189186f4ed9
PyObject_Generic{Get,Set}Attr(): ensure that the attribute name is a
Guido van Rossum <guido@python.org>
parents:
20865
diff
changeset
|
1430 res = descr; |
| 30159 | 1431 /* descr was already increfed above */ |
|
21186
0189186f4ed9
PyObject_Generic{Get,Set}Attr(): ensure that the attribute name is a
Guido van Rossum <guido@python.org>
parents:
20865
diff
changeset
|
1432 goto done; |
|
14660
983502b2cd43
PyObject_SetAttr() and PyObject_GetAttr() now also accept Unicode
Marc-André Lemburg <mal@egenix.com>
parents:
14375
diff
changeset
|
1433 } |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1434 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1435 PyErr_Format(PyExc_AttributeError, |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1436 "'%.50s' object has no attribute '%.400s'", |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
1437 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
|
1438 done: |
|
0189186f4ed9
PyObject_Generic{Get,Set}Attr(): ensure that the attribute name is a
Guido van Rossum <guido@python.org>
parents:
20865
diff
changeset
|
1439 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
|
1440 return res; |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1441 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1442 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1443 int |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1444 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
|
1445 { |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
43958
diff
changeset
|
1446 PyTypeObject *tp = Py_TYPE(obj); |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1447 PyObject *descr; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1448 descrsetfunc f; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1449 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
|
1450 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
|
1451 |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
1452 if (!PyString_Check(name)){ |
|
21186
0189186f4ed9
PyObject_Generic{Get,Set}Attr(): ensure that the attribute name is a
Guido van Rossum <guido@python.org>
parents:
20865
diff
changeset
|
1453 #ifdef Py_USING_UNICODE |
|
22302
a0cc52735e6e
Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents:
22112
diff
changeset
|
1454 /* The Unicode to string conversion is done here because the |
|
a0cc52735e6e
Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents:
22112
diff
changeset
|
1455 existing tp_setattro slots expect a string object as name |
|
a0cc52735e6e
Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents:
22112
diff
changeset
|
1456 and we wouldn't want to break those. */ |
|
a0cc52735e6e
Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents:
22112
diff
changeset
|
1457 if (PyUnicode_Check(name)) { |
|
a0cc52735e6e
Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents:
22112
diff
changeset
|
1458 name = PyUnicode_AsEncodedString(name, NULL, NULL); |
|
a0cc52735e6e
Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents:
22112
diff
changeset
|
1459 if (name == NULL) |
|
a0cc52735e6e
Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents:
22112
diff
changeset
|
1460 return -1; |
|
a0cc52735e6e
Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents:
22112
diff
changeset
|
1461 } |
|
24122
d332484e394a
Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents:
24121
diff
changeset
|
1462 else |
|
22302
a0cc52735e6e
Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents:
22112
diff
changeset
|
1463 #endif |
|
a0cc52735e6e
Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents:
22112
diff
changeset
|
1464 { |
|
38871
a3c61c648fc8
Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents:
38485
diff
changeset
|
1465 PyErr_Format(PyExc_TypeError, |
|
a3c61c648fc8
Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents:
38485
diff
changeset
|
1466 "attribute name must be string, not '%.200s'", |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
43958
diff
changeset
|
1467 Py_TYPE(name)->tp_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
|
1468 return -1; |
|
22302
a0cc52735e6e
Patch #517521: Consider byte strings before Unicode strings
Martin v. Löwis <martin@v.loewis.de>
parents:
22112
diff
changeset
|
1469 } |
|
21186
0189186f4ed9
PyObject_Generic{Get,Set}Attr(): ensure that the attribute name is a
Guido van Rossum <guido@python.org>
parents:
20865
diff
changeset
|
1470 } |
|
0189186f4ed9
PyObject_Generic{Get,Set}Attr(): ensure that the attribute name is a
Guido van Rossum <guido@python.org>
parents:
20865
diff
changeset
|
1471 else |
|
0189186f4ed9
PyObject_Generic{Get,Set}Attr(): ensure that the attribute name is a
Guido van Rossum <guido@python.org>
parents:
20865
diff
changeset
|
1472 Py_INCREF(name); |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1473 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1474 if (tp->tp_dict == NULL) { |
|
19040
4df72ea78ae8
- Rename PyType_InitDict() to PyType_Ready().
Guido van Rossum <guido@python.org>
parents:
18933
diff
changeset
|
1475 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
|
1476 goto done; |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1477 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1478 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1479 descr = _PyType_Lookup(tp, name); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1480 f = NULL; |
|
27966
b28d4fde9e9f
PyObject_Generic{Get,Set}Attr:
Guido van Rossum <guido@python.org>
parents:
27938
diff
changeset
|
1481 if (descr != NULL && |
|
b28d4fde9e9f
PyObject_Generic{Get,Set}Attr:
Guido van Rossum <guido@python.org>
parents:
27938
diff
changeset
|
1482 PyType_HasFeature(descr->ob_type, Py_TPFLAGS_HAVE_CLASS)) { |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1483 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
|
1484 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
|
1485 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
|
1486 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
|
1487 } |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1488 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1489 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1490 dictptr = _PyObject_GetDictPtr(obj); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1491 if (dictptr != NULL) { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1492 PyObject *dict = *dictptr; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1493 if (dict == NULL && value != NULL) { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1494 dict = PyDict_New(); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1495 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
|
1496 goto done; |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1497 *dictptr = dict; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1498 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1499 if (dict != NULL) { |
|
44915
ebbb7bb0033b
Fix test67.py from issue #1303614.
Guido van Rossum <guido@python.org>
parents:
44664
diff
changeset
|
1500 Py_INCREF(dict); |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1501 if (value == NULL) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1502 res = PyDict_DelItem(dict, name); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1503 else |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1504 res = PyDict_SetItem(dict, name, value); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1505 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
|
1506 PyErr_SetObject(PyExc_AttributeError, name); |
|
44915
ebbb7bb0033b
Fix test67.py from issue #1303614.
Guido van Rossum <guido@python.org>
parents:
44664
diff
changeset
|
1507 Py_DECREF(dict); |
|
21186
0189186f4ed9
PyObject_Generic{Get,Set}Attr(): ensure that the attribute name is a
Guido van Rossum <guido@python.org>
parents:
20865
diff
changeset
|
1508 goto done; |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1509 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1510 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1511 |
|
21186
0189186f4ed9
PyObject_Generic{Get,Set}Attr(): ensure that the attribute name is a
Guido van Rossum <guido@python.org>
parents:
20865
diff
changeset
|
1512 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
|
1513 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
|
1514 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
|
1515 } |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1516 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1517 if (descr == NULL) { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1518 PyErr_Format(PyExc_AttributeError, |
|
38871
a3c61c648fc8
Patch #1507676: improve exception messages in abstract.c, object.c and typeobject.c.
Georg Brandl <georg@python.org>
parents:
38485
diff
changeset
|
1519 "'%.100s' object has no attribute '%.200s'", |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
1520 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
|
1521 goto done; |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1522 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1523 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1524 PyErr_Format(PyExc_AttributeError, |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1525 "'%.50s' object attribute '%.400s' is read-only", |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
1526 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
|
1527 done: |
|
0189186f4ed9
PyObject_Generic{Get,Set}Attr(): ensure that the attribute name is a
Guido van Rossum <guido@python.org>
parents:
20865
diff
changeset
|
1528 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
|
1529 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
|
1530 } |
|
8ec298b255a2
Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents:
5535
diff
changeset
|
1531 |
|
1282
e0464e7115ca
* pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
1532 /* 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
|
1533 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
|
1534 |
|
e0464e7115ca
* pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
1535 int |
|
13473
31b1f691a7d9
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
1536 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
|
1537 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
1538 Py_ssize_t res; |
|
25015
24e80d96bd0b
Speedup for PyObject_IsTrue(): check for True and False first.
Guido van Rossum <guido@python.org>
parents:
25013
diff
changeset
|
1539 if (v == Py_True) |
|
24e80d96bd0b
Speedup for PyObject_IsTrue(): check for True and False first.
Guido van Rossum <guido@python.org>
parents:
25013
diff
changeset
|
1540 return 1; |
|
24e80d96bd0b
Speedup for PyObject_IsTrue(): check for True and False first.
Guido van Rossum <guido@python.org>
parents:
25013
diff
changeset
|
1541 if (v == Py_False) |
|
24e80d96bd0b
Speedup for PyObject_IsTrue(): check for True and False first.
Guido van Rossum <guido@python.org>
parents:
25013
diff
changeset
|
1542 return 0; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
1543 if (v == Py_None) |
|
23838
95263ccec819
SF # 561244 Micro optimizations
Neal Norwitz <nnorwitz@gmail.com>
parents:
23599
diff
changeset
|
1544 return 0; |
|
8404
64b88a178328
In PyObject_IsTrue(), don't call function pointers that are NULL
Guido van Rossum <guido@python.org>
parents:
8181
diff
changeset
|
1545 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
|
1546 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
|
1547 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
|
1548 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
|
1549 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
|
1550 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
|
1551 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
|
1552 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
|
1553 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
|
1554 else |
|
23838
95263ccec819
SF # 561244 Micro optimizations
Neal Norwitz <nnorwitz@gmail.com>
parents:
23599
diff
changeset
|
1555 return 1; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
1556 /* if it is negative, it should be either -1 or -2 */ |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
1557 return (res > 0) ? 1 : Py_SAFE_DOWNCAST(res, Py_ssize_t, int); |
|
1282
e0464e7115ca
* pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
1558 } |
|
e0464e7115ca
* pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
1559 |
|
24122
d332484e394a
Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents:
24121
diff
changeset
|
1560 /* equivalent of 'not v' |
| 7935 | 1561 Return -1 if an error occurred */ |
| 1562 | |
| 1563 int | |
|
13473
31b1f691a7d9
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
1564 PyObject_Not(PyObject *v) |
| 7935 | 1565 { |
| 1566 int res; | |
| 1567 res = PyObject_IsTrue(v); | |
| 1568 if (res < 0) | |
| 1569 return res; | |
| 1570 return res == 0; | |
| 1571 } | |
| 1572 | |
|
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
|
1573 /* 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
|
1574 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
|
1575 Return value: |
|
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
1576 -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
|
1577 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
|
1578 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
|
1579 */ |
|
76c860a18d82
move coerce() from bltinmodule.c to object.c and implement builtin_coerce() differently
Guido van Rossum <guido@python.org>
parents:
2079
diff
changeset
|
1580 int |
|
13473
31b1f691a7d9
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
1581 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
|
1582 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
1583 register PyObject *v = *pv; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
1584 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
|
1585 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
|
1586 |
|
23282
a4e75d53d32b
PyNumber_CoerceEx: this took a shortcut (not doing anything) when the
Guido van Rossum <guido@python.org>
parents:
22985
diff
changeset
|
1587 /* Shortcut only for old-style types */ |
|
a4e75d53d32b
PyNumber_CoerceEx: this took a shortcut (not doing anything) when the
Guido van Rossum <guido@python.org>
parents:
22985
diff
changeset
|
1588 if (v->ob_type == w->ob_type && |
|
a4e75d53d32b
PyNumber_CoerceEx: this took a shortcut (not doing anything) when the
Guido van Rossum <guido@python.org>
parents:
22985
diff
changeset
|
1589 !PyType_HasFeature(v->ob_type, Py_TPFLAGS_CHECKTYPES)) |
|
a4e75d53d32b
PyNumber_CoerceEx: this took a shortcut (not doing anything) when the
Guido van Rossum <guido@python.org>
parents:
22985
diff
changeset
|
1590 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
1591 Py_INCREF(v); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
1592 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
|
1593 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
|
1594 } |
|
76c860a18d82
move coerce() from bltinmodule.c to object.c and implement builtin_coerce() differently
Guido van Rossum <guido@python.org>
parents:
2079
diff
changeset
|
1595 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
|
1596 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
|
1597 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
|
1598 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
|
1599 } |
|
76c860a18d82
move coerce() from bltinmodule.c to object.c and implement builtin_coerce() differently
Guido van Rossum <guido@python.org>
parents:
2079
diff
changeset
|
1600 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
|
1601 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
|
1602 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
|
1603 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
|
1604 } |
|
6597
b88fd3894d5a
Add a new function PyNumber_CoerceEx() which works just like
Guido van Rossum <guido@python.org>
parents:
5981
diff
changeset
|
1605 return 1; |
|
b88fd3894d5a
Add a new function PyNumber_CoerceEx() which works just like
Guido van Rossum <guido@python.org>
parents:
5981
diff
changeset
|
1606 } |
|
b88fd3894d5a
Add a new function PyNumber_CoerceEx() which works just like
Guido van Rossum <guido@python.org>
parents:
5981
diff
changeset
|
1607 |
|
15941
5abae795742e
Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents:
15726
diff
changeset
|
1608 /* 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
|
1609 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
|
1610 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
|
1611 (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
|
1612 */ |
|
6597
b88fd3894d5a
Add a new function PyNumber_CoerceEx() which works just like
Guido van Rossum <guido@python.org>
parents:
5981
diff
changeset
|
1613 int |
|
13473
31b1f691a7d9
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
1614 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
|
1615 { |
|
b88fd3894d5a
Add a new function PyNumber_CoerceEx() which works just like
Guido van Rossum <guido@python.org>
parents:
5981
diff
changeset
|
1616 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
|
1617 if (err <= 0) |
|
b88fd3894d5a
Add a new function PyNumber_CoerceEx() which works just like
Guido van Rossum <guido@python.org>
parents:
5981
diff
changeset
|
1618 return err; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
1619 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
|
1620 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
|
1621 } |
|
76c860a18d82
move coerce() from bltinmodule.c to object.c and implement builtin_coerce() differently
Guido van Rossum <guido@python.org>
parents:
2079
diff
changeset
|
1622 |
| 6 | 1623 |
| 2237 | 1624 /* Test whether an object can be called */ |
| 1625 | |
| 1626 int | |
|
13473
31b1f691a7d9
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
1627 PyCallable_Check(PyObject *x) |
| 2237 | 1628 { |
| 1629 if (x == NULL) | |
| 1630 return 0; | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
1631 if (PyInstance_Check(x)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
1632 PyObject *call = PyObject_GetAttrString(x, "__call__"); |
| 2237 | 1633 if (call == NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
1634 PyErr_Clear(); |
| 2237 | 1635 return 0; |
| 1636 } | |
| 1637 /* Could test recursively but don't, for fear of endless | |
| 1638 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
|
1639 Py_DECREF(call); |
| 2237 | 1640 return 1; |
| 1641 } | |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1642 else { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
1643 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
|
1644 } |
| 2237 | 1645 } |
| 1646 | |
|
41125
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1647 /* ------------------------- PyObject_Dir() helpers ------------------------- */ |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1648 |
|
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
|
1649 /* 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
|
1650 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
|
1651 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
|
1652 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
|
1653 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
|
1654 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
|
1655 */ |
|
b7f6fbc087a7
At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents:
19633
diff
changeset
|
1656 |
|
b7f6fbc087a7
At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents:
19633
diff
changeset
|
1657 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
|
1658 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
|
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 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
|
1661 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
|
1662 |
|
b7f6fbc087a7
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 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
|
1664 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
|
1665 |
|
b7f6fbc087a7
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 /* 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
|
1667 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
|
1668 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
|
1669 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
|
1670 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
|
1671 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
|
1672 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
|
1673 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
|
1674 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
|
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 |
|
b7f6fbc087a7
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 /* 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
|
1678 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
|
1679 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
|
1680 PyErr_Clear(); |
|
c327a8ac9f2b
merge_class_dict(): Clear the error if __bases__ doesn't exist.
Tim Peters <tim.peters@gmail.com>
parents:
19906
diff
changeset
|
1681 else { |
|
23436
f9d5e1fb2aa6
Jim Fulton reported a segfault in dir(). A heavily proxied object
Guido van Rossum <guido@python.org>
parents:
23282
diff
changeset
|
1682 /* We have no guarantee that bases is a real tuple */ |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
1683 Py_ssize_t i, n; |
|
23436
f9d5e1fb2aa6
Jim Fulton reported a segfault in dir(). A heavily proxied object
Guido van Rossum <guido@python.org>
parents:
23282
diff
changeset
|
1684 n = PySequence_Size(bases); /* This better be right */ |
|
f9d5e1fb2aa6
Jim Fulton reported a segfault in dir(). A heavily proxied object
Guido van Rossum <guido@python.org>
parents:
23282
diff
changeset
|
1685 if (n < 0) |
|
f9d5e1fb2aa6
Jim Fulton reported a segfault in dir(). A heavily proxied object
Guido van Rossum <guido@python.org>
parents:
23282
diff
changeset
|
1686 PyErr_Clear(); |
|
f9d5e1fb2aa6
Jim Fulton reported a segfault in dir(). A heavily proxied object
Guido van Rossum <guido@python.org>
parents:
23282
diff
changeset
|
1687 else { |
|
f9d5e1fb2aa6
Jim Fulton reported a segfault in dir(). A heavily proxied object
Guido van Rossum <guido@python.org>
parents:
23282
diff
changeset
|
1688 for (i = 0; i < n; i++) { |
|
27702
84f2c438c3f5
SF bug 681122: Built-in function dir() causes refcount leak in baseclasses.
Tim Peters <tim.peters@gmail.com>
parents:
27314
diff
changeset
|
1689 int status; |
|
23436
f9d5e1fb2aa6
Jim Fulton reported a segfault in dir(). A heavily proxied object
Guido van Rossum <guido@python.org>
parents:
23282
diff
changeset
|
1690 PyObject *base = PySequence_GetItem(bases, i); |
|
f9d5e1fb2aa6
Jim Fulton reported a segfault in dir(). A heavily proxied object
Guido van Rossum <guido@python.org>
parents:
23282
diff
changeset
|
1691 if (base == NULL) { |
|
f9d5e1fb2aa6
Jim Fulton reported a segfault in dir(). A heavily proxied object
Guido van Rossum <guido@python.org>
parents:
23282
diff
changeset
|
1692 Py_DECREF(bases); |
|
f9d5e1fb2aa6
Jim Fulton reported a segfault in dir(). A heavily proxied object
Guido van Rossum <guido@python.org>
parents:
23282
diff
changeset
|
1693 return -1; |
|
f9d5e1fb2aa6
Jim Fulton reported a segfault in dir(). A heavily proxied object
Guido van Rossum <guido@python.org>
parents:
23282
diff
changeset
|
1694 } |
|
27702
84f2c438c3f5
SF bug 681122: Built-in function dir() causes refcount leak in baseclasses.
Tim Peters <tim.peters@gmail.com>
parents:
27314
diff
changeset
|
1695 status = merge_class_dict(dict, base); |
|
84f2c438c3f5
SF bug 681122: Built-in function dir() causes refcount leak in baseclasses.
Tim Peters <tim.peters@gmail.com>
parents:
27314
diff
changeset
|
1696 Py_DECREF(base); |
|
84f2c438c3f5
SF bug 681122: Built-in function dir() causes refcount leak in baseclasses.
Tim Peters <tim.peters@gmail.com>
parents:
27314
diff
changeset
|
1697 if (status < 0) { |
|
23436
f9d5e1fb2aa6
Jim Fulton reported a segfault in dir(). A heavily proxied object
Guido van Rossum <guido@python.org>
parents:
23282
diff
changeset
|
1698 Py_DECREF(bases); |
|
f9d5e1fb2aa6
Jim Fulton reported a segfault in dir(). A heavily proxied object
Guido van Rossum <guido@python.org>
parents:
23282
diff
changeset
|
1699 return -1; |
|
f9d5e1fb2aa6
Jim Fulton reported a segfault in dir(). A heavily proxied object
Guido van Rossum <guido@python.org>
parents:
23282
diff
changeset
|
1700 } |
|
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
|
1701 } |
|
b7f6fbc087a7
At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents:
19633
diff
changeset
|
1702 } |
|
b7f6fbc087a7
At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents:
19633
diff
changeset
|
1703 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
|
1704 } |
|
b7f6fbc087a7
At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents:
19633
diff
changeset
|
1705 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
|
1706 } |
|
b7f6fbc087a7
At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents:
19633
diff
changeset
|
1707 |
|
19932
621ed13445de
PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
Tim Peters <tim.peters@gmail.com>
parents:
19931
diff
changeset
|
1708 /* 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
|
1709 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
|
1710 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
|
1711 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
|
1712 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
|
1713 */ |
|
621ed13445de
PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
Tim Peters <tim.peters@gmail.com>
parents:
19931
diff
changeset
|
1714 |
|
621ed13445de
PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
Tim Peters <tim.peters@gmail.com>
parents:
19931
diff
changeset
|
1715 static int |
|
35999
9541369b0dca
Add const to several API functions that take char *.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
35331
diff
changeset
|
1716 merge_list_attr(PyObject* dict, PyObject* obj, const char *attrname) |
|
19932
621ed13445de
PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
Tim Peters <tim.peters@gmail.com>
parents:
19931
diff
changeset
|
1717 { |
|
621ed13445de
PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
Tim Peters <tim.peters@gmail.com>
parents:
19931
diff
changeset
|
1718 PyObject *list; |
|
621ed13445de
PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
Tim Peters <tim.peters@gmail.com>
parents:
19931
diff
changeset
|
1719 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
|
1720 |
|
621ed13445de
PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
Tim Peters <tim.peters@gmail.com>
parents:
19931
diff
changeset
|
1721 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
|
1722 assert(obj); |
|
621ed13445de
PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
Tim Peters <tim.peters@gmail.com>
parents:
19931
diff
changeset
|
1723 assert(attrname); |
|
621ed13445de
PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
Tim Peters <tim.peters@gmail.com>
parents:
19931
diff
changeset
|
1724 |
|
621ed13445de
PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
Tim Peters <tim.peters@gmail.com>
parents:
19931
diff
changeset
|
1725 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
|
1726 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
|
1727 PyErr_Clear(); |
|
621ed13445de
PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
Tim Peters <tim.peters@gmail.com>
parents:
19931
diff
changeset
|
1728 |
|
621ed13445de
PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
Tim Peters <tim.peters@gmail.com>
parents:
19931
diff
changeset
|
1729 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
|
1730 int i; |
|
621ed13445de
PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
Tim Peters <tim.peters@gmail.com>
parents:
19931
diff
changeset
|
1731 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
|
1732 PyObject *item = PyList_GET_ITEM(list, i); |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
1733 if (PyString_Check(item)) { |
|
19932
621ed13445de
PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
Tim Peters <tim.peters@gmail.com>
parents:
19931
diff
changeset
|
1734 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
|
1735 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
|
1736 break; |
|
621ed13445de
PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
Tim Peters <tim.peters@gmail.com>
parents:
19931
diff
changeset
|
1737 } |
|
621ed13445de
PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
Tim Peters <tim.peters@gmail.com>
parents:
19931
diff
changeset
|
1738 } |
|
45984
2275612c16a9
#2346/#2347: add py3k warning for __methods__ and __members__. Patch by Jack Diederich.
Georg Brandl <georg@python.org>
parents:
45860
diff
changeset
|
1739 if (Py_Py3kWarningFlag && |
|
2275612c16a9
#2346/#2347: add py3k warning for __methods__ and __members__. Patch by Jack Diederich.
Georg Brandl <georg@python.org>
parents:
45860
diff
changeset
|
1740 (strcmp(attrname, "__members__") == 0 || |
|
2275612c16a9
#2346/#2347: add py3k warning for __methods__ and __members__. Patch by Jack Diederich.
Georg Brandl <georg@python.org>
parents:
45860
diff
changeset
|
1741 strcmp(attrname, "__methods__") == 0)) { |
|
46573
aef3501d8e77
Use PyErr_WarnPy3k throughout
Benjamin Peterson <benjamin@python.org>
parents:
46107
diff
changeset
|
1742 if (PyErr_WarnEx(PyExc_DeprecationWarning, |
|
46075
84bb7701efba
Make Py3k warnings consistent w.r.t. punctuation; also respect the
Georg Brandl <georg@python.org>
parents:
45984
diff
changeset
|
1743 "__members__ and __methods__ not " |
|
46573
aef3501d8e77
Use PyErr_WarnPy3k throughout
Benjamin Peterson <benjamin@python.org>
parents:
46107
diff
changeset
|
1744 "supported in 3.x", 1) < 0) { |
|
45984
2275612c16a9
#2346/#2347: add py3k warning for __methods__ and __members__. Patch by Jack Diederich.
Georg Brandl <georg@python.org>
parents:
45860
diff
changeset
|
1745 Py_XDECREF(list); |
|
2275612c16a9
#2346/#2347: add py3k warning for __methods__ and __members__. Patch by Jack Diederich.
Georg Brandl <georg@python.org>
parents:
45860
diff
changeset
|
1746 return -1; |
|
2275612c16a9
#2346/#2347: add py3k warning for __methods__ and __members__. Patch by Jack Diederich.
Georg Brandl <georg@python.org>
parents:
45860
diff
changeset
|
1747 } |
|
2275612c16a9
#2346/#2347: add py3k warning for __methods__ and __members__. Patch by Jack Diederich.
Georg Brandl <georg@python.org>
parents:
45860
diff
changeset
|
1748 } |
|
19932
621ed13445de
PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
Tim Peters <tim.peters@gmail.com>
parents:
19931
diff
changeset
|
1749 } |
|
621ed13445de
PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
Tim Peters <tim.peters@gmail.com>
parents:
19931
diff
changeset
|
1750 |
|
621ed13445de
PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
Tim Peters <tim.peters@gmail.com>
parents:
19931
diff
changeset
|
1751 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
|
1752 return result; |
|
621ed13445de
PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
Tim Peters <tim.peters@gmail.com>
parents:
19931
diff
changeset
|
1753 } |
|
621ed13445de
PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
Tim Peters <tim.peters@gmail.com>
parents:
19931
diff
changeset
|
1754 |
|
41125
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1755 /* Helper for PyObject_Dir without arguments: returns the local scope. */ |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1756 static PyObject * |
|
41214
d1ba8134528e
Remove warning: funcion declaration isn't a prototype
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
41140
diff
changeset
|
1757 _dir_locals(void) |
|
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
|
1758 { |
|
41125
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1759 PyObject *names; |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1760 PyObject *locals = PyEval_GetLocals(); |
|
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
|
1761 |
|
41125
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1762 if (locals == NULL) { |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1763 PyErr_SetString(PyExc_SystemError, "frame does not exist"); |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1764 return NULL; |
|
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
|
1765 } |
|
b7f6fbc087a7
At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents:
19633
diff
changeset
|
1766 |
|
41125
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1767 names = PyMapping_Keys(locals); |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1768 if (!names) |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1769 return NULL; |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1770 if (!PyList_Check(names)) { |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1771 PyErr_Format(PyExc_TypeError, |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1772 "dir(): expected keys() of locals to be a list, " |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
43958
diff
changeset
|
1773 "not '%.200s'", Py_TYPE(names)->tp_name); |
|
41125
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1774 Py_DECREF(names); |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1775 return NULL; |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1776 } |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1777 /* the locals don't need to be DECREF'd */ |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1778 return names; |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1779 } |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1780 |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1781 /* Helper for PyObject_Dir of type objects: returns __dict__ and __bases__. |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1782 We deliberately don't suck up its __class__, as methods belonging to the |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1783 metaclass would probably be more confusing than helpful. |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1784 */ |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1785 static PyObject * |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1786 _specialized_dir_type(PyObject *obj) |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1787 { |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1788 PyObject *result = NULL; |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1789 PyObject *dict = PyDict_New(); |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1790 |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1791 if (dict != NULL && merge_class_dict(dict, obj) == 0) |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1792 result = PyDict_Keys(dict); |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1793 |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1794 Py_XDECREF(dict); |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1795 return result; |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1796 } |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1797 |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1798 /* Helper for PyObject_Dir of module objects: returns the module's __dict__. */ |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1799 static PyObject * |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1800 _specialized_dir_module(PyObject *obj) |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1801 { |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1802 PyObject *result = NULL; |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1803 PyObject *dict = PyObject_GetAttrString(obj, "__dict__"); |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1804 |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1805 if (dict != NULL) { |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1806 if (PyDict_Check(dict)) |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1807 result = PyDict_Keys(dict); |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1808 else { |
|
55645
bc1c403054e1
Merged revisions 74457 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
55049
diff
changeset
|
1809 char *name = PyModule_GetName(obj); |
|
bc1c403054e1
Merged revisions 74457 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
55049
diff
changeset
|
1810 if (name) |
|
bc1c403054e1
Merged revisions 74457 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
55049
diff
changeset
|
1811 PyErr_Format(PyExc_TypeError, |
|
bc1c403054e1
Merged revisions 74457 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
55049
diff
changeset
|
1812 "%.200s.__dict__ is not a dictionary", |
|
bc1c403054e1
Merged revisions 74457 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
55049
diff
changeset
|
1813 name); |
| 19833 | 1814 } |
|
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
|
1815 } |
|
b7f6fbc087a7
At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents:
19633
diff
changeset
|
1816 |
|
41125
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1817 Py_XDECREF(dict); |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1818 return result; |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1819 } |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1820 |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1821 /* Helper for PyObject_Dir of generic objects: returns __dict__, __class__, |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1822 and recursively up the __class__.__bases__ chain. |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1823 */ |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1824 static PyObject * |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1825 _generic_dir(PyObject *obj) |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1826 { |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1827 PyObject *result = NULL; |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1828 PyObject *dict = NULL; |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1829 PyObject *itsclass = NULL; |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1830 |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1831 /* Get __dict__ (which may or may not be a real dict...) */ |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1832 dict = PyObject_GetAttrString(obj, "__dict__"); |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1833 if (dict == NULL) { |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1834 PyErr_Clear(); |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1835 dict = PyDict_New(); |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1836 } |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1837 else if (!PyDict_Check(dict)) { |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1838 Py_DECREF(dict); |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1839 dict = PyDict_New(); |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1840 } |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1841 else { |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1842 /* Copy __dict__ to avoid mutating it. */ |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1843 PyObject *temp = PyDict_Copy(dict); |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1844 Py_DECREF(dict); |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1845 dict = temp; |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1846 } |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1847 |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1848 if (dict == NULL) |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1849 goto error; |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1850 |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1851 /* Merge in __members__ and __methods__ (if any). |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1852 * This is removed in Python 3000. */ |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1853 if (merge_list_attr(dict, obj, "__members__") < 0) |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1854 goto error; |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1855 if (merge_list_attr(dict, obj, "__methods__") < 0) |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1856 goto error; |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1857 |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1858 /* Merge in attrs reachable from its class. */ |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1859 itsclass = PyObject_GetAttrString(obj, "__class__"); |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1860 if (itsclass == NULL) |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1861 /* XXX(tomer): Perhaps fall back to obj->ob_type if no |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1862 __class__ exists? */ |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1863 PyErr_Clear(); |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1864 else { |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1865 if (merge_class_dict(dict, itsclass) != 0) |
|
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
|
1866 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
|
1867 } |
|
b7f6fbc087a7
At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents:
19633
diff
changeset
|
1868 |
|
41125
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1869 result = PyDict_Keys(dict); |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1870 /* fall through */ |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1871 error: |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1872 Py_XDECREF(itsclass); |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1873 Py_XDECREF(dict); |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1874 return result; |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1875 } |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1876 |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1877 /* Helper for PyObject_Dir: object introspection. |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1878 This calls one of the above specialized versions if no __dir__ method |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1879 exists. */ |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1880 static PyObject * |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1881 _dir_object(PyObject *obj) |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1882 { |
| 41140 | 1883 PyObject *result = NULL; |
| 1884 PyObject *dirfunc = PyObject_GetAttrString((PyObject *)obj->ob_type, | |
| 1885 "__dir__"); | |
|
41125
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1886 |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1887 assert(obj); |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1888 if (dirfunc == NULL) { |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1889 /* use default implementation */ |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1890 PyErr_Clear(); |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1891 if (PyModule_Check(obj)) |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1892 result = _specialized_dir_module(obj); |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1893 else if (PyType_Check(obj) || PyClass_Check(obj)) |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1894 result = _specialized_dir_type(obj); |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1895 else |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1896 result = _generic_dir(obj); |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1897 } |
|
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
|
1898 else { |
|
41125
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1899 /* use __dir__ */ |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1900 result = PyObject_CallFunctionObjArgs(dirfunc, obj, NULL); |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1901 Py_DECREF(dirfunc); |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1902 if (result == NULL) |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1903 return NULL; |
|
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
|
1904 |
|
41125
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1905 /* result must be a list */ |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1906 /* XXX(gbrandl): could also check if all items are strings */ |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1907 if (!PyList_Check(result)) { |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1908 PyErr_Format(PyExc_TypeError, |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1909 "__dir__() must return a list, not %.200s", |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
43958
diff
changeset
|
1910 Py_TYPE(result)->tp_name); |
|
41125
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1911 Py_DECREF(result); |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1912 result = NULL; |
|
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
|
1913 } |
|
b7f6fbc087a7
At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents:
19633
diff
changeset
|
1914 } |
|
b7f6fbc087a7
At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents:
19633
diff
changeset
|
1915 |
|
41125
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1916 return result; |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1917 } |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1918 |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1919 /* Implementation of dir() -- if obj is NULL, returns the names in the current |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1920 (local) scope. Otherwise, performs introspection of the object: returns a |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1921 sorted list of attribute names (supposedly) accessible from the object |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1922 */ |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1923 PyObject * |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1924 PyObject_Dir(PyObject *obj) |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1925 { |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1926 PyObject * result; |
|
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
|
1927 |
|
41125
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1928 if (obj == NULL) |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1929 /* no object -- introspect the locals */ |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1930 result = _dir_locals(); |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1931 else |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1932 /* object -- introspect the object */ |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1933 result = _dir_object(obj); |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1934 |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1935 assert(result == NULL || PyList_Check(result)); |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1936 |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1937 if (result != NULL && PyList_Sort(result) != 0) { |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1938 /* sorting the list failed */ |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1939 Py_DECREF(result); |
|
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1940 result = NULL; |
|
32981
50c122bc7aac
SF bug #1004669: Type returned from .keys() is not checked
Raymond Hettinger <python@rcn.com>
parents:
32531
diff
changeset
|
1941 } |
|
41125
717bc2a4eee7
Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents:
39487
diff
changeset
|
1942 |
|
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
|
1943 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
|
1944 } |
| 2237 | 1945 |
| 6 | 1946 /* |
| 1947 NoObject is usable as a non-NULL undefined value, used by the macro None. | |
| 1948 There is (and should be!) no way to create other objects of this type, | |
| 86 | 1949 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
|
1950 (XXX This type and the type of NotImplemented below should be unified.) |
| 6 | 1951 */ |
| 1952 | |
| 739 | 1953 /* ARGSUSED */ |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
1954 static PyObject * |
|
13473
31b1f691a7d9
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
1955 none_repr(PyObject *op) |
| 86 | 1956 { |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
1957 return PyString_FromString("None"); |
| 6 | 1958 } |
| 1959 | |
|
16165
0a72070eab6e
A few miscellaneous helpers.
Barry Warsaw <barry@python.org>
parents:
16123
diff
changeset
|
1960 /* ARGUSED */ |
|
0a72070eab6e
A few miscellaneous helpers.
Barry Warsaw <barry@python.org>
parents:
16123
diff
changeset
|
1961 static void |
|
24122
d332484e394a
Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents:
24121
diff
changeset
|
1962 none_dealloc(PyObject* ignore) |
|
16165
0a72070eab6e
A few miscellaneous helpers.
Barry Warsaw <barry@python.org>
parents:
16123
diff
changeset
|
1963 { |
|
0a72070eab6e
A few miscellaneous helpers.
Barry Warsaw <barry@python.org>
parents:
16123
diff
changeset
|
1964 /* This should never get called, but we also don't want to SEGV if |
|
55049
74c0a78114ae
Merged revisions 69846 via svnmerge from
Jesus Cea <jcea@jcea.es>
parents:
53273
diff
changeset
|
1965 * we accidentally decref None out of existence. |
|
16165
0a72070eab6e
A few miscellaneous helpers.
Barry Warsaw <barry@python.org>
parents:
16123
diff
changeset
|
1966 */ |
|
24644
7ba3e5fbaad8
Replace abort with Py_FatalError.
Martin v. Löwis <martin@v.loewis.de>
parents:
24420
diff
changeset
|
1967 Py_FatalError("deallocating None"); |
|
16165
0a72070eab6e
A few miscellaneous helpers.
Barry Warsaw <barry@python.org>
parents:
16123
diff
changeset
|
1968 } |
|
0a72070eab6e
A few miscellaneous helpers.
Barry Warsaw <barry@python.org>
parents:
16123
diff
changeset
|
1969 |
|
0a72070eab6e
A few miscellaneous helpers.
Barry Warsaw <barry@python.org>
parents:
16123
diff
changeset
|
1970 |
|
19294
1456ec735087
Add a function _Py_ReadyTypes() which initializes various and sundry
Guido van Rossum <guido@python.org>
parents:
19293
diff
changeset
|
1971 static PyTypeObject PyNone_Type = { |
|
42333
6620a510e9a9
PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents:
41832
diff
changeset
|
1972 PyVarObject_HEAD_INIT(&PyType_Type, 0) |
|
19294
1456ec735087
Add a function _Py_ReadyTypes() which initializes various and sundry
Guido van Rossum <guido@python.org>
parents:
19293
diff
changeset
|
1973 "NoneType", |
| 6 | 1974 0, |
| 1975 0, | |
|
37304
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
37300
diff
changeset
|
1976 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
|
1977 0, /*tp_print*/ |
| 86 | 1978 0, /*tp_getattr*/ |
| 1979 0, /*tp_setattr*/ | |
| 1980 0, /*tp_compare*/ | |
|
37304
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
37300
diff
changeset
|
1981 none_repr, /*tp_repr*/ |
| 86 | 1982 0, /*tp_as_number*/ |
| 1983 0, /*tp_as_sequence*/ | |
| 1984 0, /*tp_as_mapping*/ | |
|
43958
103b7f955036
Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents:
43451
diff
changeset
|
1985 (hashfunc)_Py_HashPointer, /*tp_hash */ |
| 6 | 1986 }; |
| 1987 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
1988 PyObject _Py_NoneStruct = { |
|
42333
6620a510e9a9
PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents:
41832
diff
changeset
|
1989 _PyObject_EXTRA_INIT |
|
6620a510e9a9
PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents:
41832
diff
changeset
|
1990 1, &PyNone_Type |
| 6 | 1991 }; |
| 1992 | |
|
15726
db9f1893be01
Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1993 /* 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
|
1994 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
|
1995 |
|
db9f1893be01
Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1996 static PyObject * |
|
db9f1893be01
Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1997 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
|
1998 { |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
1999 return PyString_FromString("NotImplemented"); |
|
15726
db9f1893be01
Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
2000 } |
|
db9f1893be01
Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
2001 |
|
db9f1893be01
Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
2002 static PyTypeObject PyNotImplemented_Type = { |
|
42333
6620a510e9a9
PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents:
41832
diff
changeset
|
2003 PyVarObject_HEAD_INIT(&PyType_Type, 0) |
|
19294
1456ec735087
Add a function _Py_ReadyTypes() which initializes various and sundry
Guido van Rossum <guido@python.org>
parents:
19293
diff
changeset
|
2004 "NotImplementedType", |
|
15726
db9f1893be01
Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
2005 0, |
|
db9f1893be01
Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
2006 0, |
|
37304
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
37300
diff
changeset
|
2007 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
|
2008 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
|
2009 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
|
2010 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
|
2011 0, /*tp_compare*/ |
|
37304
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
37300
diff
changeset
|
2012 NotImplemented_repr, /*tp_repr*/ |
|
15726
db9f1893be01
Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
2013 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
|
2014 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
|
2015 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
|
2016 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
|
2017 }; |
|
db9f1893be01
Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
2018 |
|
db9f1893be01
Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
2019 PyObject _Py_NotImplementedStruct = { |
|
42333
6620a510e9a9
PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents:
41832
diff
changeset
|
2020 _PyObject_EXTRA_INIT |
|
6620a510e9a9
PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents:
41832
diff
changeset
|
2021 1, &PyNotImplemented_Type |
|
15726
db9f1893be01
Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
2022 }; |
|
db9f1893be01
Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
2023 |
|
19294
1456ec735087
Add a function _Py_ReadyTypes() which initializes various and sundry
Guido van Rossum <guido@python.org>
parents:
19293
diff
changeset
|
2024 void |
|
1456ec735087
Add a function _Py_ReadyTypes() which initializes various and sundry
Guido van Rossum <guido@python.org>
parents:
19293
diff
changeset
|
2025 _Py_ReadyTypes(void) |
|
1456ec735087
Add a function _Py_ReadyTypes() which initializes various and sundry
Guido van Rossum <guido@python.org>
parents:
19293
diff
changeset
|
2026 { |
|
1456ec735087
Add a function _Py_ReadyTypes() which initializes various and sundry
Guido van Rossum <guido@python.org>
parents:
19293
diff
changeset
|
2027 if (PyType_Ready(&PyType_Type) < 0) |
|
53243
329cb02f18e6
Merged revisions 71722,71725 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52903
diff
changeset
|
2028 Py_FatalError("Can't initialize type type"); |
|
19294
1456ec735087
Add a function _Py_ReadyTypes() which initializes various and sundry
Guido van Rossum <guido@python.org>
parents:
19293
diff
changeset
|
2029 |
|
32472
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
32463
diff
changeset
|
2030 if (PyType_Ready(&_PyWeakref_RefType) < 0) |
|
53243
329cb02f18e6
Merged revisions 71722,71725 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52903
diff
changeset
|
2031 Py_FatalError("Can't initialize weakref type"); |
|
32472
4383db77b19a
Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents:
32463
diff
changeset
|
2032 |
|
53273
42bf56e34825
Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
53243
diff
changeset
|
2033 if (PyType_Ready(&_PyWeakref_CallableProxyType) < 0) |
|
42bf56e34825
Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
53243
diff
changeset
|
2034 Py_FatalError("Can't initialize callable weakref proxy type"); |
|
42bf56e34825
Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
53243
diff
changeset
|
2035 |
|
42bf56e34825
Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
53243
diff
changeset
|
2036 if (PyType_Ready(&_PyWeakref_ProxyType) < 0) |
|
42bf56e34825
Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
53243
diff
changeset
|
2037 Py_FatalError("Can't initialize weakref proxy type"); |
|
42bf56e34825
Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
53243
diff
changeset
|
2038 |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22649
diff
changeset
|
2039 if (PyType_Ready(&PyBool_Type) < 0) |
|
53243
329cb02f18e6
Merged revisions 71722,71725 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52903
diff
changeset
|
2040 Py_FatalError("Can't initialize bool type"); |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22649
diff
changeset
|
2041 |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
2042 if (PyType_Ready(&PyString_Type) < 0) |
|
53243
329cb02f18e6
Merged revisions 71722,71725 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52903
diff
changeset
|
2043 Py_FatalError("Can't initialize str type"); |
|
23542
220b98f06415
- A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents:
23436
diff
changeset
|
2044 |
|
47291
04bd3690fb1f
First step of the C API rename:
Christian Heimes <christian@cheimes.de>
parents:
46573
diff
changeset
|
2045 if (PyType_Ready(&PyByteArray_Type) < 0) |
|
53273
42bf56e34825
Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
53243
diff
changeset
|
2046 Py_FatalError("Can't initialize bytearray type"); |
|
46107
5341b30b1812
Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
46075
diff
changeset
|
2047 |
|
19294
1456ec735087
Add a function _Py_ReadyTypes() which initializes various and sundry
Guido van Rossum <guido@python.org>
parents:
19293
diff
changeset
|
2048 if (PyType_Ready(&PyList_Type) < 0) |
|
53273
42bf56e34825
Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
53243
diff
changeset
|
2049 Py_FatalError("Can't initialize list type"); |
|
19294
1456ec735087
Add a function _Py_ReadyTypes() which initializes various and sundry
Guido van Rossum <guido@python.org>
parents:
19293
diff
changeset
|
2050 |
|
1456ec735087
Add a function _Py_ReadyTypes() which initializes various and sundry
Guido van Rossum <guido@python.org>
parents:
19293
diff
changeset
|
2051 if (PyType_Ready(&PyNone_Type) < 0) |
|
53243
329cb02f18e6
Merged revisions 71722,71725 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52903
diff
changeset
|
2052 Py_FatalError("Can't initialize None type"); |
|
19294
1456ec735087
Add a function _Py_ReadyTypes() which initializes various and sundry
Guido van Rossum <guido@python.org>
parents:
19293
diff
changeset
|
2053 |
|
1456ec735087
Add a function _Py_ReadyTypes() which initializes various and sundry
Guido van Rossum <guido@python.org>
parents:
19293
diff
changeset
|
2054 if (PyType_Ready(&PyNotImplemented_Type) < 0) |
|
53243
329cb02f18e6
Merged revisions 71722,71725 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52903
diff
changeset
|
2055 Py_FatalError("Can't initialize NotImplemented type"); |
|
329cb02f18e6
Merged revisions 71722,71725 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52903
diff
changeset
|
2056 |
|
329cb02f18e6
Merged revisions 71722,71725 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52903
diff
changeset
|
2057 if (PyType_Ready(&PyTraceBack_Type) < 0) |
|
329cb02f18e6
Merged revisions 71722,71725 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52903
diff
changeset
|
2058 Py_FatalError("Can't initialize traceback type"); |
|
329cb02f18e6
Merged revisions 71722,71725 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52903
diff
changeset
|
2059 |
|
329cb02f18e6
Merged revisions 71722,71725 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52903
diff
changeset
|
2060 if (PyType_Ready(&PySuper_Type) < 0) |
|
329cb02f18e6
Merged revisions 71722,71725 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52903
diff
changeset
|
2061 Py_FatalError("Can't initialize super type"); |
|
329cb02f18e6
Merged revisions 71722,71725 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52903
diff
changeset
|
2062 |
|
329cb02f18e6
Merged revisions 71722,71725 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52903
diff
changeset
|
2063 if (PyType_Ready(&PyBaseObject_Type) < 0) |
|
329cb02f18e6
Merged revisions 71722,71725 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52903
diff
changeset
|
2064 Py_FatalError("Can't initialize object type"); |
|
329cb02f18e6
Merged revisions 71722,71725 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52903
diff
changeset
|
2065 |
|
329cb02f18e6
Merged revisions 71722,71725 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52903
diff
changeset
|
2066 if (PyType_Ready(&PyRange_Type) < 0) |
|
329cb02f18e6
Merged revisions 71722,71725 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52903
diff
changeset
|
2067 Py_FatalError("Can't initialize xrange type"); |
|
329cb02f18e6
Merged revisions 71722,71725 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52903
diff
changeset
|
2068 |
|
329cb02f18e6
Merged revisions 71722,71725 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52903
diff
changeset
|
2069 if (PyType_Ready(&PyDict_Type) < 0) |
|
329cb02f18e6
Merged revisions 71722,71725 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52903
diff
changeset
|
2070 Py_FatalError("Can't initialize dict type"); |
|
329cb02f18e6
Merged revisions 71722,71725 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52903
diff
changeset
|
2071 |
|
329cb02f18e6
Merged revisions 71722,71725 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52903
diff
changeset
|
2072 if (PyType_Ready(&PySet_Type) < 0) |
|
329cb02f18e6
Merged revisions 71722,71725 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52903
diff
changeset
|
2073 Py_FatalError("Can't initialize set type"); |
|
329cb02f18e6
Merged revisions 71722,71725 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52903
diff
changeset
|
2074 |
|
329cb02f18e6
Merged revisions 71722,71725 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52903
diff
changeset
|
2075 if (PyType_Ready(&PyUnicode_Type) < 0) |
|
329cb02f18e6
Merged revisions 71722,71725 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52903
diff
changeset
|
2076 Py_FatalError("Can't initialize unicode type"); |
|
329cb02f18e6
Merged revisions 71722,71725 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52903
diff
changeset
|
2077 |
|
329cb02f18e6
Merged revisions 71722,71725 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52903
diff
changeset
|
2078 if (PyType_Ready(&PySlice_Type) < 0) |
|
329cb02f18e6
Merged revisions 71722,71725 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52903
diff
changeset
|
2079 Py_FatalError("Can't initialize slice type"); |
|
329cb02f18e6
Merged revisions 71722,71725 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52903
diff
changeset
|
2080 |
|
329cb02f18e6
Merged revisions 71722,71725 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52903
diff
changeset
|
2081 if (PyType_Ready(&PyStaticMethod_Type) < 0) |
|
329cb02f18e6
Merged revisions 71722,71725 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52903
diff
changeset
|
2082 Py_FatalError("Can't initialize static method type"); |
|
329cb02f18e6
Merged revisions 71722,71725 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52903
diff
changeset
|
2083 |
|
53273
42bf56e34825
Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
53243
diff
changeset
|
2084 #ifndef WITHOUT_COMPLEX |
|
53243
329cb02f18e6
Merged revisions 71722,71725 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52903
diff
changeset
|
2085 if (PyType_Ready(&PyComplex_Type) < 0) |
|
53273
42bf56e34825
Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
53243
diff
changeset
|
2086 Py_FatalError("Can't initialize complex type"); |
|
42bf56e34825
Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
53243
diff
changeset
|
2087 #endif |
|
53243
329cb02f18e6
Merged revisions 71722,71725 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52903
diff
changeset
|
2088 |
|
329cb02f18e6
Merged revisions 71722,71725 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52903
diff
changeset
|
2089 if (PyType_Ready(&PyFloat_Type) < 0) |
|
329cb02f18e6
Merged revisions 71722,71725 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52903
diff
changeset
|
2090 Py_FatalError("Can't initialize float type"); |
|
329cb02f18e6
Merged revisions 71722,71725 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52903
diff
changeset
|
2091 |
|
329cb02f18e6
Merged revisions 71722,71725 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52903
diff
changeset
|
2092 if (PyType_Ready(&PyBuffer_Type) < 0) |
|
329cb02f18e6
Merged revisions 71722,71725 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52903
diff
changeset
|
2093 Py_FatalError("Can't initialize buffer type"); |
|
329cb02f18e6
Merged revisions 71722,71725 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52903
diff
changeset
|
2094 |
|
329cb02f18e6
Merged revisions 71722,71725 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52903
diff
changeset
|
2095 if (PyType_Ready(&PyLong_Type) < 0) |
|
329cb02f18e6
Merged revisions 71722,71725 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52903
diff
changeset
|
2096 Py_FatalError("Can't initialize long type"); |
|
329cb02f18e6
Merged revisions 71722,71725 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52903
diff
changeset
|
2097 |
|
329cb02f18e6
Merged revisions 71722,71725 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52903
diff
changeset
|
2098 if (PyType_Ready(&PyInt_Type) < 0) |
|
329cb02f18e6
Merged revisions 71722,71725 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52903
diff
changeset
|
2099 Py_FatalError("Can't initialize int type"); |
|
329cb02f18e6
Merged revisions 71722,71725 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52903
diff
changeset
|
2100 |
|
329cb02f18e6
Merged revisions 71722,71725 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52903
diff
changeset
|
2101 if (PyType_Ready(&PyFrozenSet_Type) < 0) |
|
329cb02f18e6
Merged revisions 71722,71725 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52903
diff
changeset
|
2102 Py_FatalError("Can't initialize frozenset type"); |
|
329cb02f18e6
Merged revisions 71722,71725 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52903
diff
changeset
|
2103 |
|
329cb02f18e6
Merged revisions 71722,71725 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52903
diff
changeset
|
2104 if (PyType_Ready(&PyProperty_Type) < 0) |
|
329cb02f18e6
Merged revisions 71722,71725 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52903
diff
changeset
|
2105 Py_FatalError("Can't initialize property type"); |
|
329cb02f18e6
Merged revisions 71722,71725 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52903
diff
changeset
|
2106 |
|
329cb02f18e6
Merged revisions 71722,71725 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52903
diff
changeset
|
2107 if (PyType_Ready(&PyTuple_Type) < 0) |
|
329cb02f18e6
Merged revisions 71722,71725 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52903
diff
changeset
|
2108 Py_FatalError("Can't initialize tuple type"); |
|
329cb02f18e6
Merged revisions 71722,71725 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52903
diff
changeset
|
2109 |
|
329cb02f18e6
Merged revisions 71722,71725 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52903
diff
changeset
|
2110 if (PyType_Ready(&PyEnum_Type) < 0) |
|
329cb02f18e6
Merged revisions 71722,71725 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52903
diff
changeset
|
2111 Py_FatalError("Can't initialize enumerate type"); |
|
329cb02f18e6
Merged revisions 71722,71725 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52903
diff
changeset
|
2112 |
|
329cb02f18e6
Merged revisions 71722,71725 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52903
diff
changeset
|
2113 if (PyType_Ready(&PyReversed_Type) < 0) |
|
329cb02f18e6
Merged revisions 71722,71725 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
52903
diff
changeset
|
2114 Py_FatalError("Can't initialize reversed type"); |
|
53273
42bf56e34825
Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
53243
diff
changeset
|
2115 |
|
42bf56e34825
Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
53243
diff
changeset
|
2116 if (PyType_Ready(&PyCode_Type) < 0) |
|
42bf56e34825
Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
53243
diff
changeset
|
2117 Py_FatalError("Can't initialize code type"); |
|
42bf56e34825
Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
53243
diff
changeset
|
2118 |
|
42bf56e34825
Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
53243
diff
changeset
|
2119 if (PyType_Ready(&PyFrame_Type) < 0) |
|
42bf56e34825
Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
53243
diff
changeset
|
2120 Py_FatalError("Can't initialize frame type"); |
|
42bf56e34825
Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
53243
diff
changeset
|
2121 |
|
42bf56e34825
Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
53243
diff
changeset
|
2122 if (PyType_Ready(&PyCFunction_Type) < 0) |
|
42bf56e34825
Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
53243
diff
changeset
|
2123 Py_FatalError("Can't initialize builtin function type"); |
|
42bf56e34825
Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
53243
diff
changeset
|
2124 |
|
42bf56e34825
Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
53243
diff
changeset
|
2125 if (PyType_Ready(&PyMethod_Type) < 0) |
|
42bf56e34825
Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
53243
diff
changeset
|
2126 Py_FatalError("Can't initialize method type"); |
|
42bf56e34825
Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
53243
diff
changeset
|
2127 |
|
42bf56e34825
Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
53243
diff
changeset
|
2128 if (PyType_Ready(&PyFunction_Type) < 0) |
|
42bf56e34825
Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
53243
diff
changeset
|
2129 Py_FatalError("Can't initialize function type"); |
|
42bf56e34825
Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
53243
diff
changeset
|
2130 |
|
42bf56e34825
Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
53243
diff
changeset
|
2131 if (PyType_Ready(&PyClass_Type) < 0) |
|
42bf56e34825
Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
53243
diff
changeset
|
2132 Py_FatalError("Can't initialize class type"); |
|
42bf56e34825
Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
53243
diff
changeset
|
2133 |
|
42bf56e34825
Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
53243
diff
changeset
|
2134 if (PyType_Ready(&PyDictProxy_Type) < 0) |
|
42bf56e34825
Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
53243
diff
changeset
|
2135 Py_FatalError("Can't initialize dict proxy type"); |
|
42bf56e34825
Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
53243
diff
changeset
|
2136 |
|
42bf56e34825
Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
53243
diff
changeset
|
2137 if (PyType_Ready(&PyGen_Type) < 0) |
|
42bf56e34825
Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
53243
diff
changeset
|
2138 Py_FatalError("Can't initialize generator type"); |
|
42bf56e34825
Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
53243
diff
changeset
|
2139 |
|
42bf56e34825
Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
53243
diff
changeset
|
2140 if (PyType_Ready(&PyGetSetDescr_Type) < 0) |
|
42bf56e34825
Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
53243
diff
changeset
|
2141 Py_FatalError("Can't initialize get-set descriptor type"); |
|
42bf56e34825
Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
53243
diff
changeset
|
2142 |
|
42bf56e34825
Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
53243
diff
changeset
|
2143 if (PyType_Ready(&PyWrapperDescr_Type) < 0) |
|
42bf56e34825
Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
53243
diff
changeset
|
2144 Py_FatalError("Can't initialize wrapper type"); |
|
42bf56e34825
Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
53243
diff
changeset
|
2145 |
|
42bf56e34825
Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
53243
diff
changeset
|
2146 if (PyType_Ready(&PyInstance_Type) < 0) |
|
42bf56e34825
Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
53243
diff
changeset
|
2147 Py_FatalError("Can't initialize instance type"); |
|
42bf56e34825
Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
53243
diff
changeset
|
2148 |
|
42bf56e34825
Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
53243
diff
changeset
|
2149 if (PyType_Ready(&PyEllipsis_Type) < 0) |
|
42bf56e34825
Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
53243
diff
changeset
|
2150 Py_FatalError("Can't initialize ellipsis type"); |
|
42bf56e34825
Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
53243
diff
changeset
|
2151 |
|
42bf56e34825
Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
53243
diff
changeset
|
2152 if (PyType_Ready(&PyMemberDescr_Type) < 0) |
|
42bf56e34825
Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
53243
diff
changeset
|
2153 Py_FatalError("Can't initialize member descriptor type"); |
|
19294
1456ec735087
Add a function _Py_ReadyTypes() which initializes various and sundry
Guido van Rossum <guido@python.org>
parents:
19293
diff
changeset
|
2154 } |
|
1456ec735087
Add a function _Py_ReadyTypes() which initializes various and sundry
Guido van Rossum <guido@python.org>
parents:
19293
diff
changeset
|
2155 |
| 6 | 2156 |
|
3574
1ec154241747
TRACE_REFS -> Py_TRACE_REFS.
Guido van Rossum <guido@python.org>
parents:
3330
diff
changeset
|
2157 #ifdef Py_TRACE_REFS |
| 6 | 2158 |
|
4002
4241582636ad
Define reference count admin debug functions to return void.
Guido van Rossum <guido@python.org>
parents:
3986
diff
changeset
|
2159 void |
|
13473
31b1f691a7d9
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
2160 _Py_NewReference(PyObject *op) |
| 6 | 2161 { |
|
24184
c4dcb80f48ea
object.h special-build macro minefield: renamed all the new lexical
Tim Peters <tim.peters@gmail.com>
parents:
24155
diff
changeset
|
2162 _Py_INC_REFTOTAL; |
| 6 | 2163 op->ob_refcnt = 1; |
|
28396
d56282c28a1e
Improved new Py_TRACE_REFS gimmicks.
Tim Peters <tim.peters@gmail.com>
parents:
28389
diff
changeset
|
2164 _Py_AddToAllObjects(op, 1); |
|
24184
c4dcb80f48ea
object.h special-build macro minefield: renamed all the new lexical
Tim Peters <tim.peters@gmail.com>
parents:
24155
diff
changeset
|
2165 _Py_INC_TPALLOCS(op); |
| 6 | 2166 } |
| 2167 | |
|
4002
4241582636ad
Define reference count admin debug functions to return void.
Guido van Rossum <guido@python.org>
parents:
3986
diff
changeset
|
2168 void |
|
13473
31b1f691a7d9
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
2169 _Py_ForgetReference(register PyObject *op) |
| 6 | 2170 { |
|
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
|
2171 #ifdef SLOW_UNREF_CHECK |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
2172 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
|
2173 #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
|
2174 if (op->ob_refcnt < 0) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
2175 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
|
2176 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
|
2177 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
|
2178 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
|
2179 #ifdef SLOW_UNREF_CHECK |
| 86 | 2180 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) { |
| 2181 if (p == op) | |
| 2182 break; | |
| 2183 } | |
|
2053
de49d49044d7
Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents:
1869
diff
changeset
|
2184 if (p == &refchain) /* Not found */ |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
2185 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
|
2186 #endif |
| 6 | 2187 op->_ob_next->_ob_prev = op->_ob_prev; |
| 2188 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
|
2189 op->_ob_next = op->_ob_prev = NULL; |
|
24184
c4dcb80f48ea
object.h special-build macro minefield: renamed all the new lexical
Tim Peters <tim.peters@gmail.com>
parents:
24155
diff
changeset
|
2190 _Py_INC_TPFREES(op); |
| 86 | 2191 } |
| 2192 | |
|
4002
4241582636ad
Define reference count admin debug functions to return void.
Guido van Rossum <guido@python.org>
parents:
3986
diff
changeset
|
2193 void |
|
13473
31b1f691a7d9
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
2194 _Py_Dealloc(PyObject *op) |
| 86 | 2195 { |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
43958
diff
changeset
|
2196 destructor dealloc = Py_TYPE(op)->tp_dealloc; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
2197 _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
|
2198 (*dealloc)(op); |
| 6 | 2199 } |
| 2200 | |
|
28653
56006d97bcef
_Py_PrintReferences(): Changed to print object address at start of each
Tim Peters <tim.peters@gmail.com>
parents:
28616
diff
changeset
|
2201 /* Print all live objects. Because PyObject_Print is called, the |
|
56006d97bcef
_Py_PrintReferences(): Changed to print object address at start of each
Tim Peters <tim.peters@gmail.com>
parents:
28616
diff
changeset
|
2202 * interpreter must be in a healthy state. |
|
56006d97bcef
_Py_PrintReferences(): Changed to print object address at start of each
Tim Peters <tim.peters@gmail.com>
parents:
28616
diff
changeset
|
2203 */ |
|
4002
4241582636ad
Define reference count admin debug functions to return void.
Guido van Rossum <guido@python.org>
parents:
3986
diff
changeset
|
2204 void |
|
13473
31b1f691a7d9
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
2205 _Py_PrintReferences(FILE *fp) |
| 6 | 2206 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
2207 PyObject *op; |
|
5931
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
2208 fprintf(fp, "Remaining objects:\n"); |
| 6 | 2209 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) { |
|
37609
e60b31647a6b
_Py_PrintReferenceAddresses,_Py_PrintReferences:
Tim Peters <tim.peters@gmail.com>
parents:
37304
diff
changeset
|
2210 fprintf(fp, "%p [%" PY_FORMAT_SIZE_T "d] ", op, op->ob_refcnt); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
2211 if (PyObject_Print(op, fp, 0) != 0) |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5006
diff
changeset
|
2212 PyErr_Clear(); |
| 6 | 2213 putc('\n', fp); |
| 2214 } | |
| 2215 } | |
| 2216 | |
|
28653
56006d97bcef
_Py_PrintReferences(): Changed to print object address at start of each
Tim Peters <tim.peters@gmail.com>
parents:
28616
diff
changeset
|
2217 /* Print the addresses of all live objects. Unlike _Py_PrintReferences, this |
|
56006d97bcef
_Py_PrintReferences(): Changed to print object address at start of each
Tim Peters <tim.peters@gmail.com>
parents:
28616
diff
changeset
|
2218 * doesn't make any calls to the Python C API, so is always safe to call. |
|
56006d97bcef
_Py_PrintReferences(): Changed to print object address at start of each
Tim Peters <tim.peters@gmail.com>
parents:
28616
diff
changeset
|
2219 */ |
|
56006d97bcef
_Py_PrintReferences(): Changed to print object address at start of each
Tim Peters <tim.peters@gmail.com>
parents:
28616
diff
changeset
|
2220 void |
|
56006d97bcef
_Py_PrintReferences(): Changed to print object address at start of each
Tim Peters <tim.peters@gmail.com>
parents:
28616
diff
changeset
|
2221 _Py_PrintReferenceAddresses(FILE *fp) |
|
56006d97bcef
_Py_PrintReferences(): Changed to print object address at start of each
Tim Peters <tim.peters@gmail.com>
parents:
28616
diff
changeset
|
2222 { |
|
56006d97bcef
_Py_PrintReferences(): Changed to print object address at start of each
Tim Peters <tim.peters@gmail.com>
parents:
28616
diff
changeset
|
2223 PyObject *op; |
|
56006d97bcef
_Py_PrintReferences(): Changed to print object address at start of each
Tim Peters <tim.peters@gmail.com>
parents:
28616
diff
changeset
|
2224 fprintf(fp, "Remaining object addresses:\n"); |
|
56006d97bcef
_Py_PrintReferences(): Changed to print object address at start of each
Tim Peters <tim.peters@gmail.com>
parents:
28616
diff
changeset
|
2225 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) |
|
37609
e60b31647a6b
_Py_PrintReferenceAddresses,_Py_PrintReferences:
Tim Peters <tim.peters@gmail.com>
parents:
37304
diff
changeset
|
2226 fprintf(fp, "%p [%" PY_FORMAT_SIZE_T "d] %s\n", op, |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
43958
diff
changeset
|
2227 op->ob_refcnt, Py_TYPE(op)->tp_name); |
|
28653
56006d97bcef
_Py_PrintReferences(): Changed to print object address at start of each
Tim Peters <tim.peters@gmail.com>
parents:
28616
diff
changeset
|
2228 } |
|
56006d97bcef
_Py_PrintReferences(): Changed to print object address at start of each
Tim Peters <tim.peters@gmail.com>
parents:
28616
diff
changeset
|
2229 |
|
3024
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
2230 PyObject * |
|
13473
31b1f691a7d9
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
2231 _Py_GetObjects(PyObject *self, PyObject *args) |
|
3024
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
2232 { |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
2233 int i, n; |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
2234 PyObject *t = NULL; |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
2235 PyObject *res, *op; |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
2236 |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
2237 if (!PyArg_ParseTuple(args, "i|O", &n, &t)) |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
2238 return NULL; |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
2239 op = refchain._ob_next; |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
2240 res = PyList_New(0); |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
2241 if (res == NULL) |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
2242 return NULL; |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
2243 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
|
2244 while (op == self || op == args || op == res || op == t || |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
43958
diff
changeset
|
2245 (t != NULL && Py_TYPE(op) != (PyTypeObject *) t)) { |
|
3024
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
2246 op = op->_ob_next; |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
2247 if (op == &refchain) |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
2248 return res; |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
2249 } |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
2250 if (PyList_Append(res, op) < 0) { |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
2251 Py_DECREF(res); |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
2252 return NULL; |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
2253 } |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
2254 op = op->_ob_next; |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
2255 } |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
2256 return res; |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
2257 } |
|
54aa87b1ccee
Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents:
2827
diff
changeset
|
2258 |
| 6 | 2259 #endif |
|
3330
9f1b6e2db8e0
Hack to force loading of cobject.o
Guido van Rossum <guido@python.org>
parents:
3024
diff
changeset
|
2260 |
|
9f1b6e2db8e0
Hack to force loading of cobject.o
Guido van Rossum <guido@python.org>
parents:
3024
diff
changeset
|
2261 |
|
9f1b6e2db8e0
Hack to force loading of cobject.o
Guido van Rossum <guido@python.org>
parents:
3024
diff
changeset
|
2262 /* Hack to force loading of cobject.o */ |
| 4588 | 2263 PyTypeObject *_Py_cobject_hack = &PyCObject_Type; |
|
3574
1ec154241747
TRACE_REFS -> Py_TRACE_REFS.
Guido van Rossum <guido@python.org>
parents:
3330
diff
changeset
|
2264 |
|
1ec154241747
TRACE_REFS -> Py_TRACE_REFS.
Guido van Rossum <guido@python.org>
parents:
3330
diff
changeset
|
2265 |
|
1ec154241747
TRACE_REFS -> Py_TRACE_REFS.
Guido van Rossum <guido@python.org>
parents:
3330
diff
changeset
|
2266 /* Hack to force loading of abstract.o */ |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
2267 Py_ssize_t (*_Py_abstract_hack)(PyObject *) = PyObject_Size; |
|
5931
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
2268 |
|
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
2269 |
| 14006 | 2270 /* 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
|
2271 |
|
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
|
2272 void * |
|
13473
31b1f691a7d9
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
2273 PyMem_Malloc(size_t nbytes) |
|
5931
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
2274 { |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
2275 return PyMem_MALLOC(nbytes); |
|
5931
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
2276 } |
|
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
2277 |
|
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
|
2278 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
|
2279 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
|
2280 { |
|
22985
d9d82c1ef864
First stab at rationalizing the PyMem_ API. Mixing PyObject_xyz with
Tim Peters <tim.peters@gmail.com>
parents:
22982
diff
changeset
|
2281 return PyMem_REALLOC(p, nbytes); |
|
5931
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
2282 } |
|
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
2283 |
|
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
2284 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
|
2285 PyMem_Free(void *p) |
|
5931
12fe5cc1516d
Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
2286 { |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
2287 PyMem_FREE(p); |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
2288 } |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
2289 |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12481
diff
changeset
|
2290 |
|
7984
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
2291 /* 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
|
2292 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
|
2293 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
|
2294 Py_ReprLeave() to avoid infinite recursion. |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
2295 |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
2296 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
|
2297 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
|
2298 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
|
2299 |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
2300 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
|
2301 */ |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
2302 |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
2303 #define KEY "Py_Repr" |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
2304 |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
2305 int |
|
13473
31b1f691a7d9
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
2306 Py_ReprEnter(PyObject *obj) |
|
7984
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
2307 { |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
2308 PyObject *dict; |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
2309 PyObject *list; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
2310 Py_ssize_t i; |
|
7984
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
2311 |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
2312 dict = PyThreadState_GetDict(); |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
2313 if (dict == NULL) |
|
28616
4f0b7acffc7d
- pythunrun.c, Py_Finalize(): move the call to _Py_PrintReferences()
Guido van Rossum <guido@python.org>
parents:
28397
diff
changeset
|
2314 return 0; |
|
7984
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
2315 list = PyDict_GetItemString(dict, KEY); |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
2316 if (list == NULL) { |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
2317 list = PyList_New(0); |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
2318 if (list == NULL) |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
2319 return -1; |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
2320 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
|
2321 return -1; |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
2322 Py_DECREF(list); |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
2323 } |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
2324 i = PyList_GET_SIZE(list); |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
2325 while (--i >= 0) { |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
2326 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
|
2327 return 1; |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
2328 } |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
2329 PyList_Append(list, obj); |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
2330 return 0; |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
2331 } |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
2332 |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
2333 void |
|
13473
31b1f691a7d9
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
2334 Py_ReprLeave(PyObject *obj) |
|
7984
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
2335 { |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
2336 PyObject *dict; |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
2337 PyObject *list; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
2338 Py_ssize_t i; |
|
7984
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
2339 |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
2340 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
|
2341 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
|
2342 return; |
|
7984
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
2343 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
|
2344 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
|
2345 return; |
|
7984
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
2346 i = PyList_GET_SIZE(list); |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
2347 /* 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
|
2348 while (--i >= 0) { |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
2349 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
|
2350 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
|
2351 break; |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
2352 } |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
2353 } |
|
1d0eb2e96d76
Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents:
7935
diff
changeset
|
2354 } |
|
11999
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
2355 |
|
24122
d332484e394a
Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents:
24121
diff
changeset
|
2356 /* Trashcan support. */ |
| 12052 | 2357 |
|
24122
d332484e394a
Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents:
24121
diff
changeset
|
2358 /* Current call-stack depth of tp_dealloc calls. */ |
|
11999
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
2359 int _PyTrash_delete_nesting = 0; |
|
12481
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
2360 |
|
24122
d332484e394a
Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents:
24121
diff
changeset
|
2361 /* List of objects that still need to be cleaned up, singly linked via their |
|
d332484e394a
Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents:
24121
diff
changeset
|
2362 * gc headers' gc_prev pointers. |
|
d332484e394a
Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents:
24121
diff
changeset
|
2363 */ |
|
d332484e394a
Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents:
24121
diff
changeset
|
2364 PyObject *_PyTrash_delete_later = NULL; |
|
11999
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
2365 |
|
24122
d332484e394a
Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents:
24121
diff
changeset
|
2366 /* Add op to the _PyTrash_delete_later list. Called when the current |
|
d332484e394a
Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents:
24121
diff
changeset
|
2367 * call-stack depth gets large. op must be a currently untracked gc'ed |
|
d332484e394a
Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents:
24121
diff
changeset
|
2368 * object, with refcount 0. Py_DECREF must already have been called on it. |
|
d332484e394a
Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents:
24121
diff
changeset
|
2369 */ |
|
11999
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
2370 void |
|
13473
31b1f691a7d9
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
2371 _PyTrash_deposit_object(PyObject *op) |
|
11999
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
2372 { |
|
24122
d332484e394a
Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents:
24121
diff
changeset
|
2373 assert(PyObject_IS_GC(op)); |
|
d332484e394a
Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents:
24121
diff
changeset
|
2374 assert(_Py_AS_GC(op)->gc.gc_refs == _PyGC_REFS_UNTRACKED); |
|
d332484e394a
Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents:
24121
diff
changeset
|
2375 assert(op->ob_refcnt == 0); |
|
22649
1616bd8b31bc
If the GC is enabled then don't use the ob_type pointer to create a list
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22469
diff
changeset
|
2376 _Py_AS_GC(op)->gc.gc_prev = (PyGC_Head *)_PyTrash_delete_later; |
|
12481
a8898a172fe2
Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents:
12406
diff
changeset
|
2377 _PyTrash_delete_later = op; |
|
11999
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
2378 } |
|
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
2379 |
|
24122
d332484e394a
Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents:
24121
diff
changeset
|
2380 /* Dealloccate all the objects in the _PyTrash_delete_later list. Called when |
|
d332484e394a
Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents:
24121
diff
changeset
|
2381 * the call-stack unwinds again. |
|
d332484e394a
Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents:
24121
diff
changeset
|
2382 */ |
|
11999
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
2383 void |
|
13473
31b1f691a7d9
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
2384 _PyTrash_destroy_chain(void) |
|
11999
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
2385 { |
|
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
2386 while (_PyTrash_delete_later) { |
|
24122
d332484e394a
Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents:
24121
diff
changeset
|
2387 PyObject *op = _PyTrash_delete_later; |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
43958
diff
changeset
|
2388 destructor dealloc = Py_TYPE(op)->tp_dealloc; |
|
22649
1616bd8b31bc
If the GC is enabled then don't use the ob_type pointer to create a list
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22469
diff
changeset
|
2389 |
|
1616bd8b31bc
If the GC is enabled then don't use the ob_type pointer to create a list
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22469
diff
changeset
|
2390 _PyTrash_delete_later = |
|
24122
d332484e394a
Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents:
24121
diff
changeset
|
2391 (PyObject*) _Py_AS_GC(op)->gc.gc_prev; |
|
22649
1616bd8b31bc
If the GC is enabled then don't use the ob_type pointer to create a list
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22469
diff
changeset
|
2392 |
|
24122
d332484e394a
Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents:
24121
diff
changeset
|
2393 /* Call the deallocator directly. This used to try to |
|
d332484e394a
Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents:
24121
diff
changeset
|
2394 * fool Py_DECREF into calling it indirectly, but |
|
d332484e394a
Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents:
24121
diff
changeset
|
2395 * Py_DECREF was already called on this object, and in |
|
d332484e394a
Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents:
24121
diff
changeset
|
2396 * assorted non-release builds calling Py_DECREF again ends |
|
d332484e394a
Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents:
24121
diff
changeset
|
2397 * up distorting allocation statistics. |
|
d332484e394a
Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents:
24121
diff
changeset
|
2398 */ |
|
d332484e394a
Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents:
24121
diff
changeset
|
2399 assert(op->ob_refcnt == 0); |
|
11999
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
2400 ++_PyTrash_delete_nesting; |
|
24122
d332484e394a
Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents:
24121
diff
changeset
|
2401 (*dealloc)(op); |
|
11999
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
2402 --_PyTrash_delete_nesting; |
|
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
2403 } |
|
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
2404 } |
|
37655
2f3f2e32dfd9
spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37638
diff
changeset
|
2405 |
|
2f3f2e32dfd9
spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37638
diff
changeset
|
2406 #ifdef __cplusplus |
|
2f3f2e32dfd9
spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37638
diff
changeset
|
2407 } |
|
2f3f2e32dfd9
spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37638
diff
changeset
|
2408 #endif |
