##// END OF EJS Templates
Remove mention of Traits (removed in e1ced0b3)
Anthony Sottile -
Show More
@@ -1,58 +1,58 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 #-----------------------------------------------------------------------------
5 #-----------------------------------------------------------------------------
6 # Copyright (C) 2008-2011 The IPython Development Team
6 # Copyright (C) 2008-2011 The IPython Development Team
7 #
7 #
8 # Distributed under the terms of the BSD License. The full license is in
8 # Distributed under the terms of the BSD License. The full license is in
9 # the file COPYING, distributed as part of this software.
9 # the file COPYING, distributed as part of this software.
10 #-----------------------------------------------------------------------------
10 #-----------------------------------------------------------------------------
11
11
12 #-----------------------------------------------------------------------------
12 #-----------------------------------------------------------------------------
13 # Imports
13 # Imports
14 #-----------------------------------------------------------------------------
14 #-----------------------------------------------------------------------------
15 from .py3compat import string_types
15 from .py3compat import string_types
16
16
17 #-----------------------------------------------------------------------------
17 #-----------------------------------------------------------------------------
18 # Code
18 # Code
19 #-----------------------------------------------------------------------------
19 #-----------------------------------------------------------------------------
20
20
21
21
22 def safe_hasattr(obj, attr):
22 def safe_hasattr(obj, attr):
23 """In recent versions of Python, hasattr() only catches AttributeError.
23 """In recent versions of Python, hasattr() only catches AttributeError.
24 This catches all errors.
24 This catches all errors.
25 """
25 """
26 try:
26 try:
27 getattr(obj, attr)
27 getattr(obj, attr)
28 return True
28 return True
29 except:
29 except:
30 return False
30 return False
31
31
32
32
33 def dir2(obj):
33 def dir2(obj):
34 """dir2(obj) -> list of strings
34 """dir2(obj) -> list of strings
35
35
36 Extended version of the Python builtin dir(), which does a few extra
36 Extended version of the Python builtin dir(), which does a few extra
37 checks, and handles Traits objects, which can confuse dir().
37 checks.
38
38
39 This version is guaranteed to return only a list of true strings, whereas
39 This version is guaranteed to return only a list of true strings, whereas
40 dir() returns anything that objects inject into themselves, even if they
40 dir() returns anything that objects inject into themselves, even if they
41 are later not really valid for attribute access (many extension libraries
41 are later not really valid for attribute access (many extension libraries
42 have such bugs).
42 have such bugs).
43 """
43 """
44
44
45 # Start building the attribute list via dir(), and then complete it
45 # Start building the attribute list via dir(), and then complete it
46 # with a few extra special-purpose calls.
46 # with a few extra special-purpose calls.
47
47
48 try:
48 try:
49 words = set(dir(obj))
49 words = set(dir(obj))
50 except Exception:
50 except Exception:
51 # TypeError: dir(obj) does not return a list
51 # TypeError: dir(obj) does not return a list
52 words = set()
52 words = set()
53
53
54 # filter out non-string attributes which may be stuffed by dir() calls
54 # filter out non-string attributes which may be stuffed by dir() calls
55 # and poor coding in third-party modules
55 # and poor coding in third-party modules
56
56
57 words = [w for w in words if isinstance(w, string_types)]
57 words = [w for w in words if isinstance(w, string_types)]
58 return sorted(words)
58 return sorted(words)
General Comments 0
You need to be logged in to leave comments. Login now