annotate Objects/object.c @ 56121:ea697cde6882 3.1

Merged revisions 74881-74885,74888,74891-74895,74900,74905-74906,74931-74932,74934,74941-74942,74944,74956 via svnmerge from svn+ssh://pythondev@svn.python.org/python/branches/py3k ........ r74881 | r.david.murray | 2009-09-17 15:12:49 -0400 (Thu, 17 Sep 2009) | 2 lines Check in first draft of maintainers.rst. ........ r74882 | eric.smith | 2009-09-17 15:22:30 -0400 (Thu, 17 Sep 2009) | 1 line Typo. ........ r74883 | mark.dickinson | 2009-09-17 15:37:28 -0400 (Thu, 17 Sep 2009) | 1 line Add some more module maintainers. ........ r74884 | mark.dickinson | 2009-09-17 15:39:12 -0400 (Thu, 17 Sep 2009) | 1 line Revert accidental changes to Objects/longobject.c ........ r74885 | mark.dickinson | 2009-09-17 16:20:01 -0400 (Thu, 17 Sep 2009) | 1 line Add decimal maintainers ........ r74888 | r.david.murray | 2009-09-17 18:10:48 -0400 (Thu, 17 Sep 2009) | 2 lines Maintainer additions from MAL. ........ r74891 | georg.brandl | 2009-09-17 18:18:01 -0400 (Thu, 17 Sep 2009) | 1 line Some more maintainers. ........ r74892 | r.david.murray | 2009-09-17 19:23:56 -0400 (Thu, 17 Sep 2009) | 2 lines Add a couple interest areas for Nick per his request. ........ r74893 | r.david.murray | 2009-09-17 19:43:20 -0400 (Thu, 17 Sep 2009) | 2 lines Benajmin is also compiler-knowledgeable. ........ r74894 | alexandre.vassalotti | 2009-09-17 20:59:05 -0400 (Thu, 17 Sep 2009) | 2 lines Added myself to my areas of interest. ........ r74895 | brett.cannon | 2009-09-17 21:03:35 -0400 (Thu, 17 Sep 2009) | 1 line Add myself to a couple places as maintainer. ........ r74900 | georg.brandl | 2009-09-18 05:06:37 -0400 (Fri, 18 Sep 2009) | 1 line Add myself in interest areas. ........ r74905 | ezio.melotti | 2009-09-18 05:58:43 -0400 (Fri, 18 Sep 2009) | 1 line Add myself in interest areas and mark effbot as inactive in winsound ........ r74906 | antoine.pitrou | 2009-09-18 09:15:23 -0400 (Fri, 18 Sep 2009) | 3 lines Add myself in a couple of places ........ r74931 | r.david.murray | 2009-09-18 17:25:36 -0400 (Fri, 18 Sep 2009) | 5 lines Add some names gleaned from the "Tracker Archeology" thread on python-dev from back in February. Also consolidate the autoconf and makefiles topics into one, and add tracker and svn/hg topics. ........ r74932 | r.david.murray | 2009-09-18 17:32:16 -0400 (Fri, 18 Sep 2009) | 2 lines Remove non-committer mistakenly added. ........ r74934 | r.david.murray | 2009-09-18 17:40:30 -0400 (Fri, 18 Sep 2009) | 2 lines More descriptive/generic name for the svn/hg entry (version control) ........ r74941 | benjamin.peterson | 2009-09-18 18:45:59 -0400 (Fri, 18 Sep 2009) | 1 line add a new category for myself ........ r74942 | benjamin.peterson | 2009-09-18 18:50:55 -0400 (Fri, 18 Sep 2009) | 1 line add myself for symtable ........ r74944 | georg.brandl | 2009-09-19 03:39:05 -0400 (Sat, 19 Sep 2009) | 1 line I would like to maintain the token module! ........ r74956 | r.david.murray | 2009-09-19 14:40:08 -0400 (Sat, 19 Sep 2009) | 3 lines Add everyone who we know has been a release manager as a resource for the 'release management' topic. ........
author R. David Murray <rdmurray@bitdance.com>
date Sat, 19 Sep 2009 19:04:37 +0000
parents 4c51c4bffb21
children 3ec6b51cdbdd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 95
diff changeset
1
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
2 /* Generic object operations; and implementation of None (NoObject) */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5006
diff changeset
4 #include "Python.h"
39536
8f5b9982beba Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
5 #include "sliceobject.h" /* For PyEllipsis_Type */
53274
8533cfc2420a Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 53244
diff changeset
6 #include "frameobject.h"
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
7
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36977
diff changeset
8 #ifdef __cplusplus
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36977
diff changeset
9 extern "C" {
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36977
diff changeset
10 #endif
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36977
diff changeset
11
24184
c4dcb80f48ea object.h special-build macro minefield: renamed all the new lexical
Tim Peters <tim.peters@gmail.com>
parents: 24155
diff changeset
12 #ifdef Py_REF_DEBUG
36838
dedc2c94d112 Oops, forgot to include this in the last checkin.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36759
diff changeset
13 Py_ssize_t _Py_RefTotal;
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36977
diff changeset
14
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36977
diff changeset
15 Py_ssize_t
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36977
diff changeset
16 _Py_GetRefTotal(void)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36977
diff changeset
17 {
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36977
diff changeset
18 PyObject *o;
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36977
diff changeset
19 Py_ssize_t total = _Py_RefTotal;
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36977
diff changeset
20 /* ignore the references to the dummy object of the dicts and sets
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36977
diff changeset
21 because they are not reliable and not useful (now that the
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36977
diff changeset
22 hash table code is well-tested) */
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36977
diff changeset
23 o = _PyDict_Dummy();
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36977
diff changeset
24 if (o != NULL)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36977
diff changeset
25 total -= o->ob_refcnt;
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36977
diff changeset
26 o = _PySet_Dummy();
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36977
diff changeset
27 if (o != NULL)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36977
diff changeset
28 total -= o->ob_refcnt;
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36977
diff changeset
29 return total;
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36977
diff changeset
30 }
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36977
diff changeset
31 #endif /* Py_REF_DEBUG */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
32
24420
36e2fd3f08f7 Excise DL_IMPORT/EXPORT from object.h, and related files. This patch
Mark Hammond <mhammond@skippinet.com.au>
parents: 24184
diff changeset
33 int Py_DivisionWarningFlag;
19633
c0a44829bcd5 Add warning mode for classic division, almost exactly as specified in
Guido van Rossum <guido@python.org>
parents: 19620
diff changeset
34
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
35 /* Object allocation routines used by NEWOBJ and NEWVAROBJ macros.
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
36 These are used by the individual routines for object creation.
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
37 Do not call them otherwise, they do not initialize the object! */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
4f038d3a8878 Typo in comment.
Tim Peters <tim.peters@gmail.com>
parents: 28396
diff changeset
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;
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36977
diff changeset
79 /* All types are added to type_list, at least when
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36977
diff changeset
80 they get one object created. That makes them
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36977
diff changeset
81 immortal, which unfortunately contributes to
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36977
diff changeset
82 garbage itself. If unlist_types_without_objects
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36977
diff changeset
83 is set, they will be removed from the type_list
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36977
diff changeset
84 once the last object is deallocated. */
50735
27db82a5cda9 Merged revisions 68381 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50374
diff changeset
85 static int unlist_types_without_objects;
27db82a5cda9 Merged revisions 68381 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50374
diff changeset
86 extern Py_ssize_t tuple_zero_allocs, fast_tuple_allocs;
27db82a5cda9 Merged revisions 68381 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50374
diff changeset
87 extern Py_ssize_t quick_int_allocs, quick_neg_int_allocs;
27db82a5cda9 Merged revisions 68381 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50374
diff changeset
88 extern Py_ssize_t 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
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36977
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)
50735
27db82a5cda9 Merged revisions 68381 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50374
diff changeset
95 fprintf(f, "%s alloc'd: %" PY_FORMAT_SIZE_T "d, "
27db82a5cda9 Merged revisions 68381 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50374
diff changeset
96 "freed: %" PY_FORMAT_SIZE_T "d, "
27db82a5cda9 Merged revisions 68381 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50374
diff changeset
97 "max in use: %" PY_FORMAT_SIZE_T "d\n",
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
98 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
99 tp->tp_maxalloc);
50735
27db82a5cda9 Merged revisions 68381 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50374
diff changeset
100 fprintf(f, "fast tuple allocs: %" PY_FORMAT_SIZE_T "d, "
27db82a5cda9 Merged revisions 68381 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50374
diff changeset
101 "empty: %" PY_FORMAT_SIZE_T "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
102 fast_tuple_allocs, tuple_zero_allocs);
50735
27db82a5cda9 Merged revisions 68381 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50374
diff changeset
103 fprintf(f, "fast int allocs: pos: %" PY_FORMAT_SIZE_T "d, "
27db82a5cda9 Merged revisions 68381 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50374
diff changeset
104 "neg: %" PY_FORMAT_SIZE_T "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
105 quick_int_allocs, quick_neg_int_allocs);
50735
27db82a5cda9 Merged revisions 68381 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50374
diff changeset
106 fprintf(f, "null strings: %" PY_FORMAT_SIZE_T "d, "
27db82a5cda9 Merged revisions 68381 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50374
diff changeset
107 "1-strings: %" PY_FORMAT_SIZE_T "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
108 null_strings, one_strings);
1347
a22a350d3a5d * Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents: 1324
diff changeset
109 }
a22a350d3a5d * Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents: 1324
diff changeset
110
3024
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
111 PyObject *
13473
31b1f691a7d9 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
112 get_counts(void)
3024
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
113 {
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
114 PyTypeObject *tp;
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
115 PyObject *result;
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
116 PyObject *v;
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
117
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
118 result = PyList_New(0);
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
119 if (result == NULL)
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
120 return NULL;
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
121 for (tp = type_list; tp; tp = tp->tp_next) {
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 37927
diff changeset
122 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
123 tp->tp_frees, tp->tp_maxalloc);
3024
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
124 if (v == NULL) {
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 if (PyList_Append(result, v) < 0) {
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
129 Py_DECREF(v);
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
130 Py_DECREF(result);
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
131 return NULL;
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
132 }
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
133 Py_DECREF(v);
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
134 }
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
135 return result;
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
136 }
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
137
1347
a22a350d3a5d * Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents: 1324
diff changeset
138 void
13473
31b1f691a7d9 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
139 inc_count(PyTypeObject *tp)
1347
a22a350d3a5d * Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents: 1324
diff changeset
140 {
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36977
diff changeset
141 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
142 /* 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
143 if (tp->tp_next != NULL) /* sanity check */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5006
diff changeset
144 Py_FatalError("XXX inc_count sanity check");
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36977
diff changeset
145 if (type_list)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36977
diff changeset
146 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
147 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
148 /* 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
149 * 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
150 * 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
151 * 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
152 * 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
153 * (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
154 * 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
155 */
d7d816e1f522 SF bug 578752: COUNT_ALLOCS vs heap types
Tim Peters <tim.peters@gmail.com>
parents: 24126
diff changeset
156 Py_INCREF(tp);
1347
a22a350d3a5d * Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents: 1324
diff changeset
157 type_list = tp;
28388
f81239c12c60 Oops! Used a wrong preprocessor symbol.
Tim Peters <tim.peters@gmail.com>
parents: 28387
diff changeset
158 #ifdef Py_TRACE_REFS
28396
d56282c28a1e Improved new Py_TRACE_REFS gimmicks.
Tim Peters <tim.peters@gmail.com>
parents: 28389
diff changeset
159 /* 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
160 * if not already there.
d56282c28a1e Improved new Py_TRACE_REFS gimmicks.
Tim Peters <tim.peters@gmail.com>
parents: 28389
diff changeset
161 */
d56282c28a1e Improved new Py_TRACE_REFS gimmicks.
Tim Peters <tim.peters@gmail.com>
parents: 28389
diff changeset
162 _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
163 #endif
1347
a22a350d3a5d * Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents: 1324
diff changeset
164 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
165 tp->tp_allocs++;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
166 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
167 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
168 }
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36977
diff changeset
169
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36977
diff changeset
170 void dec_count(PyTypeObject *tp)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36977
diff changeset
171 {
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36977
diff changeset
172 tp->tp_frees++;
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36977
diff changeset
173 if (unlist_types_without_objects &&
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36977
diff changeset
174 tp->tp_allocs == tp->tp_frees) {
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36977
diff changeset
175 /* unlink the type from type_list */
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36977
diff changeset
176 if (tp->tp_prev)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36977
diff changeset
177 tp->tp_prev->tp_next = tp->tp_next;
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36977
diff changeset
178 else
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36977
diff changeset
179 type_list = tp->tp_next;
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36977
diff changeset
180 if (tp->tp_next)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36977
diff changeset
181 tp->tp_next->tp_prev = tp->tp_prev;
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36977
diff changeset
182 tp->tp_next = tp->tp_prev = NULL;
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36977
diff changeset
183 Py_DECREF(tp);
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36977
diff changeset
184 }
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36977
diff changeset
185 }
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36977
diff changeset
186
1347
a22a350d3a5d * Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents: 1324
diff changeset
187 #endif
a22a350d3a5d * Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents: 1324
diff changeset
188
24155
9456a2ccda61 The Py_REF_DEBUG/COUNT_ALLOCS/Py_TRACE_REFS macro minefield: added
Tim Peters <tim.peters@gmail.com>
parents: 24144
diff changeset
189 #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
190 /* 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
191 void
9456a2ccda61 The Py_REF_DEBUG/COUNT_ALLOCS/Py_TRACE_REFS macro minefield: added
Tim Peters <tim.peters@gmail.com>
parents: 24144
diff changeset
192 _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
193 {
9456a2ccda61 The Py_REF_DEBUG/COUNT_ALLOCS/Py_TRACE_REFS macro minefield: added
Tim Peters <tim.peters@gmail.com>
parents: 24144
diff changeset
194 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
195
9456a2ccda61 The Py_REF_DEBUG/COUNT_ALLOCS/Py_TRACE_REFS macro minefield: added
Tim Peters <tim.peters@gmail.com>
parents: 24144
diff changeset
196 PyOS_snprintf(buf, sizeof(buf),
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36977
diff changeset
197 "%s:%i object at %p has negative ref count "
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36977
diff changeset
198 "%" PY_FORMAT_SIZE_T "d",
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36977
diff changeset
199 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
200 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
201 }
9456a2ccda61 The Py_REF_DEBUG/COUNT_ALLOCS/Py_TRACE_REFS macro minefield: added
Tim Peters <tim.peters@gmail.com>
parents: 24144
diff changeset
202
9456a2ccda61 The Py_REF_DEBUG/COUNT_ALLOCS/Py_TRACE_REFS macro minefield: added
Tim Peters <tim.peters@gmail.com>
parents: 24144
diff changeset
203 #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
204
31940
059740d52ae5 Two new public API functions, Py_IncRef and Py_DecRef. Useful for
Thomas Heller <theller@ctypes.org>
parents: 31738
diff changeset
205 void
059740d52ae5 Two new public API functions, Py_IncRef and Py_DecRef. Useful for
Thomas Heller <theller@ctypes.org>
parents: 31738
diff changeset
206 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
207 {
059740d52ae5 Two new public API functions, Py_IncRef and Py_DecRef. Useful for
Thomas Heller <theller@ctypes.org>
parents: 31738
diff changeset
208 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
209 }
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 void
059740d52ae5 Two new public API functions, Py_IncRef and Py_DecRef. Useful for
Thomas Heller <theller@ctypes.org>
parents: 31738
diff changeset
212 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
213 {
059740d52ae5 Two new public API functions, Py_IncRef and Py_DecRef. Useful for
Thomas Heller <theller@ctypes.org>
parents: 31738
diff changeset
214 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
215 }
059740d52ae5 Two new public API functions, Py_IncRef and Py_DecRef. Useful for
Thomas Heller <theller@ctypes.org>
parents: 31738
diff changeset
216
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5006
diff changeset
217 PyObject *
13473
31b1f691a7d9 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
218 PyObject_Init(PyObject *op, PyTypeObject *tp)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
219 {
25737
b0fa5f6a8bb9 PyObject_Init[Var] is almost always called from the PyObject_NEW[_VAR]
Guido van Rossum <guido@python.org>
parents: 25015
diff changeset
220 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
221 return PyErr_NoMemory();
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12481
diff changeset
222 /* Any changes should be reflected in PyObject_INIT (objimpl.h) */
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44064
diff changeset
223 Py_TYPE(op) = tp;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5006
diff changeset
224 _Py_NewReference(op);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
225 return op;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
226 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
227
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12481
diff changeset
228 PyVarObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
229 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
230 {
25737
b0fa5f6a8bb9 PyObject_Init[Var] is almost always called from the PyObject_NEW[_VAR]
Guido van Rossum <guido@python.org>
parents: 25015
diff changeset
231 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
232 return (PyVarObject *) PyErr_NoMemory();
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12481
diff changeset
233 /* 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
234 op->ob_size = size;
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44064
diff changeset
235 Py_TYPE(op) = tp;
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12481
diff changeset
236 _Py_NewReference((PyObject *)op);
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12481
diff changeset
237 return op;
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12481
diff changeset
238 }
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12481
diff changeset
239
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12481
diff changeset
240 PyObject *
13473
31b1f691a7d9 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
241 _PyObject_New(PyTypeObject *tp)
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12481
diff changeset
242 {
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12481
diff changeset
243 PyObject *op;
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12481
diff changeset
244 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
245 if (op == NULL)
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12481
diff changeset
246 return PyErr_NoMemory();
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12481
diff changeset
247 return PyObject_INIT(op, tp);
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12481
diff changeset
248 }
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12481
diff changeset
249
5535
20c2b715188e Oops, another forgotten renaming: varobject -> PyVarObject.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
250 PyVarObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
251 _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
252 {
5535
20c2b715188e Oops, another forgotten renaming: varobject -> PyVarObject.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
253 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
254 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
255 op = (PyVarObject *) PyObject_MALLOC(size);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
256 if (op == NULL)
5535
20c2b715188e Oops, another forgotten renaming: varobject -> PyVarObject.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
257 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
258 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
259 }
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12481
diff changeset
260
27234
e9f45b8efd6b Fix SF bug #667147, Segmentation fault printing str subclass
Neal Norwitz <nnorwitz@gmail.com>
parents: 26263
diff changeset
261 /* 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
262 static int
e9f45b8efd6b Fix SF bug #667147, Segmentation fault printing str subclass
Neal Norwitz <nnorwitz@gmail.com>
parents: 26263
diff changeset
263 internal_print(PyObject *op, FILE *fp, int flags, int nesting)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
264 {
322
1a6cf1f8ccec Check for write errors after printing a value
Guido van Rossum <guido@python.org>
parents: 307
diff changeset
265 int ret = 0;
27234
e9f45b8efd6b Fix SF bug #667147, Segmentation fault printing str subclass
Neal Norwitz <nnorwitz@gmail.com>
parents: 26263
diff changeset
266 if (nesting > 10) {
e9f45b8efd6b Fix SF bug #667147, Segmentation fault printing str subclass
Neal Norwitz <nnorwitz@gmail.com>
parents: 26263
diff changeset
267 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
268 return -1;
e9f45b8efd6b Fix SF bug #667147, Segmentation fault printing str subclass
Neal Norwitz <nnorwitz@gmail.com>
parents: 26263
diff changeset
269 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5006
diff changeset
270 if (PyErr_CheckSignals())
284
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 231
diff changeset
271 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
272 #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
273 if (PyOS_CheckStack()) {
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14869
diff changeset
274 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
275 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
276 }
c69ad566aeb1 If USE_STACKCHECK is defined use PyOS_CheckStack() in the repr and str
Guido van Rossum <guido@python.org>
parents: 8111
diff changeset
277 #endif
11649
38560a3a3249 On Linux, one sometimes sees spurious errors after interrupting
Guido van Rossum <guido@python.org>
parents: 8695
diff changeset
278 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
279 if (op == NULL) {
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 42854
diff changeset
280 Py_BEGIN_ALLOW_THREADS
284
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 231
diff changeset
281 fprintf(fp, "<nil>");
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 42854
diff changeset
282 Py_END_ALLOW_THREADS
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
283 }
284
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 231
diff changeset
284 else {
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 231
diff changeset
285 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
286 /* 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
287 universally available */
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 42854
diff changeset
288 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
289 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
290 (long)op->ob_refcnt, op);
43400
1bac386c17cc Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 42854
diff changeset
291 Py_END_ALLOW_THREADS
42477
e7fd327e0c19 Kill all uses and definitions of tp_print under Objects/. (Others will follow.)
Guido van Rossum <guido@python.org>
parents: 42334
diff changeset
292 else {
17646
d1ec679baa3d Printing objects to a real file still wasn't done right: if the
Guido van Rossum <guido@python.org>
parents: 17639
diff changeset
293 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
294 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
295 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
296 else
42508
522a3e3a03a9 Oops. The PyObject_Print() function was totally broken; the original code
Guido van Rossum <guido@python.org>
parents: 42477
diff changeset
297 s = PyObject_Repr(op);
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
298 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
299 ret = -1;
47301
23ab586c427a Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents: 47300
diff changeset
300 else if (PyBytes_Check(s)) {
23ab586c427a Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents: 47300
diff changeset
301 fwrite(PyBytes_AS_STRING(s), 1,
23ab586c427a Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents: 47300
diff changeset
302 PyBytes_GET_SIZE(s), fp);
42508
522a3e3a03a9 Oops. The PyObject_Print() function was totally broken; the original code
Guido van Rossum <guido@python.org>
parents: 42477
diff changeset
303 }
522a3e3a03a9 Oops. The PyObject_Print() function was totally broken; the original code
Guido van Rossum <guido@python.org>
parents: 42477
diff changeset
304 else if (PyUnicode_Check(s)) {
522a3e3a03a9 Oops. The PyObject_Print() function was totally broken; the original code
Guido van Rossum <guido@python.org>
parents: 42477
diff changeset
305 PyObject *t;
522a3e3a03a9 Oops. The PyObject_Print() function was totally broken; the original code
Guido van Rossum <guido@python.org>
parents: 42477
diff changeset
306 t = _PyUnicode_AsDefaultEncodedString(s, NULL);
522a3e3a03a9 Oops. The PyObject_Print() function was totally broken; the original code
Guido van Rossum <guido@python.org>
parents: 42477
diff changeset
307 if (t == NULL)
522a3e3a03a9 Oops. The PyObject_Print() function was totally broken; the original code
Guido van Rossum <guido@python.org>
parents: 42477
diff changeset
308 ret = 0;
522a3e3a03a9 Oops. The PyObject_Print() function was totally broken; the original code
Guido van Rossum <guido@python.org>
parents: 42477
diff changeset
309 else {
47301
23ab586c427a Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents: 47300
diff changeset
310 fwrite(PyBytes_AS_STRING(t), 1,
23ab586c427a Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents: 47300
diff changeset
311 PyBytes_GET_SIZE(t), fp);
42508
522a3e3a03a9 Oops. The PyObject_Print() function was totally broken; the original code
Guido van Rossum <guido@python.org>
parents: 42477
diff changeset
312 }
522a3e3a03a9 Oops. The PyObject_Print() function was totally broken; the original code
Guido van Rossum <guido@python.org>
parents: 42477
diff changeset
313 }
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
314 else {
42508
522a3e3a03a9 Oops. The PyObject_Print() function was totally broken; the original code
Guido van Rossum <guido@python.org>
parents: 42477
diff changeset
315 PyErr_Format(PyExc_TypeError,
522a3e3a03a9 Oops. The PyObject_Print() function was totally broken; the original code
Guido van Rossum <guido@python.org>
parents: 42477
diff changeset
316 "str() or repr() returned '%.100s'",
522a3e3a03a9 Oops. The PyObject_Print() function was totally broken; the original code
Guido van Rossum <guido@python.org>
parents: 42477
diff changeset
317 s->ob_type->tp_name);
522a3e3a03a9 Oops. The PyObject_Print() function was totally broken; the original code
Guido van Rossum <guido@python.org>
parents: 42477
diff changeset
318 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
319 }
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
320 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
321 }
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
322 }
322
1a6cf1f8ccec Check for write errors after printing a value
Guido van Rossum <guido@python.org>
parents: 307
diff changeset
323 if (ret == 0) {
1a6cf1f8ccec Check for write errors after printing a value
Guido van Rossum <guido@python.org>
parents: 307
diff changeset
324 if (ferror(fp)) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5006
diff changeset
325 PyErr_SetFromErrno(PyExc_IOError);
322
1a6cf1f8ccec Check for write errors after printing a value
Guido van Rossum <guido@python.org>
parents: 307
diff changeset
326 clearerr(fp);
1a6cf1f8ccec Check for write errors after printing a value
Guido van Rossum <guido@python.org>
parents: 307
diff changeset
327 ret = -1;
1a6cf1f8ccec Check for write errors after printing a value
Guido van Rossum <guido@python.org>
parents: 307
diff changeset
328 }
1a6cf1f8ccec Check for write errors after printing a value
Guido van Rossum <guido@python.org>
parents: 307
diff changeset
329 }
1a6cf1f8ccec Check for write errors after printing a value
Guido van Rossum <guido@python.org>
parents: 307
diff changeset
330 return ret;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
331 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
332
27234
e9f45b8efd6b Fix SF bug #667147, Segmentation fault printing str subclass
Neal Norwitz <nnorwitz@gmail.com>
parents: 26263
diff changeset
333 int
e9f45b8efd6b Fix SF bug #667147, Segmentation fault printing str subclass
Neal Norwitz <nnorwitz@gmail.com>
parents: 26263
diff changeset
334 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
335 {
e9f45b8efd6b Fix SF bug #667147, Segmentation fault printing str subclass
Neal Norwitz <nnorwitz@gmail.com>
parents: 26263
diff changeset
336 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
337 }
e9f45b8efd6b Fix SF bug #667147, Segmentation fault printing str subclass
Neal Norwitz <nnorwitz@gmail.com>
parents: 26263
diff changeset
338
39623
c6d9fa81f20f Change the way __hash__ is inherited; when __eq__ or __cmp__ is overridden
Guido van Rossum <guido@python.org>
parents: 39610
diff changeset
339 /* For debugging convenience. Set a breakpoint here and call it from your DLL */
c6d9fa81f20f Change the way __hash__ is inherited; when __eq__ or __cmp__ is overridden
Guido van Rossum <guido@python.org>
parents: 39610
diff changeset
340 void
40737
6ebad2a6b990 Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40485
diff changeset
341 _Py_BreakPoint(void)
39623
c6d9fa81f20f Change the way __hash__ is inherited; when __eq__ or __cmp__ is overridden
Guido van Rossum <guido@python.org>
parents: 39610
diff changeset
342 {
c6d9fa81f20f Change the way __hash__ is inherited; when __eq__ or __cmp__ is overridden
Guido van Rossum <guido@python.org>
parents: 39610
diff changeset
343 }
c6d9fa81f20f Change the way __hash__ is inherited; when __eq__ or __cmp__ is overridden
Guido van Rossum <guido@python.org>
parents: 39610
diff changeset
344
27234
e9f45b8efd6b Fix SF bug #667147, Segmentation fault printing str subclass
Neal Norwitz <nnorwitz@gmail.com>
parents: 26263
diff changeset
345
16165
0a72070eab6e A few miscellaneous helpers.
Barry Warsaw <barry@python.org>
parents: 16123
diff changeset
346 /* For debugging convenience. See Misc/gdbinit for some useful gdb hooks */
39623
c6d9fa81f20f Change the way __hash__ is inherited; when __eq__ or __cmp__ is overridden
Guido van Rossum <guido@python.org>
parents: 39610
diff changeset
347 void
c6d9fa81f20f Change the way __hash__ is inherited; when __eq__ or __cmp__ is overridden
Guido van Rossum <guido@python.org>
parents: 39610
diff changeset
348 _PyObject_Dump(PyObject* op)
16165
0a72070eab6e A few miscellaneous helpers.
Barry Warsaw <barry@python.org>
parents: 16123
diff changeset
349 {
16742
db61894b0d6f _PyObject_Dump(): If argument is NULL, print "NULL" instead of
Barry Warsaw <barry@python.org>
parents: 16741
diff changeset
350 if (op == NULL)
db61894b0d6f _PyObject_Dump(): If argument is NULL, print "NULL" instead of
Barry Warsaw <barry@python.org>
parents: 16741
diff changeset
351 fprintf(stderr, "NULL\n");
db61894b0d6f _PyObject_Dump(): If argument is NULL, print "NULL" instead of
Barry Warsaw <barry@python.org>
parents: 16741
diff changeset
352 else {
52870
98f42392835b Merge revision 71222 from trunk: #5615: make it possible to configure --without-threads again.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
353 #ifdef WITH_THREAD
50250
1adfabcbe073 Merged revisions 67886-67887 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49721
diff changeset
354 PyGILState_STATE gil;
52870
98f42392835b Merge revision 71222 from trunk: #5615: make it possible to configure --without-threads again.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
355 #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
356 fprintf(stderr, "object : ");
52870
98f42392835b Merge revision 71222 from trunk: #5615: make it possible to configure --without-threads again.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
357 #ifdef WITH_THREAD
50250
1adfabcbe073 Merged revisions 67886-67887 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49721
diff changeset
358 gil = PyGILState_Ensure();
52870
98f42392835b Merge revision 71222 from trunk: #5615: make it possible to configure --without-threads again.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
359 #endif
16742
db61894b0d6f _PyObject_Dump(): If argument is NULL, print "NULL" instead of
Barry Warsaw <barry@python.org>
parents: 16741
diff changeset
360 (void)PyObject_Print(op, stderr, 0);
52870
98f42392835b Merge revision 71222 from trunk: #5615: make it possible to configure --without-threads again.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
361 #ifdef WITH_THREAD
50250
1adfabcbe073 Merged revisions 67886-67887 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49721
diff changeset
362 PyGILState_Release(gil);
52870
98f42392835b Merge revision 71222 from trunk: #5615: make it possible to configure --without-threads again.
Georg Brandl <georg@python.org>
parents: 51776
diff changeset
363 #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
364 /* 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
365 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
366 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
367 "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
368 "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
369 "address : %p\n",
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44064
diff changeset
370 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
371 (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
372 op);
16742
db61894b0d6f _PyObject_Dump(): If argument is NULL, print "NULL" instead of
Barry Warsaw <barry@python.org>
parents: 16741
diff changeset
373 }
16165
0a72070eab6e A few miscellaneous helpers.
Barry Warsaw <barry@python.org>
parents: 16123
diff changeset
374 }
16167
71ceaba3f615 PyObject_Dump(): Use %p format to print the address of the pointer.
Barry Warsaw <barry@python.org>
parents: 16165
diff changeset
375
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5006
diff changeset
376 PyObject *
13473
31b1f691a7d9 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
377 PyObject_Repr(PyObject *v)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
378 {
42854
824b9d52e006 Use unicode and remove support for some uses of str8.
Neal Norwitz <nnorwitz@gmail.com>
parents: 42508
diff changeset
379 PyObject *res;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5006
diff changeset
380 if (PyErr_CheckSignals())
284
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 231
diff changeset
381 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
382 #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
383 if (PyOS_CheckStack()) {
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14869
diff changeset
384 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
385 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
386 }
c69ad566aeb1 If USE_STACKCHECK is defined use PyOS_CheckStack() in the repr and str
Guido van Rossum <guido@python.org>
parents: 8111
diff changeset
387 #endif
284
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 231
diff changeset
388 if (v == NULL)
41769
b29d71d43c80 Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents: 41700
diff changeset
389 return PyUnicode_FromString("<NULL>");
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44064
diff changeset
390 if (Py_TYPE(v)->tp_repr == NULL)
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43734
diff changeset
391 return PyUnicode_FromFormat("<%s object at %p>",
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43734
diff changeset
392 v->ob_type->tp_name, v);
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43734
diff changeset
393 res = (*v->ob_type->tp_repr)(v);
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43734
diff changeset
394 if (res != NULL && !PyUnicode_Check(res)) {
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43734
diff changeset
395 PyErr_Format(PyExc_TypeError,
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43734
diff changeset
396 "__repr__ returned non-string (type %.200s)",
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43734
diff changeset
397 res->ob_type->tp_name);
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43734
diff changeset
398 Py_DECREF(res);
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43734
diff changeset
399 return NULL;
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43734
diff changeset
400 }
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43734
diff changeset
401 return res;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
402 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
403
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5006
diff changeset
404 PyObject *
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43734
diff changeset
405 PyObject_Str(PyObject *v)
1400
aac8231a715c * mpzmodule.c: removed redundant mpz_print function.
Guido van Rossum <guido@python.org>
parents: 1361
diff changeset
406 {
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
407 PyObject *res;
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43734
diff changeset
408 if (PyErr_CheckSignals())
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43734
diff changeset
409 return NULL;
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43734
diff changeset
410 #ifdef USE_STACKCHECK
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43734
diff changeset
411 if (PyOS_CheckStack()) {
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43734
diff changeset
412 PyErr_SetString(PyExc_MemoryError, "stack overflow");
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43734
diff changeset
413 return NULL;
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43734
diff changeset
414 }
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43734
diff changeset
415 #endif
1400
aac8231a715c * mpzmodule.c: removed redundant mpz_print function.
Guido van Rossum <guido@python.org>
parents: 1361
diff changeset
416 if (v == NULL)
42854
824b9d52e006 Use unicode and remove support for some uses of str8.
Neal Norwitz <nnorwitz@gmail.com>
parents: 42508
diff changeset
417 return PyUnicode_FromString("<NULL>");
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
418 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
419 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
420 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
421 }
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44064
diff changeset
422 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
423 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
424
43734
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43718
diff changeset
425 /* It is possible for a type to have a tp_str representation that loops
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43718
diff changeset
426 infinitely. */
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43718
diff changeset
427 if (Py_EnterRecursiveCall(" while getting the str of an object"))
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43718
diff changeset
428 return NULL;
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44064
diff changeset
429 res = (*Py_TYPE(v)->tp_str)(v);
43734
3e5edf19d5f0 Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 43718
diff changeset
430 Py_LeaveRecursiveCall();
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
431 if (res == NULL)
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
432 return NULL;
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43734
diff changeset
433 if (!PyUnicode_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
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)",
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44064
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
47611
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47301
diff changeset
443 PyObject *
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47301
diff changeset
444 PyObject_ASCII(PyObject *v)
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47301
diff changeset
445 {
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47301
diff changeset
446 PyObject *repr, *ascii, *res;
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47301
diff changeset
447
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47301
diff changeset
448 repr = PyObject_Repr(v);
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47301
diff changeset
449 if (repr == NULL)
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47301
diff changeset
450 return NULL;
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47301
diff changeset
451
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47301
diff changeset
452 /* repr is guaranteed to be a PyUnicode object by PyObject_Repr */
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47301
diff changeset
453 ascii = PyUnicode_EncodeASCII(
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47301
diff changeset
454 PyUnicode_AS_UNICODE(repr),
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47301
diff changeset
455 PyUnicode_GET_SIZE(repr),
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47301
diff changeset
456 "backslashreplace");
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47301
diff changeset
457
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47301
diff changeset
458 Py_DECREF(repr);
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47301
diff changeset
459 if (ascii == NULL)
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47301
diff changeset
460 return NULL;
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47301
diff changeset
461
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47301
diff changeset
462 res = PyUnicode_DecodeASCII(
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47301
diff changeset
463 PyBytes_AS_STRING(ascii),
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47301
diff changeset
464 PyBytes_GET_SIZE(ascii),
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47301
diff changeset
465 NULL);
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47301
diff changeset
466
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47301
diff changeset
467 Py_DECREF(ascii);
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47301
diff changeset
468 return res;
da47e7e135ae #2630: Implement PEP 3138.
Georg Brandl <georg@python.org>
parents: 47301
diff changeset
469 }
16001
f3740674ac5b Changes to recursive-object comparisons, having to do with a test case
Guido van Rossum <guido@python.org>
parents: 15962
diff changeset
470
48871
548dccc4008e make bytes(o) respect __bytes__ #2415
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
471 PyObject *
548dccc4008e make bytes(o) respect __bytes__ #2415
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
472 PyObject_Bytes(PyObject *v)
548dccc4008e make bytes(o) respect __bytes__ #2415
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
473 {
48885
47192d0b731f Removed bytesmeth declaration in OyObject_Bytes. It's not used any more and causes a compiler warning.
Christian Heimes <christian@cheimes.de>
parents: 48871
diff changeset
474 PyObject *result, *func;
48871
548dccc4008e make bytes(o) respect __bytes__ #2415
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
475 static PyObject *bytesstring = NULL;
548dccc4008e make bytes(o) respect __bytes__ #2415
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
476
548dccc4008e make bytes(o) respect __bytes__ #2415
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
477 if (v == NULL)
548dccc4008e make bytes(o) respect __bytes__ #2415
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
478 return PyBytes_FromString("<NULL>");
548dccc4008e make bytes(o) respect __bytes__ #2415
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
479
548dccc4008e make bytes(o) respect __bytes__ #2415
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
480 if (PyBytes_CheckExact(v)) {
548dccc4008e make bytes(o) respect __bytes__ #2415
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
481 Py_INCREF(v);
548dccc4008e make bytes(o) respect __bytes__ #2415
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
482 return v;
548dccc4008e make bytes(o) respect __bytes__ #2415
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
483 }
548dccc4008e make bytes(o) respect __bytes__ #2415
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
484
53905
2993f049e2c9 Merged revisions 72461 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 53816
diff changeset
485 func = _PyObject_LookupSpecial(v, "__bytes__", &bytesstring);
48871
548dccc4008e make bytes(o) respect __bytes__ #2415
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
486 if (func != NULL) {
53906
5420dae355f7 this is now a bound method
Benjamin Peterson <benjamin@python.org>
parents: 53905
diff changeset
487 result = PyObject_CallFunctionObjArgs(func, NULL);
53905
2993f049e2c9 Merged revisions 72461 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 53816
diff changeset
488 Py_DECREF(func);
48871
548dccc4008e make bytes(o) respect __bytes__ #2415
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
489 if (result == NULL)
548dccc4008e make bytes(o) respect __bytes__ #2415
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
490 return NULL;
548dccc4008e make bytes(o) respect __bytes__ #2415
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
491 if (!PyBytes_Check(result)) {
548dccc4008e make bytes(o) respect __bytes__ #2415
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
492 PyErr_Format(PyExc_TypeError,
548dccc4008e make bytes(o) respect __bytes__ #2415
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
493 "__bytes__ returned non-bytes (type %.200s)",
548dccc4008e make bytes(o) respect __bytes__ #2415
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
494 Py_TYPE(result)->tp_name);
548dccc4008e make bytes(o) respect __bytes__ #2415
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
495 Py_DECREF(result);
548dccc4008e make bytes(o) respect __bytes__ #2415
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
496 return NULL;
548dccc4008e make bytes(o) respect __bytes__ #2415
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
497 }
548dccc4008e make bytes(o) respect __bytes__ #2415
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
498 return result;
548dccc4008e make bytes(o) respect __bytes__ #2415
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
499 }
54326
db212869d814 Merged revisions 72907 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 53953
diff changeset
500 else if (PyErr_Occurred())
db212869d814 Merged revisions 72907 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 53953
diff changeset
501 return NULL;
48871
548dccc4008e make bytes(o) respect __bytes__ #2415
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
502 return PyBytes_FromObject(v);
548dccc4008e make bytes(o) respect __bytes__ #2415
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
503 }
548dccc4008e make bytes(o) respect __bytes__ #2415
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
504
51247
ba94d7c095e8 Issue #1717, continued: remove PyObject_Compare and Py_CmpToRich declarations
Mark Dickinson <dickinsm@gmail.com>
parents: 51245
diff changeset
505 /* For Python 3.0.1 and later, the old three-way comparison has been
ba94d7c095e8 Issue #1717, continued: remove PyObject_Compare and Py_CmpToRich declarations
Mark Dickinson <dickinsm@gmail.com>
parents: 51245
diff changeset
506 completely removed in favour of rich comparisons. PyObject_Compare() and
ba94d7c095e8 Issue #1717, continued: remove PyObject_Compare and Py_CmpToRich declarations
Mark Dickinson <dickinsm@gmail.com>
parents: 51245
diff changeset
507 PyObject_Cmp() are gone, and the builtin cmp function no longer exists.
51270
debd14c489b9 Issue #1717: rename tp_compare to tp_reserved. I'll change the
Mark Dickinson <dickinsm@gmail.com>
parents: 51247
diff changeset
508 The old tp_compare slot has been renamed to tp_reserved, and should no
51247
ba94d7c095e8 Issue #1717, continued: remove PyObject_Compare and Py_CmpToRich declarations
Mark Dickinson <dickinsm@gmail.com>
parents: 51245
diff changeset
509 longer be used. Use tp_richcompare instead.
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43734
diff changeset
510
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39623
diff changeset
511 See (*) below for practical amendments.
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39623
diff changeset
512
51247
ba94d7c095e8 Issue #1717, continued: remove PyObject_Compare and Py_CmpToRich declarations
Mark Dickinson <dickinsm@gmail.com>
parents: 51245
diff changeset
513 tp_richcompare gets called with a first argument of the appropriate type
ba94d7c095e8 Issue #1717, continued: remove PyObject_Compare and Py_CmpToRich declarations
Mark Dickinson <dickinsm@gmail.com>
parents: 51245
diff changeset
514 and a second object of an arbitrary type. We never do any kind of
ba94d7c095e8 Issue #1717, continued: remove PyObject_Compare and Py_CmpToRich declarations
Mark Dickinson <dickinsm@gmail.com>
parents: 51245
diff changeset
515 coercion.
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39623
diff changeset
516
51247
ba94d7c095e8 Issue #1717, continued: remove PyObject_Compare and Py_CmpToRich declarations
Mark Dickinson <dickinsm@gmail.com>
parents: 51245
diff changeset
517 The tp_richcompare slot should return an object, as follows:
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39623
diff changeset
518
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39623
diff changeset
519 NULL if an exception occurred
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39623
diff changeset
520 NotImplemented if the requested comparison is not implemented
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39623
diff changeset
521 any other false value if the requested comparison is false
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39623
diff changeset
522 any other true value if the requested comparison is true
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39623
diff changeset
523
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39623
diff changeset
524 The PyObject_RichCompare[Bool]() wrappers raise TypeError when they get
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39623
diff changeset
525 NotImplemented.
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39623
diff changeset
526
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39623
diff changeset
527 (*) Practical amendments:
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39623
diff changeset
528
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39623
diff changeset
529 - If rich comparison returns NotImplemented, == and != are decided by
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39623
diff changeset
530 comparing the object pointer (i.e. falling back to the base object
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39623
diff changeset
531 implementation).
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39623
diff changeset
532
23598
2b1d61781d63 Implement the intention of SF patch 472523 (but coded differently).
Guido van Rossum <guido@python.org>
parents: 23542
diff changeset
533 */
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39623
diff changeset
534
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39623
diff changeset
535 /* 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
536 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
537
40485
d4fcb5656316 The opstrings table was backwards. Add an XXX comment.
Guido van Rossum <guido@python.org>
parents: 40401
diff changeset
538 static char *opstrings[] = {"<", "<=", "==", "!=", ">", ">="};
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39623
diff changeset
539
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39623
diff changeset
540 /* Perform a rich comparison, raising TypeError when the requested comparison
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39623
diff changeset
541 operator is not supported. */
15941
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
542 static PyObject *
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39623
diff changeset
543 do_richcompare(PyObject *v, PyObject *w, int op)
15941
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
544 {
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
545 richcmpfunc f;
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
546 PyObject *res;
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
547
20140
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20002
diff changeset
548 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
549 PyType_IsSubtype(w->ob_type, v->ob_type) &&
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39623
diff changeset
550 (f = w->ob_type->tp_richcompare) != 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
551 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
552 if (res != Py_NotImplemented)
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20002
diff changeset
553 return res;
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20002
diff changeset
554 Py_DECREF(res);
225f17ec8932 Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents: 20002
diff changeset
555 }
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39623
diff changeset
556 if ((f = v->ob_type->tp_richcompare) != NULL) {
15941
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
557 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
558 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
559 return res;
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
560 Py_DECREF(res);
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
561 }
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39623
diff changeset
562 if ((f = w->ob_type->tp_richcompare) != NULL) {
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39623
diff changeset
563 res = (*f)(w, v, _Py_SwappedOp[op]);
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39623
diff changeset
564 if (res != Py_NotImplemented)
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39623
diff changeset
565 return res;
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39623
diff changeset
566 Py_DECREF(res);
15941
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
567 }
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39623
diff changeset
568 /* If neither object implements it, provide a sensible default
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39623
diff changeset
569 for == and !=, but raise an exception for ordering. */
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39623
diff changeset
570 switch (op) {
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39623
diff changeset
571 case Py_EQ:
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39623
diff changeset
572 res = (v == w) ? Py_True : Py_False;
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39623
diff changeset
573 break;
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39623
diff changeset
574 case Py_NE:
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39623
diff changeset
575 res = (v != w) ? Py_True : Py_False;
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39623
diff changeset
576 break;
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39623
diff changeset
577 default:
40485
d4fcb5656316 The opstrings table was backwards. Add an XXX comment.
Guido van Rossum <guido@python.org>
parents: 40401
diff changeset
578 /* XXX Special-case None so it doesn't show as NoneType() */
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39623
diff changeset
579 PyErr_Format(PyExc_TypeError,
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39623
diff changeset
580 "unorderable types: %.100s() %s %.100s()",
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39623
diff changeset
581 v->ob_type->tp_name,
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39623
diff changeset
582 opstrings[op],
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39623
diff changeset
583 w->ob_type->tp_name);
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39623
diff changeset
584 return NULL;
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39623
diff changeset
585 }
15941
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
586 Py_INCREF(res);
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
587 return res;
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
588 }
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
589
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39623
diff changeset
590 /* Perform a rich comparison with object result. This wraps do_richcompare()
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39623
diff changeset
591 with a check for NULL arguments and a recursion check. */
15941
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
592
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
593 PyObject *
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
594 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
595 {
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
596 PyObject *res;
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
597
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
598 assert(Py_LT <= op && op <= Py_GE);
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39623
diff changeset
599 if (v == NULL || w == NULL) {
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39623
diff changeset
600 if (!PyErr_Occurred())
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39623
diff changeset
601 PyErr_BadInternalCall();
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39623
diff changeset
602 return NULL;
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39623
diff changeset
603 }
30801
c42c40034536 Deleting cyclic object comparison.
Armin Rigo <arigo@tunes.org>
parents: 30159
diff changeset
604 if (Py_EnterRecursiveCall(" in cmp"))
c42c40034536 Deleting cyclic object comparison.
Armin Rigo <arigo@tunes.org>
parents: 30159
diff changeset
605 return NULL;
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39623
diff changeset
606 res = do_richcompare(v, w, op);
30801
c42c40034536 Deleting cyclic object comparison.
Armin Rigo <arigo@tunes.org>
parents: 30159
diff changeset
607 Py_LeaveRecursiveCall();
15941
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
608 return res;
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
609 }
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
610
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39623
diff changeset
611 /* Perform a rich comparison with integer result. This wraps
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39623
diff changeset
612 PyObject_RichCompare(), returning -1 for error, 0 for false, 1 for true. */
15941
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
613 int
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
614 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
615 {
31736
6fcbf6ac699a Add identity shortcut to PyObject_RichCompareBool.
Raymond Hettinger <python@rcn.com>
parents: 31433
diff changeset
616 PyObject *res;
15941
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
617 int ok;
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
618
49721
98628dc730da Issue #4296: Fix PyObject_RichCompareBool so that "x in [x]" evaluates to
Mark Dickinson <dickinsm@gmail.com>
parents: 48885
diff changeset
619 /* Quick result when objects are the same.
98628dc730da Issue #4296: Fix PyObject_RichCompareBool so that "x in [x]" evaluates to
Mark Dickinson <dickinsm@gmail.com>
parents: 48885
diff changeset
620 Guarantees that identity implies equality. */
98628dc730da Issue #4296: Fix PyObject_RichCompareBool so that "x in [x]" evaluates to
Mark Dickinson <dickinsm@gmail.com>
parents: 48885
diff changeset
621 if (v == w) {
98628dc730da Issue #4296: Fix PyObject_RichCompareBool so that "x in [x]" evaluates to
Mark Dickinson <dickinsm@gmail.com>
parents: 48885
diff changeset
622 if (op == Py_EQ)
98628dc730da Issue #4296: Fix PyObject_RichCompareBool so that "x in [x]" evaluates to
Mark Dickinson <dickinsm@gmail.com>
parents: 48885
diff changeset
623 return 1;
98628dc730da Issue #4296: Fix PyObject_RichCompareBool so that "x in [x]" evaluates to
Mark Dickinson <dickinsm@gmail.com>
parents: 48885
diff changeset
624 else if (op == Py_NE)
98628dc730da Issue #4296: Fix PyObject_RichCompareBool so that "x in [x]" evaluates to
Mark Dickinson <dickinsm@gmail.com>
parents: 48885
diff changeset
625 return 0;
98628dc730da Issue #4296: Fix PyObject_RichCompareBool so that "x in [x]" evaluates to
Mark Dickinson <dickinsm@gmail.com>
parents: 48885
diff changeset
626 }
98628dc730da Issue #4296: Fix PyObject_RichCompareBool so that "x in [x]" evaluates to
Mark Dickinson <dickinsm@gmail.com>
parents: 48885
diff changeset
627
31736
6fcbf6ac699a Add identity shortcut to PyObject_RichCompareBool.
Raymond Hettinger <python@rcn.com>
parents: 31433
diff changeset
628 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
629 if (res == NULL)
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
630 return -1;
25013
68627e0acd15 Speedup for PyObject_RichCompareBool(): PyObject_RichCompare() almost
Guido van Rossum <guido@python.org>
parents: 24897
diff changeset
631 if (PyBool_Check(res))
68627e0acd15 Speedup for PyObject_RichCompareBool(): PyObject_RichCompare() almost
Guido van Rossum <guido@python.org>
parents: 24897
diff changeset
632 ok = (res == Py_True);
68627e0acd15 Speedup for PyObject_RichCompareBool(): PyObject_RichCompare() almost
Guido van Rossum <guido@python.org>
parents: 24897
diff changeset
633 else
68627e0acd15 Speedup for PyObject_RichCompareBool(): PyObject_RichCompare() almost
Guido van Rossum <guido@python.org>
parents: 24897
diff changeset
634 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
635 Py_DECREF(res);
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
636 return ok;
5abae795742e Rich comparisons. Refactored internal routine do_cmp() and added APIs
Guido van Rossum <guido@python.org>
parents: 15726
diff changeset
637 }
13141
cca84745da3e This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents: 13089
diff changeset
638
cca84745da3e This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents: 13089
diff changeset
639 /* Set of hash utility functions to help maintaining the invariant that
31738
dba8db412e35 Fix typo in comment.
Raymond Hettinger <python@rcn.com>
parents: 31736
diff changeset
640 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
641
cca84745da3e This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents: 13089
diff changeset
642 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
643 */
cca84745da3e This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents: 13089
diff changeset
644
cca84745da3e This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents: 13089
diff changeset
645 long
13473
31b1f691a7d9 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
646 _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
647 {
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
648 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
649 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
650 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
651 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
652 /* 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
653 * 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
654 * 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
655 */
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
656
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
657 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
658 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
659 /* This must return the same hash as an equal int or long. */
51469
224d0efcbf6c Merged revisions 69436 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 51270
diff changeset
660 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
661 /* 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
662 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
663 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
664 /* 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
665 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
666 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
667 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
668 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
669 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
670 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
671 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
672 }
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
673 /* 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
674 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
675 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
676 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
677 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
678 }
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
679 /* 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
680 * 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
681 * 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
682 * 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
683 * 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
684 * 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
685 * 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
686 * 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
687 * 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
688 * 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
689 * 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
690 */
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
691 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
692 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
693 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
694 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
695 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
696 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
697 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
698 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
699 }
cca84745da3e This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents: 13089
diff changeset
700
cca84745da3e This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents: 13089
diff changeset
701 long
13473
31b1f691a7d9 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
702 _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
703 {
cca84745da3e This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents: 13089
diff changeset
704 long x;
51603
ff096d6a9222 Merged revisions 69582-69583 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51469
diff changeset
705 size_t y = (size_t)p;
ff096d6a9222 Merged revisions 69582-69583 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51469
diff changeset
706 /* bottom 3 or 4 bits are likely to be 0; rotate y by 4 to avoid
ff096d6a9222 Merged revisions 69582-69583 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51469
diff changeset
707 excessive hash collisions for dicts and sets */
ff096d6a9222 Merged revisions 69582-69583 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51469
diff changeset
708 y = (y >> 4) | (y << (8 * SIZEOF_VOID_P - 4));
ff096d6a9222 Merged revisions 69582-69583 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51469
diff changeset
709 x = (long)y;
ff096d6a9222 Merged revisions 69582-69583 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51469
diff changeset
710 if (x == -1)
ff096d6a9222 Merged revisions 69582-69583 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 51469
diff changeset
711 x = -2;
13141
cca84745da3e This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents: 13089
diff changeset
712 return x;
cca84745da3e This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents: 13089
diff changeset
713 }
cca84745da3e This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents: 13089
diff changeset
714
48475
f4e7ef4329a9 Manual forward port of 64962 - use PyObject_HashNotImplemented as a tp_hash level indicator that the default hash implementation has not been inherited
Nick Coghlan <ncoghlan@gmail.com>
parents: 47611
diff changeset
715 long
f4e7ef4329a9 Manual forward port of 64962 - use PyObject_HashNotImplemented as a tp_hash level indicator that the default hash implementation has not been inherited
Nick Coghlan <ncoghlan@gmail.com>
parents: 47611
diff changeset
716 PyObject_HashNotImplemented(PyObject *v)
f4e7ef4329a9 Manual forward port of 64962 - use PyObject_HashNotImplemented as a tp_hash level indicator that the default hash implementation has not been inherited
Nick Coghlan <ncoghlan@gmail.com>
parents: 47611
diff changeset
717 {
f4e7ef4329a9 Manual forward port of 64962 - use PyObject_HashNotImplemented as a tp_hash level indicator that the default hash implementation has not been inherited
Nick Coghlan <ncoghlan@gmail.com>
parents: 47611
diff changeset
718 PyErr_Format(PyExc_TypeError, "unhashable type: '%.200s'",
f4e7ef4329a9 Manual forward port of 64962 - use PyObject_HashNotImplemented as a tp_hash level indicator that the default hash implementation has not been inherited
Nick Coghlan <ncoghlan@gmail.com>
parents: 47611
diff changeset
719 Py_TYPE(v)->tp_name);
f4e7ef4329a9 Manual forward port of 64962 - use PyObject_HashNotImplemented as a tp_hash level indicator that the default hash implementation has not been inherited
Nick Coghlan <ncoghlan@gmail.com>
parents: 47611
diff changeset
720 return -1;
f4e7ef4329a9 Manual forward port of 64962 - use PyObject_HashNotImplemented as a tp_hash level indicator that the default hash implementation has not been inherited
Nick Coghlan <ncoghlan@gmail.com>
parents: 47611
diff changeset
721 }
13141
cca84745da3e This patch addresses two main issues: (1) There exist some non-fatal
Fred Drake <fdrake@acm.org>
parents: 13089
diff changeset
722
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
723 long
13473
31b1f691a7d9 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
724 PyObject_Hash(PyObject *v)
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
725 {
48475
f4e7ef4329a9 Manual forward port of 64962 - use PyObject_HashNotImplemented as a tp_hash level indicator that the default hash implementation has not been inherited
Nick Coghlan <ncoghlan@gmail.com>
parents: 47611
diff changeset
726 PyTypeObject *tp = Py_TYPE(v);
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
727 if (tp->tp_hash != NULL)
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
728 return (*tp->tp_hash)(v);
50374
43aacbe647cb Recorded merge of revisions 68058 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 50250
diff changeset
729 /* To keep to the general practice that inheriting
43aacbe647cb Recorded merge of revisions 68058 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 50250
diff changeset
730 * solely from object in C code should work without
43aacbe647cb Recorded merge of revisions 68058 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 50250
diff changeset
731 * an explicit call to PyType_Ready, we implicitly call
43aacbe647cb Recorded merge of revisions 68058 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 50250
diff changeset
732 * PyType_Ready here and then check the tp_hash slot again
43aacbe647cb Recorded merge of revisions 68058 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 50250
diff changeset
733 */
43aacbe647cb Recorded merge of revisions 68058 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 50250
diff changeset
734 if (tp->tp_dict == NULL) {
43aacbe647cb Recorded merge of revisions 68058 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 50250
diff changeset
735 if (PyType_Ready(tp) < 0)
43aacbe647cb Recorded merge of revisions 68058 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 50250
diff changeset
736 return -1;
43aacbe647cb Recorded merge of revisions 68058 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 50250
diff changeset
737 if (tp->tp_hash != NULL)
43aacbe647cb Recorded merge of revisions 68058 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 50250
diff changeset
738 return (*tp->tp_hash)(v);
43aacbe647cb Recorded merge of revisions 68058 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 50250
diff changeset
739 }
39536
8f5b9982beba Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
740 /* Otherwise, the object can't be hashed */
48475
f4e7ef4329a9 Manual forward port of 64962 - use PyObject_HashNotImplemented as a tp_hash level indicator that the default hash implementation has not been inherited
Nick Coghlan <ncoghlan@gmail.com>
parents: 47611
diff changeset
741 return PyObject_HashNotImplemented(v);
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
742 }
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
743
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5006
diff changeset
744 PyObject *
35999
9541369b0dca Add const to several API functions that take char *.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 35331
diff changeset
745 PyObject_GetAttrString(PyObject *v, const char *name)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
746 {
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
747 PyObject *w, *res;
3986
9c2cbf306864 Support for tp_getattro, tp_setattro (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 3770
diff changeset
748
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44064
diff changeset
749 if (Py_TYPE(v)->tp_getattr != NULL)
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44064
diff changeset
750 return (*Py_TYPE(v)->tp_getattr)(v, (char*)name);
42013
cd125fe83051 Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents: 41769
diff changeset
751 w = PyUnicode_InternFromString(name);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
752 if (w == NULL)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
753 return NULL;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
754 res = PyObject_GetAttr(v, w);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
755 Py_XDECREF(w);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
756 return res;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
757 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
758
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
759 int
35999
9541369b0dca Add const to several API functions that take char *.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 35331
diff changeset
760 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
761 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5006
diff changeset
762 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
763 if (res != NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5006
diff changeset
764 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
765 return 1;
716be765a79b * accessobject.c (ownercheck): allow a base class access to protected
Guido van Rossum <guido@python.org>
parents: 1282
diff changeset
766 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5006
diff changeset
767 PyErr_Clear();
1324
716be765a79b * accessobject.c (ownercheck): allow a base class access to protected
Guido van Rossum <guido@python.org>
parents: 1282
diff changeset
768 return 0;
716be765a79b * accessobject.c (ownercheck): allow a base class access to protected
Guido van Rossum <guido@python.org>
parents: 1282
diff changeset
769 }
716be765a79b * accessobject.c (ownercheck): allow a base class access to protected
Guido van Rossum <guido@python.org>
parents: 1282
diff changeset
770
716be765a79b * accessobject.c (ownercheck): allow a base class access to protected
Guido van Rossum <guido@python.org>
parents: 1282
diff changeset
771 int
35999
9541369b0dca Add const to several API functions that take char *.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 35331
diff changeset
772 PyObject_SetAttrString(PyObject *v, const char *name, PyObject *w)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
773 {
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
774 PyObject *s;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
775 int res;
3986
9c2cbf306864 Support for tp_getattro, tp_setattro (Sjoerd)
Guido van Rossum <guido@python.org>
parents: 3770
diff changeset
776
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44064
diff changeset
777 if (Py_TYPE(v)->tp_setattr != NULL)
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44064
diff changeset
778 return (*Py_TYPE(v)->tp_setattr)(v, (char*)name, w);
42013
cd125fe83051 Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents: 41769
diff changeset
779 s = PyUnicode_InternFromString(name);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
780 if (s == NULL)
95
a5bf7d5959ce Return -1 for errors.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
781 return -1;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
782 res = PyObject_SetAttr(v, s, w);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
783 Py_XDECREF(s);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
784 return res;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
785 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
786
5560
8ec298b255a2 Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents: 5535
diff changeset
787 PyObject *
13473
31b1f691a7d9 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
788 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
789 {
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44064
diff changeset
790 PyTypeObject *tp = Py_TYPE(v);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
791
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42196
diff changeset
792 if (!PyUnicode_Check(name)) {
42013
cd125fe83051 Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents: 41769
diff changeset
793 PyErr_Format(PyExc_TypeError,
cd125fe83051 Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents: 41769
diff changeset
794 "attribute name must be string, not '%.200s'",
cd125fe83051 Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents: 41769
diff changeset
795 name->ob_type->tp_name);
cd125fe83051 Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents: 41769
diff changeset
796 return NULL;
12991
1a40f00a48bf raise TypeError when PyObject_Get/SetAttr called with non-string name
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12904
diff changeset
797 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
798 if (tp->tp_getattro != NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
799 return (*tp->tp_getattro)(v, name);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
800 if (tp->tp_getattr != NULL)
48660
85b67fb5672b Rename PyUnicode_AsString -> _PyUnicode_AsString and
Marc-André Lemburg <mal@egenix.com>
parents: 48475
diff changeset
801 return (*tp->tp_getattr)(v, _PyUnicode_AsString(name));
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
802 PyErr_Format(PyExc_AttributeError,
42039
f83cc9b31a97 Simplify error formatting.
Walter Dörwald <walter@livinglogic.de>
parents: 42013
diff changeset
803 "'%.50s' object has no attribute '%U'",
f83cc9b31a97 Simplify error formatting.
Walter Dörwald <walter@livinglogic.de>
parents: 42013
diff changeset
804 tp->tp_name, name);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
805 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
806 }
8ec298b255a2 Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents: 5535
diff changeset
807
8ec298b255a2 Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents: 5535
diff changeset
808 int
13473
31b1f691a7d9 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
809 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
810 {
8ec298b255a2 Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents: 5535
diff changeset
811 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
812 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
813 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
814 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
815 }
8ec298b255a2 Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents: 5535
diff changeset
816 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
817 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
818 }
8ec298b255a2 Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents: 5535
diff changeset
819
8ec298b255a2 Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents: 5535
diff changeset
820 int
13473
31b1f691a7d9 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
821 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
822 {
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44064
diff changeset
823 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
824 int err;
14660
983502b2cd43 PyObject_SetAttr() and PyObject_GetAttr() now also accept Unicode
Marc-André Lemburg <mal@egenix.com>
parents: 14375
diff changeset
825
42013
cd125fe83051 Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents: 41769
diff changeset
826 if (!PyUnicode_Check(name)) {
cd125fe83051 Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents: 41769
diff changeset
827 PyErr_Format(PyExc_TypeError,
cd125fe83051 Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents: 41769
diff changeset
828 "attribute name must be string, not '%.200s'",
cd125fe83051 Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents: 41769
diff changeset
829 name->ob_type->tp_name);
cd125fe83051 Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents: 41769
diff changeset
830 return -1;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
831 }
41595
2c54763c4504 More coding by random modification.
Guido van Rossum <guido@python.org>
parents: 41587
diff changeset
832 Py_INCREF(name);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
833
42013
cd125fe83051 Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents: 41769
diff changeset
834 PyUnicode_InternInPlace(&name);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
835 if (tp->tp_setattro != NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
836 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
837 Py_DECREF(name);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
838 return err;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
839 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
840 if (tp->tp_setattr != NULL) {
48660
85b67fb5672b Rename PyUnicode_AsString -> _PyUnicode_AsString and
Marc-André Lemburg <mal@egenix.com>
parents: 48475
diff changeset
841 err = (*tp->tp_setattr)(v, _PyUnicode_AsString(name), value);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
842 Py_DECREF(name);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
843 return err;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
844 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
845 Py_DECREF(name);
41595
2c54763c4504 More coding by random modification.
Guido van Rossum <guido@python.org>
parents: 41587
diff changeset
846 assert(name->ob_refcnt >= 1);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
847 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
848 PyErr_Format(PyExc_TypeError,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
849 "'%.100s' object has no attributes "
42039
f83cc9b31a97 Simplify error formatting.
Walter Dörwald <walter@livinglogic.de>
parents: 42013
diff changeset
850 "(%s .%U)",
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
851 tp->tp_name,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
852 value==NULL ? "del" : "assign to",
42039
f83cc9b31a97 Simplify error formatting.
Walter Dörwald <walter@livinglogic.de>
parents: 42013
diff changeset
853 name);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
854 else
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
855 PyErr_Format(PyExc_TypeError,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
856 "'%.100s' object has only read-only attributes "
42039
f83cc9b31a97 Simplify error formatting.
Walter Dörwald <walter@livinglogic.de>
parents: 42013
diff changeset
857 "(%s .%U)",
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
858 tp->tp_name,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
859 value==NULL ? "del" : "assign to",
42039
f83cc9b31a97 Simplify error formatting.
Walter Dörwald <walter@livinglogic.de>
parents: 42013
diff changeset
860 name);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
861 return -1;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
862 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
863
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
864 /* 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
865
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
866 PyObject **
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
867 _PyObject_GetDictPtr(PyObject *obj)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
868 {
36866
9eb24f92a784 Change int to Py_ssize_t in several places.
Martin v. Löwis <martin@v.loewis.de>
parents: 36838
diff changeset
869 Py_ssize_t dictoffset;
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44064
diff changeset
870 PyTypeObject *tp = Py_TYPE(obj);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
871
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
872 dictoffset = tp->tp_dictoffset;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
873 if (dictoffset == 0)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
874 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
875 if (dictoffset < 0) {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
876 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
877 size_t size;
93eb4b61f687 SF patch 514641 (Naofumi Honda) - Negative ob_size of LongObjects
Guido van Rossum <guido@python.org>
parents: 21186
diff changeset
878
93eb4b61f687 SF patch 514641 (Naofumi Honda) - Negative ob_size of LongObjects
Guido van Rossum <guido@python.org>
parents: 21186
diff changeset
879 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
880 if (tsize < 0)
93eb4b61f687 SF patch 514641 (Naofumi Honda) - Negative ob_size of LongObjects
Guido van Rossum <guido@python.org>
parents: 21186
diff changeset
881 tsize = -tsize;
93eb4b61f687 SF patch 514641 (Naofumi Honda) - Negative ob_size of LongObjects
Guido van Rossum <guido@python.org>
parents: 21186
diff changeset
882 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
883
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
884 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
885 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
886 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
887 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
888 return (PyObject **) ((char *)obj + dictoffset);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
889 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
890
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
891 PyObject *
28320
fd5ef7003469 Renamed PyObject_GenericGetIter to PyObject_SelfIter
Raymond Hettinger <python@rcn.com>
parents: 28308
diff changeset
892 PyObject_SelfIter(PyObject *obj)
28308
9c3572a5575f Created PyObject_GenericGetIter().
Raymond Hettinger <python@rcn.com>
parents: 27966
diff changeset
893 {
9c3572a5575f Created PyObject_GenericGetIter().
Raymond Hettinger <python@rcn.com>
parents: 27966
diff changeset
894 Py_INCREF(obj);
9c3572a5575f Created PyObject_GenericGetIter().
Raymond Hettinger <python@rcn.com>
parents: 27966
diff changeset
895 return obj;
9c3572a5575f Created PyObject_GenericGetIter().
Raymond Hettinger <python@rcn.com>
parents: 27966
diff changeset
896 }
9c3572a5575f Created PyObject_GenericGetIter().
Raymond Hettinger <python@rcn.com>
parents: 27966
diff changeset
897
50755
c99ce0164699 Merged revisions 68560 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 50735
diff changeset
898 /* Helper used when the __next__ method is removed from a type:
c99ce0164699 Merged revisions 68560 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 50735
diff changeset
899 tp_iternext is never NULL and can be safely called without checking
c99ce0164699 Merged revisions 68560 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 50735
diff changeset
900 on every iteration.
c99ce0164699 Merged revisions 68560 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 50735
diff changeset
901 */
c99ce0164699 Merged revisions 68560 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 50735
diff changeset
902
c99ce0164699 Merged revisions 68560 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 50735
diff changeset
903 PyObject *
c99ce0164699 Merged revisions 68560 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 50735
diff changeset
904 _PyObject_NextNotImplemented(PyObject *self)
c99ce0164699 Merged revisions 68560 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 50735
diff changeset
905 {
c99ce0164699 Merged revisions 68560 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 50735
diff changeset
906 PyErr_Format(PyExc_TypeError,
c99ce0164699 Merged revisions 68560 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 50735
diff changeset
907 "'%.200s' object is not iterable",
c99ce0164699 Merged revisions 68560 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 50735
diff changeset
908 Py_TYPE(self)->tp_name);
c99ce0164699 Merged revisions 68560 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 50735
diff changeset
909 return NULL;
c99ce0164699 Merged revisions 68560 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 50735
diff changeset
910 }
c99ce0164699 Merged revisions 68560 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 50735
diff changeset
911
33489
4cb1016525b7 Move a comment back to its rightful location.
Michael W. Hudson <mwh@python.net>
parents: 32981
diff changeset
912 /* 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
913
28308
9c3572a5575f Created PyObject_GenericGetIter().
Raymond Hettinger <python@rcn.com>
parents: 27966
diff changeset
914 PyObject *
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
915 PyObject_GenericGetAttr(PyObject *obj, PyObject *name)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
916 {
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44064
diff changeset
917 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
918 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
919 PyObject *res = NULL;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
920 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
921 Py_ssize_t dictoffset;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
922 PyObject **dictptr;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
923
42013
cd125fe83051 Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents: 41769
diff changeset
924 if (!PyUnicode_Check(name)){
cd125fe83051 Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents: 41769
diff changeset
925 PyErr_Format(PyExc_TypeError,
cd125fe83051 Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents: 41769
diff changeset
926 "attribute name must be string, not '%.200s'",
cd125fe83051 Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents: 41769
diff changeset
927 name->ob_type->tp_name);
cd125fe83051 Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents: 41769
diff changeset
928 return 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
929 }
0189186f4ed9 PyObject_Generic{Get,Set}Attr(): ensure that the attribute name is a
Guido van Rossum <guido@python.org>
parents: 20865
diff changeset
930 else
0189186f4ed9 PyObject_Generic{Get,Set}Attr(): ensure that the attribute name is a
Guido van Rossum <guido@python.org>
parents: 20865
diff changeset
931 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
932
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
933 if (tp->tp_dict == NULL) {
19040
4df72ea78ae8 - Rename PyType_InitDict() to PyType_Ready().
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
934 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
935 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
936 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
937
44654
5a71dcc71eff Merged revisions 59921-59932 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
938 #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
939 /* Inline _PyType_Lookup */
6310a4a21eb8 Another modest speedup in PyObject_GenericGetAttr(): inline the call
Guido van Rossum <guido@python.org>
parents: 24894
diff changeset
940 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
941 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
942 PyObject *mro, *base, *dict;
6310a4a21eb8 Another modest speedup in PyObject_GenericGetAttr(): inline the call
Guido van Rossum <guido@python.org>
parents: 24894
diff changeset
943
6310a4a21eb8 Another modest speedup in PyObject_GenericGetAttr(): inline the call
Guido van Rossum <guido@python.org>
parents: 24894
diff changeset
944 /* 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
945 mro = tp->tp_mro;
6310a4a21eb8 Another modest speedup in PyObject_GenericGetAttr(): inline the call
Guido van Rossum <guido@python.org>
parents: 24894
diff changeset
946 assert(mro != NULL);
6310a4a21eb8 Another modest speedup in PyObject_GenericGetAttr(): inline the call
Guido van Rossum <guido@python.org>
parents: 24894
diff changeset
947 assert(PyTuple_Check(mro));
6310a4a21eb8 Another modest speedup in PyObject_GenericGetAttr(): inline the call
Guido van Rossum <guido@python.org>
parents: 24894
diff changeset
948 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
949 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
950 base = PyTuple_GET_ITEM(mro, i);
39536
8f5b9982beba Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
951 assert(PyType_Check(base));
8f5b9982beba Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
952 dict = ((PyTypeObject *)base)->tp_dict;
24897
6310a4a21eb8 Another modest speedup in PyObject_GenericGetAttr(): inline the call
Guido van Rossum <guido@python.org>
parents: 24894
diff changeset
953 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
954 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
955 if (descr != NULL)
6310a4a21eb8 Another modest speedup in PyObject_GenericGetAttr(): inline the call
Guido van Rossum <guido@python.org>
parents: 24894
diff changeset
956 break;
6310a4a21eb8 Another modest speedup in PyObject_GenericGetAttr(): inline the call
Guido van Rossum <guido@python.org>
parents: 24894
diff changeset
957 }
6310a4a21eb8 Another modest speedup in PyObject_GenericGetAttr(): inline the call
Guido van Rossum <guido@python.org>
parents: 24894
diff changeset
958 }
44654
5a71dcc71eff Merged revisions 59921-59932 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
959 #else
5a71dcc71eff Merged revisions 59921-59932 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
960 descr = _PyType_Lookup(tp, name);
5a71dcc71eff Merged revisions 59921-59932 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44351
diff changeset
961 #endif
24897
6310a4a21eb8 Another modest speedup in PyObject_GenericGetAttr(): inline the call
Guido van Rossum <guido@python.org>
parents: 24894
diff changeset
962
30159
941d49a65f06 Fix for
Michael W. Hudson <mwh@python.net>
parents: 28669
diff changeset
963 Py_XINCREF(descr);
941d49a65f06 Fix for
Michael W. Hudson <mwh@python.net>
parents: 28669
diff changeset
964
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
965 f = NULL;
39222
b0cdcbcc5f45 Get rid of most of the flags (in tp_flags) that keep track of various
Guido van Rossum <guido@python.org>
parents: 38669
diff changeset
966 if (descr != NULL) {
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
967 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
968 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
969 res = f(descr, obj, (PyObject *)obj->ob_type);
30159
941d49a65f06 Fix for
Michael W. Hudson <mwh@python.net>
parents: 28669
diff changeset
970 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
971 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
972 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
973 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
974
24894
8862bc59685a Inline call to _PyObject_GetDictPtr() in PyObject_GenericGetAttr().
Guido van Rossum <guido@python.org>
parents: 24644
diff changeset
975 /* Inline _PyObject_GetDictPtr */
8862bc59685a Inline call to _PyObject_GetDictPtr() in PyObject_GenericGetAttr().
Guido van Rossum <guido@python.org>
parents: 24644
diff changeset
976 dictoffset = tp->tp_dictoffset;
8862bc59685a Inline call to _PyObject_GetDictPtr() in PyObject_GenericGetAttr().
Guido van Rossum <guido@python.org>
parents: 24644
diff changeset
977 if (dictoffset != 0) {
8862bc59685a Inline call to _PyObject_GetDictPtr() in PyObject_GenericGetAttr().
Guido van Rossum <guido@python.org>
parents: 24644
diff changeset
978 PyObject *dict;
8862bc59685a Inline call to _PyObject_GetDictPtr() in PyObject_GenericGetAttr().
Guido van Rossum <guido@python.org>
parents: 24644
diff changeset
979 if (dictoffset < 0) {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
980 Py_ssize_t tsize;
24894
8862bc59685a Inline call to _PyObject_GetDictPtr() in PyObject_GenericGetAttr().
Guido van Rossum <guido@python.org>
parents: 24644
diff changeset
981 size_t size;
8862bc59685a Inline call to _PyObject_GetDictPtr() in PyObject_GenericGetAttr().
Guido van Rossum <guido@python.org>
parents: 24644
diff changeset
982
8862bc59685a Inline call to _PyObject_GetDictPtr() in PyObject_GenericGetAttr().
Guido van Rossum <guido@python.org>
parents: 24644
diff changeset
983 tsize = ((PyVarObject *)obj)->ob_size;
8862bc59685a Inline call to _PyObject_GetDictPtr() in PyObject_GenericGetAttr().
Guido van Rossum <guido@python.org>
parents: 24644
diff changeset
984 if (tsize < 0)
8862bc59685a Inline call to _PyObject_GetDictPtr() in PyObject_GenericGetAttr().
Guido van Rossum <guido@python.org>
parents: 24644
diff changeset
985 tsize = -tsize;
8862bc59685a Inline call to _PyObject_GetDictPtr() in PyObject_GenericGetAttr().
Guido van Rossum <guido@python.org>
parents: 24644
diff changeset
986 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
987
8862bc59685a Inline call to _PyObject_GetDictPtr() in PyObject_GenericGetAttr().
Guido van Rossum <guido@python.org>
parents: 24644
diff changeset
988 dictoffset += (long)size;
8862bc59685a Inline call to _PyObject_GetDictPtr() in PyObject_GenericGetAttr().
Guido van Rossum <guido@python.org>
parents: 24644
diff changeset
989 assert(dictoffset > 0);
8862bc59685a Inline call to _PyObject_GetDictPtr() in PyObject_GenericGetAttr().
Guido van Rossum <guido@python.org>
parents: 24644
diff changeset
990 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
991 }
8862bc59685a Inline call to _PyObject_GetDictPtr() in PyObject_GenericGetAttr().
Guido van Rossum <guido@python.org>
parents: 24644
diff changeset
992 dictptr = (PyObject **) ((char *)obj + dictoffset);
8862bc59685a Inline call to _PyObject_GetDictPtr() in PyObject_GenericGetAttr().
Guido van Rossum <guido@python.org>
parents: 24644
diff changeset
993 dict = *dictptr;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
994 if (dict != NULL) {
44943
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44654
diff changeset
995 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
996 res = PyDict_GetItem(dict, name);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
997 if (res != NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
998 Py_INCREF(res);
30159
941d49a65f06 Fix for
Michael W. Hudson <mwh@python.net>
parents: 28669
diff changeset
999 Py_XDECREF(descr);
44943
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44654
diff changeset
1000 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
1001 goto done;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1002 }
44943
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44654
diff changeset
1003 Py_DECREF(dict);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1004 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1005 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1006
21186
0189186f4ed9 PyObject_Generic{Get,Set}Attr(): ensure that the attribute name is a
Guido van Rossum <guido@python.org>
parents: 20865
diff changeset
1007 if (f != NULL) {
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44064
diff changeset
1008 res = f(descr, obj, (PyObject *)Py_TYPE(obj));
30159
941d49a65f06 Fix for
Michael W. Hudson <mwh@python.net>
parents: 28669
diff changeset
1009 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
1010 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
1011 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1012
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1013 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
1014 res = descr;
30159
941d49a65f06 Fix for
Michael W. Hudson <mwh@python.net>
parents: 28669
diff changeset
1015 /* 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
1016 goto done;
14660
983502b2cd43 PyObject_SetAttr() and PyObject_GetAttr() now also accept Unicode
Marc-André Lemburg <mal@egenix.com>
parents: 14375
diff changeset
1017 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1018
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1019 PyErr_Format(PyExc_AttributeError,
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1020 "'%.50s' object has no attribute '%.400s'",
48660
85b67fb5672b Rename PyUnicode_AsString -> _PyUnicode_AsString and
Marc-André Lemburg <mal@egenix.com>
parents: 48475
diff changeset
1021 tp->tp_name, _PyUnicode_AsString(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
1022 done:
0189186f4ed9 PyObject_Generic{Get,Set}Attr(): ensure that the attribute name is a
Guido van Rossum <guido@python.org>
parents: 20865
diff changeset
1023 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
1024 return res;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1025 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1026
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1027 int
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1028 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
1029 {
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44064
diff changeset
1030 PyTypeObject *tp = Py_TYPE(obj);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1031 PyObject *descr;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1032 descrsetfunc f;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1033 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
1034 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
1035
42013
cd125fe83051 Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents: 41769
diff changeset
1036 if (!PyUnicode_Check(name)){
cd125fe83051 Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents: 41769
diff changeset
1037 PyErr_Format(PyExc_TypeError,
cd125fe83051 Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents: 41769
diff changeset
1038 "attribute name must be string, not '%.200s'",
cd125fe83051 Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents: 41769
diff changeset
1039 name->ob_type->tp_name);
cd125fe83051 Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents: 41769
diff changeset
1040 return -1;
21186
0189186f4ed9 PyObject_Generic{Get,Set}Attr(): ensure that the attribute name is a
Guido van Rossum <guido@python.org>
parents: 20865
diff changeset
1041 }
0189186f4ed9 PyObject_Generic{Get,Set}Attr(): ensure that the attribute name is a
Guido van Rossum <guido@python.org>
parents: 20865
diff changeset
1042 else
0189186f4ed9 PyObject_Generic{Get,Set}Attr(): ensure that the attribute name is a
Guido van Rossum <guido@python.org>
parents: 20865
diff changeset
1043 Py_INCREF(name);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1044
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1045 if (tp->tp_dict == NULL) {
19040
4df72ea78ae8 - Rename PyType_InitDict() to PyType_Ready().
Guido van Rossum <guido@python.org>
parents: 18933
diff changeset
1046 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
1047 goto done;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1048 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1049
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1050 descr = _PyType_Lookup(tp, name);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1051 f = NULL;
39222
b0cdcbcc5f45 Get rid of most of the flags (in tp_flags) that keep track of various
Guido van Rossum <guido@python.org>
parents: 38669
diff changeset
1052 if (descr != NULL) {
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1053 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
1054 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
1055 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
1056 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
1057 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1058 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1059
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1060 dictptr = _PyObject_GetDictPtr(obj);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1061 if (dictptr != NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1062 PyObject *dict = *dictptr;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1063 if (dict == NULL && value != NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1064 dict = PyDict_New();
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1065 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
1066 goto done;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1067 *dictptr = dict;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1068 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1069 if (dict != NULL) {
44943
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44654
diff changeset
1070 Py_INCREF(dict);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1071 if (value == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1072 res = PyDict_DelItem(dict, name);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1073 else
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1074 res = PyDict_SetItem(dict, name, value);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1075 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
1076 PyErr_SetObject(PyExc_AttributeError, name);
44943
6547be11f443 Merged revisions 60245-60277 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44654
diff changeset
1077 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
1078 goto done;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1079 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1080 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1081
21186
0189186f4ed9 PyObject_Generic{Get,Set}Attr(): ensure that the attribute name is a
Guido van Rossum <guido@python.org>
parents: 20865
diff changeset
1082 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
1083 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
1084 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
1085 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1086
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1087 if (descr == NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1088 PyErr_Format(PyExc_AttributeError,
42039
f83cc9b31a97 Simplify error formatting.
Walter Dörwald <walter@livinglogic.de>
parents: 42013
diff changeset
1089 "'%.100s' object has no attribute '%U'",
f83cc9b31a97 Simplify error formatting.
Walter Dörwald <walter@livinglogic.de>
parents: 42013
diff changeset
1090 tp->tp_name, 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
1091 goto done;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1092 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1093
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18887
diff changeset
1094 PyErr_Format(PyExc_AttributeError,
42039
f83cc9b31a97 Simplify error formatting.
Walter Dörwald <walter@livinglogic.de>
parents: 42013
diff changeset
1095 "'%.50s' object attribute '%U' is read-only",
f83cc9b31a97 Simplify error formatting.
Walter Dörwald <walter@livinglogic.de>
parents: 42013
diff changeset
1096 tp->tp_name, 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
1097 done:
0189186f4ed9 PyObject_Generic{Get,Set}Attr(): ensure that the attribute name is a
Guido van Rossum <guido@python.org>
parents: 20865
diff changeset
1098 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
1099 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
1100 }
8ec298b255a2 Moved PyObject_{Get,Set}Attr here (from dictobject) and add PyObject_HasAttr.
Guido van Rossum <guido@python.org>
parents: 5535
diff changeset
1101
1282
e0464e7115ca * pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1102 /* 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
1103 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
1104
e0464e7115ca * pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1105 int
13473
31b1f691a7d9 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1106 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
1107 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
1108 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
1109 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
1110 return 1;
24e80d96bd0b Speedup for PyObject_IsTrue(): check for True and False first.
Guido van Rossum <guido@python.org>
parents: 25013
diff changeset
1111 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
1112 return 0;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5006
diff changeset
1113 if (v == Py_None)
23838
95263ccec819 SF # 561244 Micro optimizations
Neal Norwitz <nnorwitz@gmail.com>
parents: 23599
diff changeset
1114 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
1115 else if (v->ob_type->tp_as_number != NULL &&
40401
88133417dfd0 - patch #1600346 submitted by Tomer Filiba
Jack Diederich <jackdied@gmail.com>
parents: 39703
diff changeset
1116 v->ob_type->tp_as_number->nb_bool != NULL)
88133417dfd0 - patch #1600346 submitted by Tomer Filiba
Jack Diederich <jackdied@gmail.com>
parents: 39703
diff changeset
1117 res = (*v->ob_type->tp_as_number->nb_bool)(v);
8404
64b88a178328 In PyObject_IsTrue(), don't call function pointers that are NULL
Guido van Rossum <guido@python.org>
parents: 8181
diff changeset
1118 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
1119 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
1120 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
1121 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
1122 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
1123 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
1124 else
23838
95263ccec819 SF # 561244 Micro optimizations
Neal Norwitz <nnorwitz@gmail.com>
parents: 23599
diff changeset
1125 return 1;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
1126 /* 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
1127 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
1128 }
e0464e7115ca * pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1129
24122
d332484e394a Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents: 24121
diff changeset
1130 /* equivalent of 'not v'
7935
aa7d69d67c81 Add PyObject_Not().
Guido van Rossum <guido@python.org>
parents: 7119
diff changeset
1131 Return -1 if an error occurred */
aa7d69d67c81 Add PyObject_Not().
Guido van Rossum <guido@python.org>
parents: 7119
diff changeset
1132
aa7d69d67c81 Add PyObject_Not().
Guido van Rossum <guido@python.org>
parents: 7119
diff changeset
1133 int
13473
31b1f691a7d9 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1134 PyObject_Not(PyObject *v)
7935
aa7d69d67c81 Add PyObject_Not().
Guido van Rossum <guido@python.org>
parents: 7119
diff changeset
1135 {
aa7d69d67c81 Add PyObject_Not().
Guido van Rossum <guido@python.org>
parents: 7119
diff changeset
1136 int res;
aa7d69d67c81 Add PyObject_Not().
Guido van Rossum <guido@python.org>
parents: 7119
diff changeset
1137 res = PyObject_IsTrue(v);
aa7d69d67c81 Add PyObject_Not().
Guido van Rossum <guido@python.org>
parents: 7119
diff changeset
1138 if (res < 0)
aa7d69d67c81 Add PyObject_Not().
Guido van Rossum <guido@python.org>
parents: 7119
diff changeset
1139 return res;
aa7d69d67c81 Add PyObject_Not().
Guido van Rossum <guido@python.org>
parents: 7119
diff changeset
1140 return res == 0;
aa7d69d67c81 Add PyObject_Not().
Guido van Rossum <guido@python.org>
parents: 7119
diff changeset
1141 }
aa7d69d67c81 Add PyObject_Not().
Guido van Rossum <guido@python.org>
parents: 7119
diff changeset
1142
2237
0b0b18d655d2 move callable() here
Guido van Rossum <guido@python.org>
parents: 2160
diff changeset
1143 /* Test whether an object can be called */
0b0b18d655d2 move callable() here
Guido van Rossum <guido@python.org>
parents: 2160
diff changeset
1144
0b0b18d655d2 move callable() here
Guido van Rossum <guido@python.org>
parents: 2160
diff changeset
1145 int
13473
31b1f691a7d9 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1146 PyCallable_Check(PyObject *x)
2237
0b0b18d655d2 move callable() here
Guido van Rossum <guido@python.org>
parents: 2160
diff changeset
1147 {
0b0b18d655d2 move callable() here
Guido van Rossum <guido@python.org>
parents: 2160
diff changeset
1148 if (x == NULL)
0b0b18d655d2 move callable() here
Guido van Rossum <guido@python.org>
parents: 2160
diff changeset
1149 return 0;
39536
8f5b9982beba Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
1150 return x->ob_type->tp_call != NULL;
2237
0b0b18d655d2 move callable() here
Guido van Rossum <guido@python.org>
parents: 2160
diff changeset
1151 }
0b0b18d655d2 move callable() here
Guido van Rossum <guido@python.org>
parents: 2160
diff changeset
1152
41107
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1153 /* ------------------------- PyObject_Dir() helpers ------------------------- */
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1154
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
1155 /* 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
1156 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
1157 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
1158 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
1159 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
1160 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
1161 */
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1162
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1163 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
1164 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
1165 {
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1166 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
1167 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
1168
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1169 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
1170 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
1171
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1172 /* 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
1173 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
1174 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
1175 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
1176 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
1177 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
1178 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
1179 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
1180 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
1181 }
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1182
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1183 /* 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
1184 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
1185 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
1186 PyErr_Clear();
c327a8ac9f2b merge_class_dict(): Clear the error if __bases__ doesn't exist.
Tim Peters <tim.peters@gmail.com>
parents: 19906
diff changeset
1187 else {
23436
f9d5e1fb2aa6 Jim Fulton reported a segfault in dir(). A heavily proxied object
Guido van Rossum <guido@python.org>
parents: 23282
diff changeset
1188 /* 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
1189 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
1190 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
1191 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
1192 PyErr_Clear();
f9d5e1fb2aa6 Jim Fulton reported a segfault in dir(). A heavily proxied object
Guido van Rossum <guido@python.org>
parents: 23282
diff changeset
1193 else {
f9d5e1fb2aa6 Jim Fulton reported a segfault in dir(). A heavily proxied object
Guido van Rossum <guido@python.org>
parents: 23282
diff changeset
1194 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
1195 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
1196 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
1197 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
1198 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
1199 return -1;
f9d5e1fb2aa6 Jim Fulton reported a segfault in dir(). A heavily proxied object
Guido van Rossum <guido@python.org>
parents: 23282
diff changeset
1200 }
27702
84f2c438c3f5 SF bug 681122: Built-in function dir() causes refcount leak in baseclasses.
Tim Peters <tim.peters@gmail.com>
parents: 27314
diff changeset
1201 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
1202 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
1203 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
1204 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
1205 return -1;
f9d5e1fb2aa6 Jim Fulton reported a segfault in dir(). A heavily proxied object
Guido van Rossum <guido@python.org>
parents: 23282
diff changeset
1206 }
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
1207 }
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1208 }
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1209 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
1210 }
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1211 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
1212 }
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1213
41107
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1214 /* Helper for PyObject_Dir without arguments: returns the local scope. */
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1215 static PyObject *
41450
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41124
diff changeset
1216 _dir_locals(void)
19932
621ed13445de PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
Tim Peters <tim.peters@gmail.com>
parents: 19931
diff changeset
1217 {
41124
211201e6b045 Check the keys of the locals dict -- they need not be a list.
Georg Brandl <georg@python.org>
parents: 41107
diff changeset
1218 PyObject *names;
41107
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1219 PyObject *locals = PyEval_GetLocals();
19932
621ed13445de PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
Tim Peters <tim.peters@gmail.com>
parents: 19931
diff changeset
1220
41107
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1221 if (locals == NULL) {
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1222 PyErr_SetString(PyExc_SystemError, "frame does not exist");
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1223 return NULL;
19932
621ed13445de PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
Tim Peters <tim.peters@gmail.com>
parents: 19931
diff changeset
1224 }
621ed13445de PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
Tim Peters <tim.peters@gmail.com>
parents: 19931
diff changeset
1225
41124
211201e6b045 Check the keys of the locals dict -- they need not be a list.
Georg Brandl <georg@python.org>
parents: 41107
diff changeset
1226 names = PyMapping_Keys(locals);
211201e6b045 Check the keys of the locals dict -- they need not be a list.
Georg Brandl <georg@python.org>
parents: 41107
diff changeset
1227 if (!names)
211201e6b045 Check the keys of the locals dict -- they need not be a list.
Georg Brandl <georg@python.org>
parents: 41107
diff changeset
1228 return NULL;
211201e6b045 Check the keys of the locals dict -- they need not be a list.
Georg Brandl <georg@python.org>
parents: 41107
diff changeset
1229 if (!PyList_Check(names)) {
211201e6b045 Check the keys of the locals dict -- they need not be a list.
Georg Brandl <georg@python.org>
parents: 41107
diff changeset
1230 PyErr_Format(PyExc_TypeError,
211201e6b045 Check the keys of the locals dict -- they need not be a list.
Georg Brandl <georg@python.org>
parents: 41107
diff changeset
1231 "dir(): expected keys() of locals to be a list, "
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44064
diff changeset
1232 "not '%.200s'", Py_TYPE(names)->tp_name);
41124
211201e6b045 Check the keys of the locals dict -- they need not be a list.
Georg Brandl <georg@python.org>
parents: 41107
diff changeset
1233 Py_DECREF(names);
211201e6b045 Check the keys of the locals dict -- they need not be a list.
Georg Brandl <georg@python.org>
parents: 41107
diff changeset
1234 return NULL;
211201e6b045 Check the keys of the locals dict -- they need not be a list.
Georg Brandl <georg@python.org>
parents: 41107
diff changeset
1235 }
41107
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1236 /* the locals don't need to be DECREF'd */
41124
211201e6b045 Check the keys of the locals dict -- they need not be a list.
Georg Brandl <georg@python.org>
parents: 41107
diff changeset
1237 return names;
41107
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1238 }
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1239
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1240 /* Helper for PyObject_Dir of type objects: returns __dict__ and __bases__.
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43734
diff changeset
1241 We deliberately don't suck up its __class__, as methods belonging to the
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43734
diff changeset
1242 metaclass would probably be more confusing than helpful.
41107
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1243 */
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43734
diff changeset
1244 static PyObject *
41107
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1245 _specialized_dir_type(PyObject *obj)
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1246 {
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1247 PyObject *result = NULL;
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1248 PyObject *dict = PyDict_New();
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1249
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1250 if (dict != NULL && merge_class_dict(dict, obj) == 0)
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1251 result = PyDict_Keys(dict);
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1252
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1253 Py_XDECREF(dict);
19932
621ed13445de PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
Tim Peters <tim.peters@gmail.com>
parents: 19931
diff changeset
1254 return result;
621ed13445de PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
Tim Peters <tim.peters@gmail.com>
parents: 19931
diff changeset
1255 }
621ed13445de PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
Tim Peters <tim.peters@gmail.com>
parents: 19931
diff changeset
1256
41107
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1257 /* Helper for PyObject_Dir of module objects: returns the module's __dict__. */
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1258 static PyObject *
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1259 _specialized_dir_module(PyObject *obj)
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
1260 {
41107
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1261 PyObject *result = NULL;
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1262 PyObject *dict = PyObject_GetAttrString(obj, "__dict__");
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
1263
41107
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1264 if (dict != NULL) {
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1265 if (PyDict_Check(dict))
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1266 result = PyDict_Keys(dict);
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1267 else {
55647
4c51c4bffb21 Merged revisions 74459 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 54326
diff changeset
1268 const char *name = PyModule_GetName(obj);
4c51c4bffb21 Merged revisions 74459 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 54326
diff changeset
1269 if (name)
4c51c4bffb21 Merged revisions 74459 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 54326
diff changeset
1270 PyErr_Format(PyExc_TypeError,
4c51c4bffb21 Merged revisions 74459 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 54326
diff changeset
1271 "%.200s.__dict__ is not a dictionary",
4c51c4bffb21 Merged revisions 74459 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 54326
diff changeset
1272 name);
19833
f44606b242c5 PyObject_Dir():
Guido van Rossum <guido@python.org>
parents: 19705
diff changeset
1273 }
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
1274 }
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1275
41107
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1276 Py_XDECREF(dict);
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1277 return result;
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1278 }
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1279
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1280 /* Helper for PyObject_Dir of generic objects: returns __dict__, __class__,
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1281 and recursively up the __class__.__bases__ chain.
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1282 */
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1283 static PyObject *
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1284 _generic_dir(PyObject *obj)
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1285 {
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1286 PyObject *result = NULL;
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1287 PyObject *dict = NULL;
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1288 PyObject *itsclass = NULL;
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43734
diff changeset
1289
41107
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1290 /* Get __dict__ (which may or may not be a real dict...) */
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1291 dict = PyObject_GetAttrString(obj, "__dict__");
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1292 if (dict == NULL) {
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1293 PyErr_Clear();
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1294 dict = PyDict_New();
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1295 }
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1296 else if (!PyDict_Check(dict)) {
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1297 Py_DECREF(dict);
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1298 dict = PyDict_New();
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1299 }
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1300 else {
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1301 /* Copy __dict__ to avoid mutating it. */
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1302 PyObject *temp = PyDict_Copy(dict);
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1303 Py_DECREF(dict);
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1304 dict = temp;
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1305 }
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1306
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1307 if (dict == NULL)
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1308 goto error;
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1309
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1310 /* Merge in attrs reachable from its class. */
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1311 itsclass = PyObject_GetAttrString(obj, "__class__");
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1312 if (itsclass == NULL)
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1313 /* XXX(tomer): Perhaps fall back to obj->ob_type if no
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1314 __class__ exists? */
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1315 PyErr_Clear();
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1316 else {
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1317 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
1318 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
1319 }
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1320
41107
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1321 result = PyDict_Keys(dict);
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1322 /* fall through */
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1323 error:
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1324 Py_XDECREF(itsclass);
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1325 Py_XDECREF(dict);
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1326 return result;
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1327 }
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1328
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1329 /* Helper for PyObject_Dir: object introspection.
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1330 This calls one of the above specialized versions if no __dir__ method
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1331 exists. */
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1332 static PyObject *
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1333 _dir_object(PyObject *obj)
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1334 {
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1335 PyObject * result = NULL;
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1336 PyObject * dirfunc = PyObject_GetAttrString((PyObject*)obj->ob_type,
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1337 "__dir__");
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1338
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1339 assert(obj);
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1340 if (dirfunc == NULL) {
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1341 /* use default implementation */
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1342 PyErr_Clear();
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1343 if (PyModule_Check(obj))
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1344 result = _specialized_dir_module(obj);
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1345 else if (PyType_Check(obj))
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1346 result = _specialized_dir_type(obj);
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1347 else
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1348 result = _generic_dir(obj);
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1349 }
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
1350 else {
41107
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1351 /* use __dir__ */
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1352 result = PyObject_CallFunctionObjArgs(dirfunc, obj, NULL);
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1353 Py_DECREF(dirfunc);
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1354 if (result == NULL)
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1355 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
1356
41107
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1357 /* result must be a list */
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1358 /* XXX(gbrandl): could also check if all items are strings */
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1359 if (!PyList_Check(result)) {
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1360 PyErr_Format(PyExc_TypeError,
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1361 "__dir__() must return a list, not %.200s",
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44064
diff changeset
1362 Py_TYPE(result)->tp_name);
41107
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1363 Py_DECREF(result);
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1364 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
1365 }
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1366 }
b7f6fbc087a7 At Guido's suggestion, here's a new C API function, PyObject_Dir(), like
Tim Peters <tim.peters@gmail.com>
parents: 19633
diff changeset
1367
41107
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1368 return result;
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1369 }
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1370
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1371 /* Implementation of dir() -- if obj is NULL, returns the names in the current
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1372 (local) scope. Otherwise, performs introspection of the object: returns a
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1373 sorted list of attribute names (supposedly) accessible from the object
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1374 */
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1375 PyObject *
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1376 PyObject_Dir(PyObject *obj)
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1377 {
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1378 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
1379
41107
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1380 if (obj == NULL)
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1381 /* no object -- introspect the locals */
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1382 result = _dir_locals();
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1383 else
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1384 /* object -- introspect the object */
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1385 result = _dir_object(obj);
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1386
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1387 assert(result == NULL || PyList_Check(result));
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1388
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1389 if (result != NULL && PyList_Sort(result) != 0) {
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1390 /* sorting the list failed */
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1391 Py_DECREF(result);
cf452027073e Patch #1591665: implement the __dir__() special function lookup in PyObject_Dir.
Georg Brandl <georg@python.org>
parents: 40737
diff changeset
1392 result = NULL;
32981
50c122bc7aac SF bug #1004669: Type returned from .keys() is not checked
Raymond Hettinger <python@rcn.com>
parents: 32531
diff changeset
1393 }
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43734
diff changeset
1394
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
1395 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
1396 }
2237
0b0b18d655d2 move callable() here
Guido van Rossum <guido@python.org>
parents: 2160
diff changeset
1397
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1398 /*
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1399 NoObject is usable as a non-NULL undefined value, used by the macro None.
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1400 There is (and should be!) no way to create other objects of this type,
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
1401 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
1402 (XXX This type and the type of NotImplemented below should be unified.)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1403 */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1404
739
ab1b3486b226 Fix lint bug
Guido van Rossum <guido@python.org>
parents: 512
diff changeset
1405 /* ARGSUSED */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5006
diff changeset
1406 static PyObject *
13473
31b1f691a7d9 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1407 none_repr(PyObject *op)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
1408 {
41769
b29d71d43c80 Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents: 41700
diff changeset
1409 return PyUnicode_FromString("None");
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1410 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1411
16165
0a72070eab6e A few miscellaneous helpers.
Barry Warsaw <barry@python.org>
parents: 16123
diff changeset
1412 /* ARGUSED */
0a72070eab6e A few miscellaneous helpers.
Barry Warsaw <barry@python.org>
parents: 16123
diff changeset
1413 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
1414 none_dealloc(PyObject* ignore)
16165
0a72070eab6e A few miscellaneous helpers.
Barry Warsaw <barry@python.org>
parents: 16123
diff changeset
1415 {
0a72070eab6e A few miscellaneous helpers.
Barry Warsaw <barry@python.org>
parents: 16123
diff changeset
1416 /* This should never get called, but we also don't want to SEGV if
51776
d06a3ebf306f Merged revisions 69846 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 51603
diff changeset
1417 * we accidentally decref None out of existence.
16165
0a72070eab6e A few miscellaneous helpers.
Barry Warsaw <barry@python.org>
parents: 16123
diff changeset
1418 */
24644
7ba3e5fbaad8 Replace abort with Py_FatalError.
Martin v. Löwis <martin@v.loewis.de>
parents: 24420
diff changeset
1419 Py_FatalError("deallocating None");
16165
0a72070eab6e A few miscellaneous helpers.
Barry Warsaw <barry@python.org>
parents: 16123
diff changeset
1420 }
0a72070eab6e A few miscellaneous helpers.
Barry Warsaw <barry@python.org>
parents: 16123
diff changeset
1421
0a72070eab6e A few miscellaneous helpers.
Barry Warsaw <barry@python.org>
parents: 16123
diff changeset
1422
19294
1456ec735087 Add a function _Py_ReadyTypes() which initializes various and sundry
Guido van Rossum <guido@python.org>
parents: 19293
diff changeset
1423 static PyTypeObject PyNone_Type = {
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42196
diff changeset
1424 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
1425 "NoneType",
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1426 0,
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1427 0,
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36977
diff changeset
1428 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
1429 0, /*tp_print*/
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
1430 0, /*tp_getattr*/
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
1431 0, /*tp_setattr*/
51270
debd14c489b9 Issue #1717: rename tp_compare to tp_reserved. I'll change the
Mark Dickinson <dickinsm@gmail.com>
parents: 51247
diff changeset
1432 0, /*tp_reserved*/
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36977
diff changeset
1433 none_repr, /*tp_repr*/
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
1434 0, /*tp_as_number*/
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
1435 0, /*tp_as_sequence*/
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
1436 0, /*tp_as_mapping*/
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
1437 0, /*tp_hash */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1438 };
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1439
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5006
diff changeset
1440 PyObject _Py_NoneStruct = {
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42196
diff changeset
1441 _PyObject_EXTRA_INIT
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42196
diff changeset
1442 1, &PyNone_Type
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1443 };
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1444
15726
db9f1893be01 Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1445 /* 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
1446 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
1447
db9f1893be01 Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1448 static PyObject *
db9f1893be01 Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1449 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
1450 {
41769
b29d71d43c80 Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents: 41700
diff changeset
1451 return PyUnicode_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
1452 }
db9f1893be01 Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1453
db9f1893be01 Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1454 static PyTypeObject PyNotImplemented_Type = {
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42196
diff changeset
1455 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
1456 "NotImplementedType",
15726
db9f1893be01 Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1457 0,
db9f1893be01 Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1458 0,
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36977
diff changeset
1459 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
1460 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
1461 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
1462 0, /*tp_setattr*/
51270
debd14c489b9 Issue #1717: rename tp_compare to tp_reserved. I'll change the
Mark Dickinson <dickinsm@gmail.com>
parents: 51247
diff changeset
1463 0, /*tp_reserved*/
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36977
diff changeset
1464 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
1465 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
1466 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
1467 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
1468 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
1469 };
db9f1893be01 Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1470
db9f1893be01 Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1471 PyObject _Py_NotImplementedStruct = {
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42196
diff changeset
1472 _PyObject_EXTRA_INIT
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42196
diff changeset
1473 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
1474 };
db9f1893be01 Changes for PEP 208. PyObject_Compare has been rewritten. Instances no
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
1475
19294
1456ec735087 Add a function _Py_ReadyTypes() which initializes various and sundry
Guido van Rossum <guido@python.org>
parents: 19293
diff changeset
1476 void
1456ec735087 Add a function _Py_ReadyTypes() which initializes various and sundry
Guido van Rossum <guido@python.org>
parents: 19293
diff changeset
1477 _Py_ReadyTypes(void)
1456ec735087 Add a function _Py_ReadyTypes() which initializes various and sundry
Guido van Rossum <guido@python.org>
parents: 19293
diff changeset
1478 {
1456ec735087 Add a function _Py_ReadyTypes() which initializes various and sundry
Guido van Rossum <guido@python.org>
parents: 19293
diff changeset
1479 if (PyType_Ready(&PyType_Type) < 0)
53244
6ec3df375f5a Merged revisions 71722 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52870
diff changeset
1480 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
1481
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32463
diff changeset
1482 if (PyType_Ready(&_PyWeakref_RefType) < 0)
53244
6ec3df375f5a Merged revisions 71722 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52870
diff changeset
1483 Py_FatalError("Can't initialize weakref type");
32472
4383db77b19a Make weak references subclassable:
Fred Drake <fdrake@acm.org>
parents: 32463
diff changeset
1484
53274
8533cfc2420a Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 53244
diff changeset
1485 if (PyType_Ready(&_PyWeakref_CallableProxyType) < 0)
8533cfc2420a Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 53244
diff changeset
1486 Py_FatalError("Can't initialize callable weakref proxy type");
8533cfc2420a Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 53244
diff changeset
1487
8533cfc2420a Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 53244
diff changeset
1488 if (PyType_Ready(&_PyWeakref_ProxyType) < 0)
8533cfc2420a Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 53244
diff changeset
1489 Py_FatalError("Can't initialize weakref proxy type");
8533cfc2420a Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 53244
diff changeset
1490
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
1491 if (PyType_Ready(&PyBool_Type) < 0)
53244
6ec3df375f5a Merged revisions 71722 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52870
diff changeset
1492 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
1493
47300
96b34263e448 Renamed PyBytes to PyByteArray
Christian Heimes <christian@cheimes.de>
parents: 44943
diff changeset
1494 if (PyType_Ready(&PyByteArray_Type) < 0)
53274
8533cfc2420a Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 53244
diff changeset
1495 Py_FatalError("Can't initialize bytearray type");
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents: 37879
diff changeset
1496
47301
23ab586c427a Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents: 47300
diff changeset
1497 if (PyType_Ready(&PyBytes_Type) < 0)
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
1498 Py_FatalError("Can't initialize 'str'");
220b98f06415 - A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents: 23436
diff changeset
1499
19294
1456ec735087 Add a function _Py_ReadyTypes() which initializes various and sundry
Guido van Rossum <guido@python.org>
parents: 19293
diff changeset
1500 if (PyType_Ready(&PyList_Type) < 0)
53274
8533cfc2420a Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 53244
diff changeset
1501 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
1502
1456ec735087 Add a function _Py_ReadyTypes() which initializes various and sundry
Guido van Rossum <guido@python.org>
parents: 19293
diff changeset
1503 if (PyType_Ready(&PyNone_Type) < 0)
53244
6ec3df375f5a Merged revisions 71722 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52870
diff changeset
1504 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
1505
39536
8f5b9982beba Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
1506 if (PyType_Ready(Py_Ellipsis->ob_type) < 0)
8f5b9982beba Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
1507 Py_FatalError("Can't initialize type(Ellipsis)");
8f5b9982beba Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
1508
19294
1456ec735087 Add a function _Py_ReadyTypes() which initializes various and sundry
Guido van Rossum <guido@python.org>
parents: 19293
diff changeset
1509 if (PyType_Ready(&PyNotImplemented_Type) < 0)
53244
6ec3df375f5a Merged revisions 71722 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52870
diff changeset
1510 Py_FatalError("Can't initialize NotImplemented type");
6ec3df375f5a Merged revisions 71722 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52870
diff changeset
1511
6ec3df375f5a Merged revisions 71722 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52870
diff changeset
1512 if (PyType_Ready(&PyTraceBack_Type) < 0)
6ec3df375f5a Merged revisions 71722 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52870
diff changeset
1513 Py_FatalError("Can't initialize traceback type");
6ec3df375f5a Merged revisions 71722 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52870
diff changeset
1514
6ec3df375f5a Merged revisions 71722 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52870
diff changeset
1515 if (PyType_Ready(&PySuper_Type) < 0)
6ec3df375f5a Merged revisions 71722 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52870
diff changeset
1516 Py_FatalError("Can't initialize super type");
6ec3df375f5a Merged revisions 71722 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52870
diff changeset
1517
6ec3df375f5a Merged revisions 71722 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52870
diff changeset
1518 if (PyType_Ready(&PyBaseObject_Type) < 0)
6ec3df375f5a Merged revisions 71722 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52870
diff changeset
1519 Py_FatalError("Can't initialize object type");
6ec3df375f5a Merged revisions 71722 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52870
diff changeset
1520
6ec3df375f5a Merged revisions 71722 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52870
diff changeset
1521 if (PyType_Ready(&PyRange_Type) < 0)
6ec3df375f5a Merged revisions 71722 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52870
diff changeset
1522 Py_FatalError("Can't initialize range type");
6ec3df375f5a Merged revisions 71722 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52870
diff changeset
1523
6ec3df375f5a Merged revisions 71722 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52870
diff changeset
1524 if (PyType_Ready(&PyDict_Type) < 0)
6ec3df375f5a Merged revisions 71722 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52870
diff changeset
1525 Py_FatalError("Can't initialize dict type");
6ec3df375f5a Merged revisions 71722 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52870
diff changeset
1526
6ec3df375f5a Merged revisions 71722 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52870
diff changeset
1527 if (PyType_Ready(&PySet_Type) < 0)
6ec3df375f5a Merged revisions 71722 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52870
diff changeset
1528 Py_FatalError("Can't initialize set type");
6ec3df375f5a Merged revisions 71722 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52870
diff changeset
1529
6ec3df375f5a Merged revisions 71722 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52870
diff changeset
1530 if (PyType_Ready(&PyUnicode_Type) < 0)
6ec3df375f5a Merged revisions 71722 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52870
diff changeset
1531 Py_FatalError("Can't initialize str type");
6ec3df375f5a Merged revisions 71722 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52870
diff changeset
1532
6ec3df375f5a Merged revisions 71722 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52870
diff changeset
1533 if (PyType_Ready(&PySlice_Type) < 0)
6ec3df375f5a Merged revisions 71722 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52870
diff changeset
1534 Py_FatalError("Can't initialize slice type");
6ec3df375f5a Merged revisions 71722 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52870
diff changeset
1535
6ec3df375f5a Merged revisions 71722 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52870
diff changeset
1536 if (PyType_Ready(&PyStaticMethod_Type) < 0)
6ec3df375f5a Merged revisions 71722 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52870
diff changeset
1537 Py_FatalError("Can't initialize static method type");
6ec3df375f5a Merged revisions 71722 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52870
diff changeset
1538
53274
8533cfc2420a Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 53244
diff changeset
1539 #ifndef WITHOUT_COMPLEX
53244
6ec3df375f5a Merged revisions 71722 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52870
diff changeset
1540 if (PyType_Ready(&PyComplex_Type) < 0)
6ec3df375f5a Merged revisions 71722 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52870
diff changeset
1541 Py_FatalError("Can't initialize complex type");
53274
8533cfc2420a Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 53244
diff changeset
1542 #endif
53244
6ec3df375f5a Merged revisions 71722 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52870
diff changeset
1543 if (PyType_Ready(&PyFloat_Type) < 0)
6ec3df375f5a Merged revisions 71722 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52870
diff changeset
1544 Py_FatalError("Can't initialize float type");
6ec3df375f5a Merged revisions 71722 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52870
diff changeset
1545
6ec3df375f5a Merged revisions 71722 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52870
diff changeset
1546 if (PyType_Ready(&PyLong_Type) < 0)
6ec3df375f5a Merged revisions 71722 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52870
diff changeset
1547 Py_FatalError("Can't initialize int type");
6ec3df375f5a Merged revisions 71722 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52870
diff changeset
1548
6ec3df375f5a Merged revisions 71722 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52870
diff changeset
1549 if (PyType_Ready(&PyFrozenSet_Type) < 0)
6ec3df375f5a Merged revisions 71722 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52870
diff changeset
1550 Py_FatalError("Can't initialize frozenset type");
6ec3df375f5a Merged revisions 71722 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52870
diff changeset
1551
6ec3df375f5a Merged revisions 71722 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52870
diff changeset
1552 if (PyType_Ready(&PyProperty_Type) < 0)
6ec3df375f5a Merged revisions 71722 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52870
diff changeset
1553 Py_FatalError("Can't initialize property type");
6ec3df375f5a Merged revisions 71722 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52870
diff changeset
1554
6ec3df375f5a Merged revisions 71722 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52870
diff changeset
1555 if (PyType_Ready(&PyMemoryView_Type) < 0)
6ec3df375f5a Merged revisions 71722 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52870
diff changeset
1556 Py_FatalError("Can't initialize memoryview type");
6ec3df375f5a Merged revisions 71722 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52870
diff changeset
1557
6ec3df375f5a Merged revisions 71722 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52870
diff changeset
1558 if (PyType_Ready(&PyTuple_Type) < 0)
6ec3df375f5a Merged revisions 71722 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52870
diff changeset
1559 Py_FatalError("Can't initialize tuple type");
6ec3df375f5a Merged revisions 71722 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52870
diff changeset
1560
6ec3df375f5a Merged revisions 71722 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52870
diff changeset
1561 if (PyType_Ready(&PyEnum_Type) < 0)
6ec3df375f5a Merged revisions 71722 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52870
diff changeset
1562 Py_FatalError("Can't initialize enumerate type");
6ec3df375f5a Merged revisions 71722 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52870
diff changeset
1563
6ec3df375f5a Merged revisions 71722 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52870
diff changeset
1564 if (PyType_Ready(&PyReversed_Type) < 0)
6ec3df375f5a Merged revisions 71722 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 52870
diff changeset
1565 Py_FatalError("Can't initialize reversed type");
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39623
diff changeset
1566
43718
e510b716bd80 Patch 1352 (continued in issue 1329) by Christian Heimes.
Guido van Rossum <guido@python.org>
parents: 43400
diff changeset
1567 if (PyType_Ready(&PyStdPrinter_Type) < 0)
e510b716bd80 Patch 1352 (continued in issue 1329) by Christian Heimes.
Guido van Rossum <guido@python.org>
parents: 43400
diff changeset
1568 Py_FatalError("Can't initialize StdPrinter");
53274
8533cfc2420a Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 53244
diff changeset
1569
8533cfc2420a Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 53244
diff changeset
1570 if (PyType_Ready(&PyCode_Type) < 0)
8533cfc2420a Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 53244
diff changeset
1571 Py_FatalError("Can't initialize code type");
8533cfc2420a Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 53244
diff changeset
1572
8533cfc2420a Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 53244
diff changeset
1573 if (PyType_Ready(&PyFrame_Type) < 0)
8533cfc2420a Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 53244
diff changeset
1574 Py_FatalError("Can't initialize frame type");
8533cfc2420a Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 53244
diff changeset
1575
8533cfc2420a Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 53244
diff changeset
1576 if (PyType_Ready(&PyCFunction_Type) < 0)
8533cfc2420a Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 53244
diff changeset
1577 Py_FatalError("Can't initialize builtin function type");
8533cfc2420a Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 53244
diff changeset
1578
8533cfc2420a Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 53244
diff changeset
1579 if (PyType_Ready(&PyMethod_Type) < 0)
8533cfc2420a Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 53244
diff changeset
1580 Py_FatalError("Can't initialize method type");
8533cfc2420a Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 53244
diff changeset
1581
8533cfc2420a Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 53244
diff changeset
1582 if (PyType_Ready(&PyFunction_Type) < 0)
8533cfc2420a Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 53244
diff changeset
1583 Py_FatalError("Can't initialize function type");
8533cfc2420a Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 53244
diff changeset
1584
8533cfc2420a Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 53244
diff changeset
1585 if (PyType_Ready(&PyDictProxy_Type) < 0)
8533cfc2420a Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 53244
diff changeset
1586 Py_FatalError("Can't initialize dict proxy type");
8533cfc2420a Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 53244
diff changeset
1587
8533cfc2420a Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 53244
diff changeset
1588 if (PyType_Ready(&PyGen_Type) < 0)
8533cfc2420a Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 53244
diff changeset
1589 Py_FatalError("Can't initialize generator type");
8533cfc2420a Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 53244
diff changeset
1590
8533cfc2420a Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 53244
diff changeset
1591 if (PyType_Ready(&PyGetSetDescr_Type) < 0)
8533cfc2420a Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 53244
diff changeset
1592 Py_FatalError("Can't initialize get-set descriptor type");
8533cfc2420a Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 53244
diff changeset
1593
8533cfc2420a Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 53244
diff changeset
1594 if (PyType_Ready(&PyWrapperDescr_Type) < 0)
8533cfc2420a Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 53244
diff changeset
1595 Py_FatalError("Can't initialize wrapper type");
8533cfc2420a Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 53244
diff changeset
1596
8533cfc2420a Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 53244
diff changeset
1597 if (PyType_Ready(&PyEllipsis_Type) < 0)
8533cfc2420a Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 53244
diff changeset
1598 Py_FatalError("Can't initialize ellipsis type");
8533cfc2420a Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 53244
diff changeset
1599
8533cfc2420a Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 53244
diff changeset
1600 if (PyType_Ready(&PyMemberDescr_Type) < 0)
8533cfc2420a Merged revisions 71734,71738-71739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 53244
diff changeset
1601 Py_FatalError("Can't initialize member descriptor type");
53953
261f594014c6 these builtins have to be initialized
Benjamin Peterson <benjamin@python.org>
parents: 53906
diff changeset
1602
261f594014c6 these builtins have to be initialized
Benjamin Peterson <benjamin@python.org>
parents: 53906
diff changeset
1603 if (PyType_Ready(&PyFilter_Type) < 0)
261f594014c6 these builtins have to be initialized
Benjamin Peterson <benjamin@python.org>
parents: 53906
diff changeset
1604 Py_FatalError("Can't initialize filter type");
261f594014c6 these builtins have to be initialized
Benjamin Peterson <benjamin@python.org>
parents: 53906
diff changeset
1605
261f594014c6 these builtins have to be initialized
Benjamin Peterson <benjamin@python.org>
parents: 53906
diff changeset
1606 if (PyType_Ready(&PyMap_Type) < 0)
261f594014c6 these builtins have to be initialized
Benjamin Peterson <benjamin@python.org>
parents: 53906
diff changeset
1607 Py_FatalError("Can't initialize map type");
261f594014c6 these builtins have to be initialized
Benjamin Peterson <benjamin@python.org>
parents: 53906
diff changeset
1608
261f594014c6 these builtins have to be initialized
Benjamin Peterson <benjamin@python.org>
parents: 53906
diff changeset
1609 if (PyType_Ready(&PyZip_Type) < 0)
261f594014c6 these builtins have to be initialized
Benjamin Peterson <benjamin@python.org>
parents: 53906
diff changeset
1610 Py_FatalError("Can't initialize zip type");
19294
1456ec735087 Add a function _Py_ReadyTypes() which initializes various and sundry
Guido van Rossum <guido@python.org>
parents: 19293
diff changeset
1611 }
1456ec735087 Add a function _Py_ReadyTypes() which initializes various and sundry
Guido van Rossum <guido@python.org>
parents: 19293
diff changeset
1612
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1613
3574
1ec154241747 TRACE_REFS -> Py_TRACE_REFS.
Guido van Rossum <guido@python.org>
parents: 3330
diff changeset
1614 #ifdef Py_TRACE_REFS
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1615
4002
4241582636ad Define reference count admin debug functions to return void.
Guido van Rossum <guido@python.org>
parents: 3986
diff changeset
1616 void
13473
31b1f691a7d9 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1617 _Py_NewReference(PyObject *op)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1618 {
24184
c4dcb80f48ea object.h special-build macro minefield: renamed all the new lexical
Tim Peters <tim.peters@gmail.com>
parents: 24155
diff changeset
1619 _Py_INC_REFTOTAL;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1620 op->ob_refcnt = 1;
28396
d56282c28a1e Improved new Py_TRACE_REFS gimmicks.
Tim Peters <tim.peters@gmail.com>
parents: 28389
diff changeset
1621 _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
1622 _Py_INC_TPALLOCS(op);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1623 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1624
4002
4241582636ad Define reference count admin debug functions to return void.
Guido van Rossum <guido@python.org>
parents: 3986
diff changeset
1625 void
13473
31b1f691a7d9 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1626 _Py_ForgetReference(register PyObject *op)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1627 {
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
1628 #ifdef SLOW_UNREF_CHECK
11969
74b7213fb609 Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
1629 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
1630 #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
1631 if (op->ob_refcnt < 0)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5006
diff changeset
1632 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
1633 if (op == &refchain ||
44064
00048fedbaa0 Partial fix for bug #1306
Christian Heimes <christian@cheimes.de>
parents: 43867
diff changeset
1634 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op) {
00048fedbaa0 Partial fix for bug #1306
Christian Heimes <christian@cheimes.de>
parents: 43867
diff changeset
1635 fprintf(stderr, "* ob\n");
00048fedbaa0 Partial fix for bug #1306
Christian Heimes <christian@cheimes.de>
parents: 43867
diff changeset
1636 _PyObject_Dump(op);
00048fedbaa0 Partial fix for bug #1306
Christian Heimes <christian@cheimes.de>
parents: 43867
diff changeset
1637 fprintf(stderr, "* op->_ob_prev->_ob_next\n");
00048fedbaa0 Partial fix for bug #1306
Christian Heimes <christian@cheimes.de>
parents: 43867
diff changeset
1638 _PyObject_Dump(op->_ob_prev->_ob_next);
00048fedbaa0 Partial fix for bug #1306
Christian Heimes <christian@cheimes.de>
parents: 43867
diff changeset
1639 fprintf(stderr, "* op->_ob_next->_ob_prev\n");
00048fedbaa0 Partial fix for bug #1306
Christian Heimes <christian@cheimes.de>
parents: 43867
diff changeset
1640 _PyObject_Dump(op->_ob_next->_ob_prev);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5006
diff changeset
1641 Py_FatalError("UNREF invalid object");
44064
00048fedbaa0 Partial fix for bug #1306
Christian Heimes <christian@cheimes.de>
parents: 43867
diff changeset
1642 }
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
1643 #ifdef SLOW_UNREF_CHECK
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
1644 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
1645 if (p == op)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
1646 break;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
1647 }
2053
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1869
diff changeset
1648 if (p == &refchain) /* Not found */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5006
diff changeset
1649 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
1650 #endif
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1651 op->_ob_next->_ob_prev = op->_ob_prev;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1652 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
1653 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
1654 _Py_INC_TPFREES(op);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
1655 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
1656
4002
4241582636ad Define reference count admin debug functions to return void.
Guido van Rossum <guido@python.org>
parents: 3986
diff changeset
1657 void
13473
31b1f691a7d9 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1658 _Py_Dealloc(PyObject *op)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 58
diff changeset
1659 {
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44064
diff changeset
1660 destructor dealloc = Py_TYPE(op)->tp_dealloc;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5006
diff changeset
1661 _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
1662 (*dealloc)(op);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1663 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1664
28653
56006d97bcef _Py_PrintReferences(): Changed to print object address at start of each
Tim Peters <tim.peters@gmail.com>
parents: 28616
diff changeset
1665 /* 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
1666 * 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
1667 */
4002
4241582636ad Define reference count admin debug functions to return void.
Guido van Rossum <guido@python.org>
parents: 3986
diff changeset
1668 void
13473
31b1f691a7d9 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1669 _Py_PrintReferences(FILE *fp)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1670 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5006
diff changeset
1671 PyObject *op;
5931
12fe5cc1516d Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
1672 fprintf(fp, "Remaining objects:\n");
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1673 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36977
diff changeset
1674 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
1675 if (PyObject_Print(op, fp, 0) != 0)
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5006
diff changeset
1676 PyErr_Clear();
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1677 putc('\n', fp);
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1678 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1679 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1680
28653
56006d97bcef _Py_PrintReferences(): Changed to print object address at start of each
Tim Peters <tim.peters@gmail.com>
parents: 28616
diff changeset
1681 /* 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
1682 * 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
1683 */
56006d97bcef _Py_PrintReferences(): Changed to print object address at start of each
Tim Peters <tim.peters@gmail.com>
parents: 28616
diff changeset
1684 void
56006d97bcef _Py_PrintReferences(): Changed to print object address at start of each
Tim Peters <tim.peters@gmail.com>
parents: 28616
diff changeset
1685 _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
1686 {
56006d97bcef _Py_PrintReferences(): Changed to print object address at start of each
Tim Peters <tim.peters@gmail.com>
parents: 28616
diff changeset
1687 PyObject *op;
56006d97bcef _Py_PrintReferences(): Changed to print object address at start of each
Tim Peters <tim.peters@gmail.com>
parents: 28616
diff changeset
1688 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
1689 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next)
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36977
diff changeset
1690 fprintf(fp, "%p [%" PY_FORMAT_SIZE_T "d] %s\n", op,
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44064
diff changeset
1691 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
1692 }
56006d97bcef _Py_PrintReferences(): Changed to print object address at start of each
Tim Peters <tim.peters@gmail.com>
parents: 28616
diff changeset
1693
3024
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
1694 PyObject *
13473
31b1f691a7d9 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1695 _Py_GetObjects(PyObject *self, PyObject *args)
3024
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
1696 {
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
1697 int i, n;
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
1698 PyObject *t = NULL;
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
1699 PyObject *res, *op;
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
1700
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
1701 if (!PyArg_ParseTuple(args, "i|O", &n, &t))
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
1702 return NULL;
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
1703 op = refchain._ob_next;
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
1704 res = PyList_New(0);
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
1705 if (res == NULL)
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
1706 return NULL;
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
1707 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
1708 while (op == self || op == args || op == res || op == t ||
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44064
diff changeset
1709 (t != NULL && Py_TYPE(op) != (PyTypeObject *) t)) {
3024
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
1710 op = op->_ob_next;
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
1711 if (op == &refchain)
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
1712 return res;
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
1713 }
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
1714 if (PyList_Append(res, op) < 0) {
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
1715 Py_DECREF(res);
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
1716 return NULL;
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
1717 }
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
1718 op = op->_ob_next;
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
1719 }
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
1720 return res;
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
1721 }
54aa87b1ccee Implemented two new functions in sys:
Sjoerd Mullender <sjoerd@acm.org>
parents: 2827
diff changeset
1722
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1723 #endif
3330
9f1b6e2db8e0 Hack to force loading of cobject.o
Guido van Rossum <guido@python.org>
parents: 3024
diff changeset
1724
9f1b6e2db8e0 Hack to force loading of cobject.o
Guido van Rossum <guido@python.org>
parents: 3024
diff changeset
1725 /* Hack to force loading of cobject.o */
4588
4dc67fdd9e0b Make gcc -Wall happy
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1726 PyTypeObject *_Py_cobject_hack = &PyCObject_Type;
3574
1ec154241747 TRACE_REFS -> Py_TRACE_REFS.
Guido van Rossum <guido@python.org>
parents: 3330
diff changeset
1727
1ec154241747 TRACE_REFS -> Py_TRACE_REFS.
Guido van Rossum <guido@python.org>
parents: 3330
diff changeset
1728
53816
911df82bc25d add a replacement API for PyCObject, PyCapsule #5630
Benjamin Peterson <benjamin@python.org>
parents: 53274
diff changeset
1729 /* Hack to force loading of pycapsule.o */
911df82bc25d add a replacement API for PyCObject, PyCapsule #5630
Benjamin Peterson <benjamin@python.org>
parents: 53274
diff changeset
1730 PyTypeObject *_PyCapsule_hack = &PyCapsule_Type;
911df82bc25d add a replacement API for PyCObject, PyCapsule #5630
Benjamin Peterson <benjamin@python.org>
parents: 53274
diff changeset
1731
911df82bc25d add a replacement API for PyCObject, PyCapsule #5630
Benjamin Peterson <benjamin@python.org>
parents: 53274
diff changeset
1732
3574
1ec154241747 TRACE_REFS -> Py_TRACE_REFS.
Guido van Rossum <guido@python.org>
parents: 3330
diff changeset
1733 /* 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
1734 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
1735
12fe5cc1516d Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
1736
14006
ed369f0aa32b Updated comment
Andrew M. Kuchling <amk@amk.ca>
parents: 13976
diff changeset
1737 /* 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
1738
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
1739 void *
13473
31b1f691a7d9 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1740 PyMem_Malloc(size_t nbytes)
5931
12fe5cc1516d Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
1741 {
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12481
diff changeset
1742 return PyMem_MALLOC(nbytes);
5931
12fe5cc1516d Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
1743 }
12fe5cc1516d Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
1744
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
1745 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
1746 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
1747 {
22985
d9d82c1ef864 First stab at rationalizing the PyMem_ API. Mixing PyObject_xyz with
Tim Peters <tim.peters@gmail.com>
parents: 22982
diff changeset
1748 return PyMem_REALLOC(p, nbytes);
5931
12fe5cc1516d Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
1749 }
12fe5cc1516d Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
1750
12fe5cc1516d Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
1751 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
1752 PyMem_Free(void *p)
5931
12fe5cc1516d Added _Py_ResetReferences(), if tracing references.
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
1753 {
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12481
diff changeset
1754 PyMem_FREE(p);
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12481
diff changeset
1755 }
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12481
diff changeset
1756
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12481
diff changeset
1757
7984
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
1758 /* 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
1759 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
1760 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
1761 Py_ReprLeave() to avoid infinite recursion.
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
1762
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
1763 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
1764 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
1765 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
1766
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
1767 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
1768 */
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
1769
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
1770 #define KEY "Py_Repr"
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
1771
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
1772 int
13473
31b1f691a7d9 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1773 Py_ReprEnter(PyObject *obj)
7984
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
1774 {
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
1775 PyObject *dict;
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
1776 PyObject *list;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
1777 Py_ssize_t i;
7984
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
1778
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
1779 dict = PyThreadState_GetDict();
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
1780 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
1781 return 0;
7984
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
1782 list = PyDict_GetItemString(dict, KEY);
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
1783 if (list == NULL) {
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
1784 list = PyList_New(0);
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
1785 if (list == NULL)
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
1786 return -1;
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
1787 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
1788 return -1;
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
1789 Py_DECREF(list);
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
1790 }
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
1791 i = PyList_GET_SIZE(list);
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
1792 while (--i >= 0) {
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
1793 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
1794 return 1;
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
1795 }
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
1796 PyList_Append(list, obj);
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
1797 return 0;
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
1798 }
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
1799
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
1800 void
13473
31b1f691a7d9 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1801 Py_ReprLeave(PyObject *obj)
7984
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
1802 {
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
1803 PyObject *dict;
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
1804 PyObject *list;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
1805 Py_ssize_t i;
7984
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
1806
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
1807 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
1808 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
1809 return;
7984
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
1810 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
1811 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
1812 return;
7984
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
1813 i = PyList_GET_SIZE(list);
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
1814 /* 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
1815 while (--i >= 0) {
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
1816 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
1817 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
1818 break;
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
1819 }
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
1820 }
1d0eb2e96d76 Add implementations of Py_Repr{Enter,Leave}.
Guido van Rossum <guido@python.org>
parents: 7935
diff changeset
1821 }
11999
c94247b5c355 Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1822
24122
d332484e394a Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents: 24121
diff changeset
1823 /* Trashcan support. */
12052
5d3165e30bce Christian Tismer:
Guido van Rossum <guido@python.org>
parents: 11999
diff changeset
1824
24122
d332484e394a Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents: 24121
diff changeset
1825 /* 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
1826 int _PyTrash_delete_nesting = 0;
12481
a8898a172fe2 Christian Tismer -- total rewrite on trashcan code.
Guido van Rossum <guido@python.org>
parents: 12406
diff changeset
1827
24122
d332484e394a Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents: 24121
diff changeset
1828 /* 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
1829 * 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
1830 */
d332484e394a Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents: 24121
diff changeset
1831 PyObject *_PyTrash_delete_later = NULL;
11999
c94247b5c355 Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1832
24122
d332484e394a Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents: 24121
diff changeset
1833 /* 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
1834 * 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
1835 * 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
1836 */
11999
c94247b5c355 Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1837 void
13473
31b1f691a7d9 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1838 _PyTrash_deposit_object(PyObject *op)
11999
c94247b5c355 Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1839 {
24122
d332484e394a Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents: 24121
diff changeset
1840 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
1841 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
1842 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
1843 _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
1844 _PyTrash_delete_later = op;
11999
c94247b5c355 Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1845 }
c94247b5c355 Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1846
24122
d332484e394a Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents: 24121
diff changeset
1847 /* 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
1848 * 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
1849 */
11999
c94247b5c355 Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1850 void
13473
31b1f691a7d9 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1851 _PyTrash_destroy_chain(void)
11999
c94247b5c355 Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1852 {
c94247b5c355 Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1853 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
1854 PyObject *op = _PyTrash_delete_later;
44351
86a2679dca46 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents: 44064
diff changeset
1855 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
1856
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
1857 _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
1858 (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
1859
24122
d332484e394a Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents: 24121
diff changeset
1860 /* 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
1861 * 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
1862 * 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
1863 * 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
1864 * 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
1865 */
d332484e394a Trashcan cleanup: Now that cyclic gc is always there, the trashcan
Tim Peters <tim.peters@gmail.com>
parents: 24121
diff changeset
1866 assert(op->ob_refcnt == 0);
11999
c94247b5c355 Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1867 ++_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
1868 (*dealloc)(op);
11999
c94247b5c355 Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1869 --_PyTrash_delete_nesting;
c94247b5c355 Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1870 }
c94247b5c355 Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents: 11969
diff changeset
1871 }
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36977
diff changeset
1872
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36977
diff changeset
1873 #ifdef __cplusplus
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36977
diff changeset
1874 }
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36977
diff changeset
1875 #endif