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#if PY_VERSION_HEX < 0x03000000
115
116/* Helper for PyObject_Dir.
117 If obj has an attr named attrname that's a list, merge its string
118 elements into keys of dict.
119 Return 0 on success, -1 on error. Errors due to not finding the attr,
120 or the attr not being a list, are suppressed.
121*/
122
123static int
124merge_list_attr(PyObject* dict, PyObject* obj, const char *attrname)
125{
126 PyObject *list;
127 int result = 0;
128
129 assert(PyDict_Check(dict));
130 assert(obj);
131 assert(attrname);
132
133 list = PyObject_GetAttrString(obj, attrname);
134 if (list == NULL)
135 PyErr_Clear();
136
137 else if (PyList_Check(list)) {
138 int i;
139 for (i = 0; i < PyList_GET_SIZE(list); ++i) {
140 PyObject *item = PyList_GET_ITEM(list, i);
141 if (PyString_Check(item)) {
142 result = PyDict_SetItem(dict, item, Py_None);
143 if (result < 0)
144 break;
145 }
146 }
147 if (Py_Py3kWarningFlag &&
148 (strcmp(attrname, "__members__") == 0 ||
149 strcmp(attrname, "__methods__") == 0)) {
150 if (PyErr_WarnEx(PyExc_DeprecationWarning,
151 "__members__ and __methods__ not "
152 "supported in 3.x", 1) < 0) {
153 Py_XDECREF(list);
154 return -1;
155 }
156 }
157 }
158
159 Py_XDECREF(list);
160 return result;
161}
162
163#endif
164
165/* Helper for PyObject_Dir of type objects: returns __dict__ and __bases__.
166 We deliberately don't suck up its __class__, as methods belonging to the
167 metaclass would probably be more confusing than helpful.
168*/
169static PyObject *
170_specialized_dir_type(PyObject *obj)
171{
172 PyObject *result = NULL;
173 PyObject *dict = PyDict_New();
174
175 if (dict != NULL && merge_class_dict(dict, obj) == 0)
176 result = PyDict_Keys(dict);
177
178 Py_XDECREF(dict);
179 return result;
180}
181
182static PyObject* _generic_dir(PyObject *obj)
183{
184 if (PyType_Check(obj) || PyClass_Check(obj))
185 return _specialized_dir_type(obj);
186
187 PyObject *result = NULL;
188 PyObject *dict = NULL;
189 PyObject *itsclass = NULL;
190
191 /* Get __dict__ (which may or may not be a real dict...) */
192 dict = PyObject_GetAttrString(obj, "__dict__");
193 if (dict == NULL) {
194 PyErr_Clear();
195 dict = PyDict_New();
196 }
197 else if (!PyDict_Check(dict)) {
198 Py_DECREF(dict);
199 dict = PyDict_New();
200 }
201 else {
202 /* Copy __dict__ to avoid mutating it. */
203 PyObject *temp = PyDict_Copy(dict);
204 Py_DECREF(dict);
205 dict = temp;
206 }
207
208 if (dict == NULL)
209 goto error;
210
211#if PY_VERSION_HEX < 0x03000000
212 /* Merge in __members__ and __methods__ (if any).
213 * This is removed in Python 3000. */
214 if (merge_list_attr(dict, obj, "__members__") < 0)
215 goto error;
216 if (merge_list_attr(dict, obj, "__methods__") < 0)
217 goto error;
218#endif
219
220 /* Merge in attrs reachable from its class. */
221 itsclass = PyObject_GetAttrString(obj, "__class__");
222 if (itsclass == NULL)
223 /* XXX(tomer): Perhaps fall back to obj->ob_type if no
224 __class__ exists? */
225 PyErr_Clear();
226 else {
227 if (merge_class_dict(dict, itsclass) != 0)
228 goto error;
229 }
230
231 result = PyDict_Keys(dict);
232 /* fall through */
233error:
234 Py_XDECREF(itsclass);
235 Py_XDECREF(dict);
236 return result;
237}
int Py_ssize_t
Definition CPyCppyy.h:236
_object PyObject
const Int_t n
Definition legend1.C:16