##// END OF EJS Templates
restore tab completion behavior...
Paul Ivanov -
Show More
@@ -1,81 +1,84 b''
1 # encoding: utf-8
1 # encoding: utf-8
2 """A fancy version of Python's builtin :func:`dir` function.
2 """A fancy version of Python's builtin :func:`dir` function.
3 """
3 """
4
4
5 # Copyright (c) IPython Development Team.
5 # Copyright (c) IPython Development Team.
6 # Distributed under the terms of the Modified BSD License.
6 # Distributed under the terms of the Modified BSD License.
7
7
8 import inspect
8 import inspect
9 from .py3compat import string_types
9 from .py3compat import string_types
10
10
11
11
12 def safe_hasattr(obj, attr):
12 def safe_hasattr(obj, attr):
13 """In recent versions of Python, hasattr() only catches AttributeError.
13 """In recent versions of Python, hasattr() only catches AttributeError.
14 This catches all errors.
14 This catches all errors.
15 """
15 """
16 try:
16 try:
17 getattr(obj, attr)
17 getattr(obj, attr)
18 return True
18 return True
19 except:
19 except:
20 return False
20 return False
21
21
22
22
23 def dir2(obj):
23 def dir2(obj):
24 """dir2(obj) -> list of strings
24 """dir2(obj) -> list of strings
25
25
26 Extended version of the Python builtin dir(), which does a few extra
26 Extended version of the Python builtin dir(), which does a few extra
27 checks.
27 checks.
28
28
29 This version is guaranteed to return only a list of true strings, whereas
29 This version is guaranteed to return only a list of true strings, whereas
30 dir() returns anything that objects inject into themselves, even if they
30 dir() returns anything that objects inject into themselves, even if they
31 are later not really valid for attribute access (many extension libraries
31 are later not really valid for attribute access (many extension libraries
32 have such bugs).
32 have such bugs).
33 """
33 """
34
34
35 # Start building the attribute list via dir(), and then complete it
35 # Start building the attribute list via dir(), and then complete it
36 # with a few extra special-purpose calls.
36 # with a few extra special-purpose calls.
37
37
38 try:
38 try:
39 words = set(dir(obj))
39 words = set(dir(obj))
40 except Exception:
40 except Exception:
41 # TypeError: dir(obj) does not return a list
41 # TypeError: dir(obj) does not return a list
42 words = set()
42 words = set()
43
43
44 if safe_hasattr(obj, '__class__'):
45 words |= set(dir(obj.__class__))
46
44 # filter out non-string attributes which may be stuffed by dir() calls
47 # filter out non-string attributes which may be stuffed by dir() calls
45 # and poor coding in third-party modules
48 # and poor coding in third-party modules
46
49
47 words = [w for w in words if isinstance(w, string_types)]
50 words = [w for w in words if isinstance(w, string_types)]
48 return sorted(words)
51 return sorted(words)
49
52
50
53
51 def get_real_method(obj, name):
54 def get_real_method(obj, name):
52 """Like getattr, but with a few extra sanity checks:
55 """Like getattr, but with a few extra sanity checks:
53
56
54 - If obj is a class, ignore its methods
57 - If obj is a class, ignore its methods
55 - Check if obj is a proxy that claims to have all attributes
58 - Check if obj is a proxy that claims to have all attributes
56 - Catch attribute access failing with any exception
59 - Catch attribute access failing with any exception
57 - Check that the attribute is a callable object
60 - Check that the attribute is a callable object
58
61
59 Returns the method or None.
62 Returns the method or None.
60 """
63 """
61 if inspect.isclass(obj):
64 if inspect.isclass(obj):
62 return None
65 return None
63
66
64 try:
67 try:
65 canary = getattr(obj, '_ipython_canary_method_should_not_exist_', None)
68 canary = getattr(obj, '_ipython_canary_method_should_not_exist_', None)
66 except Exception:
69 except Exception:
67 return None
70 return None
68
71
69 if canary is not None:
72 if canary is not None:
70 # It claimed to have an attribute it should never have
73 # It claimed to have an attribute it should never have
71 return None
74 return None
72
75
73 try:
76 try:
74 m = getattr(obj, name, None)
77 m = getattr(obj, name, None)
75 except Exception:
78 except Exception:
76 return None
79 return None
77
80
78 if callable(m):
81 if callable(m):
79 return m
82 return m
80
83
81 return None
84 return None
General Comments 0
You need to be logged in to leave comments. Login now