Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
PyObjectDir27.inc
Go to the documentation of this file.
1/* Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
22011, 2012, 2013, 2014 Python Software Foundation; All Rights Reserved
3
4Copied from Objects/object.c under the PSF License Version 2.
5
6PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2
7--------------------------------------------
8
91. This LICENSE AGREEMENT is between the Python Software Foundation
10("PSF"), and the Individual or Organization ("Licensee") accessing and
11otherwise using this software ("Python") in source or binary form and
12its associated documentation.
13
142. Subject to the terms and conditions of this License Agreement, PSF hereby
15grants Licensee a nonexclusive, royalty-free, world-wide license to reproduce,
16analyze, test, perform and/or display publicly, prepare derivative works,
17distribute, and otherwise use Python alone or in any derivative version,
18provided, however, that PSF's License Agreement and PSF's notice of copyright,
19i.e., "Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
202011, 2012, 2013, 2014 Python Software Foundation; All Rights Reserved" are retained
21in Python alone or in any derivative version prepared by Licensee.
22
233. In the event Licensee prepares a derivative work that is based on
24or incorporates Python or any part thereof, and wants to make
25the derivative work available to others as provided herein, then
26Licensee hereby agrees to include in any such work a brief summary of
27the changes made to Python.
28
294. PSF is making Python available to Licensee on an "AS IS"
30basis. PSF MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR
31IMPLIED. BY WAY OF EXAMPLE, BUT NOT LIMITATION, PSF MAKES NO AND
32DISCLAIMS ANY REPRESENTATION OR WARRANTY OF MERCHANTABILITY OR FITNESS
33FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF PYTHON WILL NOT
34INFRINGE ANY THIRD PARTY RIGHTS.
35
365. PSF SHALL NOT BE LIABLE TO LICENSEE OR ANY OTHER USERS OF PYTHON
37FOR ANY INCIDENTAL, SPECIAL, OR CONSEQUENTIAL DAMAGES OR LOSS AS
38A RESULT OF MODIFYING, DISTRIBUTING, OR OTHERWISE USING PYTHON,
39OR ANY DERIVATIVE THEREOF, EVEN IF ADVISED OF THE POSSIBILITY THEREOF.
40
416. This License Agreement will automatically terminate upon a material
42breach of its terms and conditions.
43
447. Nothing in this License Agreement shall be deemed to create any
45relationship of agency, partnership, or joint venture between PSF and
46Licensee. This License Agreement does not grant permission to use PSF
47trademarks or trade name in a trademark sense to endorse or promote
48products or services of Licensee, or any third party.
49
508. By copying, installing or otherwise using Python, Licensee
51agrees to be bound by the terms and conditions of this License
52Agreement.
53
54*/
55
56/* Helper for PyObject_Dir.
57 Merge the __dict__ of aclass into dict, and recursively also all
58 the __dict__s of aclass's base classes. The order of merging isn't
59 defined, as it's expected that only the final set of dict keys is
60 interesting.
61 Return 0 on success, -1 on error.
62*/
63
64static int merge_class_dict(PyObject* dict, PyObject* aclass)
65{
66 PyObject *classdict;
67 PyObject *bases;
68
69 assert(PyDict_Check(dict));
70 assert(aclass);
71
72 /* Merge in the type's dict (if any). */
73 classdict = PyObject_GetAttrString(aclass, "__dict__");
74 if (classdict == NULL)
75 PyErr_Clear();
76 else {
77 int status = PyDict_Update(dict, classdict);
78 Py_DECREF(classdict);
79 if (status < 0)
80 return -1;
81 }
82
83 /* Recursively merge in the base types' (if any) dicts. */
84 bases = PyObject_GetAttrString(aclass, "__bases__");
85 if (bases == NULL)
86 PyErr_Clear();
87 else {
88 /* We have no guarantee that bases is a real tuple */
89 Py_ssize_t i, n;
90 n = PySequence_Size(bases); /* This better be right */
91 if (n < 0)
92 PyErr_Clear();
93 else {
94 for (i = 0; i < n; i++) {
95 int status;
96 PyObject *base = PySequence_GetItem(bases, i);
97 if (base == NULL) {
98 Py_DECREF(bases);
99 return -1;
100 }
101 status = merge_class_dict(dict, base);
102 Py_DECREF(base);
103 if (status < 0) {
104 Py_DECREF(bases);
105 return -1;
106 }
107 }
108 }
109 Py_DECREF(bases);
110 }
111 return 0;
112}
113
114/* Helper for PyObject_Dir of type objects: returns __dict__ and __bases__.
115 We deliberately don't suck up its __class__, as methods belonging to the
116 metaclass would probably be more confusing than helpful.
117*/
118static PyObject *
119_specialized_dir_type(PyObject *obj)
120{
121 PyObject *result = NULL;
122 PyObject *dict = PyDict_New();
123
124 if (dict != NULL && merge_class_dict(dict, obj) == 0)
125 result = PyDict_Keys(dict);
126
127 Py_XDECREF(dict);
128 return result;
129}
130
131static PyObject* _generic_dir(PyObject *obj)
132{
133 if (PyType_Check(obj))
134 return _specialized_dir_type(obj);
135
136 PyObject* result = NULL;
137 PyObject* dict = NULL;
138 PyObject* itsclass = NULL;
139
140 /* Get __dict__ (which may or may not be a real dict...) */
141 dict = PyObject_GetAttrString(obj, "__dict__");
142 if (dict == NULL) {
143 PyErr_Clear();
144 dict = PyDict_New();
145 } else if (!PyDict_Check(dict)) {
146 Py_DECREF(dict);
147 dict = PyDict_New();
148 } else {
149 /* Copy __dict__ to avoid mutating it. */
150 PyObject* temp = PyDict_Copy(dict);
151 Py_DECREF(dict);
152 dict = temp;
153 }
154
155 if (dict == NULL)
156 goto error;
157
158 /* Merge in attrs reachable from its class. */
159 itsclass = PyObject_GetAttrString(obj, "__class__");
160 if (itsclass == NULL)
161 /* XXX(tomer): Perhaps fall back to obj->ob_type if no
162 __class__ exists? */
163 PyErr_Clear();
164 else {
165 if (merge_class_dict(dict, itsclass) != 0)
166 goto error;
167 }
168
169 result = PyDict_Keys(dict);
170 /* fall through */
171error:
172 Py_XDECREF(itsclass);
173 Py_XDECREF(dict);
174 return result;
175}
_object PyObject
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t result
const Int_t n
Definition legend1.C:16