cpython: 902f694a7b0e (original) (raw)

Mercurial > cpython

changeset 74104:902f694a7b0e 3.2

Issue #1785: Fix inspect and pydoc with misbehaving descriptors. Also fixes issue #13581: `help(type)` wouldn't display anything. [#1785]

Antoine Pitrou solipsis@pitrou.net
date Wed, 21 Dec 2011 09:57:40 +0100
parents 40a211eb4fda
children b08bf8df8eec 19df72a77b39
files Lib/inspect.py Lib/pydoc.py Lib/test/test_inspect.py Misc/NEWS
diffstat 4 files changed, 151 insertions(+), 38 deletions(-)[+] [-] Lib/inspect.py 79 Lib/pydoc.py 29 Lib/test/test_inspect.py 79 Misc/NEWS 2

line wrap: on

line diff

--- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -100,11 +100,11 @@ def ismethoddescriptor(object): tests return false from the ismethoddescriptor() test, simply because the other tests promise more -- you can, e.g., count on having the func attribute (etc) when an object passes ismethod()."""

def isdatadescriptor(object): """Return true if the object is a data descriptor. @@ -114,7 +114,11 @@ def isdatadescriptor(object): Typically, data descriptors will also have name and doc attributes (properties, getsets, and members have both of these attributes), but this is not guaranteed."""

if hasattr(types, 'MemberDescriptorType'): # CPython and equivalent @@ -254,12 +258,23 @@ def isabstract(object): def getmembers(object, predicate=None): """Return all members of an object as (name, value) pairs sorted by name. Optionally, only return members that satisfy a given predicate."""

-

-

-

# Classify the object. if isinstance(obj, staticmethod): @@ -327,11 +333,18 @@ def classify_class_attrs(cls): kind = "class method" elif isinstance(obj, property): kind = "property"

result.append(Attribute(name, kind, homecls, obj))

--- a/Lib/pydoc.py +++ b/Lib/pydoc.py @@ -754,8 +754,15 @@ class HTMLDoc(Doc): hr.maybe() push(msg) for name, kind, homecls, value in ok:

@@ -796,7 +803,12 @@ class HTMLDoc(Doc): mdict = {} for key, kind, homecls, value in attrs: mdict[key] = anchor = '#' + name + '-' + key

@@ -1180,8 +1192,15 @@ location listed above. hr.maybe() push(msg) for name, kind, homecls, value in ok:

def spilldescriptors(msg, attrs, predicate):

--- a/Lib/test/test_inspect.py +++ b/Lib/test/test_inspect.py @@ -425,10 +425,37 @@ class TestNoEOL(GetSourceBase): def test_class(self): self.assertSourceEqual(self.fodderModule.X, 1, 2) + +class _BrokenDataDescriptor(object):

+

+

+ + +class _BrokenMethodDescriptor(object):

+

+ +

Helper for testing classify_class_attrs.

def attrs_wo_objs(cls): return [t[:3] for t in inspect.classify_class_attrs(cls)] + class TestClassesAndFunctions(unittest.TestCase): def test_newstyle_mro(self): # The same w/ new-class MRO. @@ -525,6 +552,9 @@ class TestClassesAndFunctions(unittest.T datablob = '1'

+ attrs = attrs_wo_objs(A) self.assertIn(('s', 'static method', A), attrs, 'missing static method') self.assertIn(('c', 'class method', A), attrs, 'missing class method') @@ -533,6 +563,8 @@ class TestClassesAndFunctions(unittest.T 'missing plain method: %r' % attrs) self.assertIn(('m1', 'method', A), attrs, 'missing plain method') self.assertIn(('datablob', 'data', A), attrs, 'missing data')

class B(A): @@ -545,6 +577,8 @@ class TestClassesAndFunctions(unittest.T self.assertIn(('m', 'method', B), attrs, 'missing plain method') self.assertIn(('m1', 'method', A), attrs, 'missing plain method') self.assertIn(('datablob', 'data', A), attrs, 'missing data')

class C(A): @@ -559,6 +593,8 @@ class TestClassesAndFunctions(unittest.T self.assertIn(('m', 'method', C), attrs, 'missing plain method') self.assertIn(('m1', 'method', A), attrs, 'missing plain method') self.assertIn(('datablob', 'data', A), attrs, 'missing data')

class D(B, C): @@ -571,6 +607,49 @@ class TestClassesAndFunctions(unittest.T self.assertIn(('m', 'method', B), attrs, 'missing plain method') self.assertIn(('m1', 'method', D), attrs, 'missing plain method') self.assertIn(('datablob', 'data', A), attrs, 'missing data')

+

+

+

+

+

+

+

+ class TestGetcallargsFunctions(unittest.TestCase):

--- a/Misc/NEWS +++ b/Misc/NEWS @@ -97,6 +97,8 @@ Core and Builtins Library ------- +- Issue #1785: Fix inspect and pydoc with misbehaving descriptors. +