Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(167612)

Side by Side Diff: Lib/inspect.py

Issue 18929: inspect.classify_class_attrs ignores metaclass
Patch Set: Created 6 years, 3 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Doc/library/inspect.rst ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 """Get useful information from live Python objects. 1 """Get useful information from live Python objects.
2 2
3 This module encapsulates the interface provided by the internal special 3 This module encapsulates the interface provided by the internal special
4 attributes (co_*, im_*, tb_*, etc.) in a friendlier fashion. 4 attributes (co_*, im_*, tb_*, etc.) in a friendlier fashion.
5 It also provides some help for examining source code and class layout. 5 It also provides some help for examining source code and class layout.
6 6
7 Here are some of the useful functions provided by this module: 7 Here are some of the useful functions provided by this module:
8 8
9 ismodule(), isclass(), ismethod(), isfunction(), isgeneratorfunction(), 9 ismodule(), isclass(), ismethod(), isfunction(), isgeneratorfunction(),
10 isgenerator(), istraceback(), isframe(), iscode(), isbuiltin(), 10 isgenerator(), istraceback(), isframe(), iscode(), isbuiltin(),
(...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after
301 'method' any other flavor of method 301 'method' any other flavor of method
302 'data' not a method 302 'data' not a method
303 303
304 2. The class which defined this attribute (a class). 304 2. The class which defined this attribute (a class).
305 305
306 3. The object as obtained directly from the defining class's 306 3. The object as obtained directly from the defining class's
307 __dict__, not via getattr. This is especially important for 307 __dict__, not via getattr. This is especially important for
308 data attributes: C.data is just a data object, but 308 data attributes: C.data is just a data object, but
309 C.__dict__['data'] may be a data descriptor with additional 309 C.__dict__['data'] may be a data descriptor with additional
310 info, like a __doc__ string. 310 info, like a __doc__ string.
311
312 If one of the items in dir(cls) is stored in the metaclass it will now
313 be discovered and not have None be listed as the class in which it was
314 defined.
311 """ 315 """
312 316
313 mro = getmro(cls) 317 mro = getmro(cls)
318 metamro = getmro(type(cls)) # for attributes stored in the metaclass
314 names = dir(cls) 319 names = dir(cls)
315 result = [] 320 result = []
316 for name in names: 321 for name in names:
317 # Get the object associated with the name, and where it was defined. 322 # Get the object associated with the name, and where it was defined.
318 # Getting an obj from the __dict__ sometimes reveals more than 323 # Getting an obj from the __dict__ sometimes reveals more than
319 # using getattr. Static and class methods are dramatic examples. 324 # using getattr. Static and class methods are dramatic examples.
320 # Furthermore, some objects may raise an Exception when fetched with 325 # Furthermore, some objects may raise an Exception when fetched with
321 # getattr(). This is the case with some descriptors (bug #1785). 326 # getattr(). This is the case with some descriptors (bug #1785).
322 # Thus, we only use getattr() as a last resort. 327 # Thus, we only use getattr() as a last resort.
323 homecls = None 328 homecls = None
324 for base in (cls,) + mro: 329 for base in (cls,) + mro + metamro:
325 if name in base.__dict__: 330 if name in base.__dict__:
326 obj = base.__dict__[name] 331 obj = base.__dict__[name]
327 homecls = base 332 homecls = base
328 break 333 break
329 else: 334 else:
330 obj = getattr(cls, name) 335 obj = getattr(cls, name)
331 homecls = getattr(obj, "__objclass__", homecls) 336 homecls = getattr(obj, "__objclass__", homecls)
332 337
333 # Classify the object. 338 # Classify the object.
334 if isinstance(obj, staticmethod): 339 if isinstance(obj, staticmethod):
(...skipping 1762 matching lines...) Expand 10 before | Expand all | Expand 10 after
2097 2102
2098 result.append(formatted) 2103 result.append(formatted)
2099 2104
2100 rendered = '({})'.format(', '.join(result)) 2105 rendered = '({})'.format(', '.join(result))
2101 2106
2102 if self.return_annotation is not _empty: 2107 if self.return_annotation is not _empty:
2103 anno = formatannotation(self.return_annotation) 2108 anno = formatannotation(self.return_annotation)
2104 rendered += ' -> {}'.format(anno) 2109 rendered += ' -> {}'.format(anno)
2105 2110
2106 return rendered 2111 return rendered
OLDNEW
« no previous file with comments | « Doc/library/inspect.rst ('k') | no next file » | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+