5
5
# src/engine/SCons/Util.py.
6
6
7
7
import types
8
- try :
9
- from collections import UserDict , UserList , UserString
10
- except ImportError :
11
- # No 'collections' module or no UserFoo in collections
12
- exec ('from UserDict import UserDict' )
13
- exec ('from UserList import UserList' )
14
- exec ('from UserString import UserString' )
15
-
16
- InstanceType = types .InstanceType
8
+ from collections import UserDict , UserList , UserString
9
+
17
10
DictType = dict
18
11
ListType = list
19
12
StringType = str
20
- try : unicode
21
- except NameError :
22
- UnicodeType = None
23
- else :
24
- UnicodeType = unicode
25
13
26
14
27
15
# The original implementations, pretty straightforward checks for the
28
16
# type of the object and whether it's an instance of the corresponding
29
17
# User* type.
30
18
31
19
def original_is_Dict (e ):
32
- return isinstance (e , (dict ,UserDict ))
20
+ return isinstance (e , (dict , UserDict ))
33
21
34
22
def original_is_List (e ):
35
- return isinstance (e , (list ,UserList ))
36
-
37
- if UnicodeType is not None :
38
- def original_is_String (e ):
39
- return isinstance (e , (str ,unicode ,UserString ))
40
- else :
41
- def original_is_String (e ):
42
- return isinstance (e , (str ,UserString ))
23
+ return isinstance (e , (list , UserList ))
43
24
25
+ def original_is_String (e ):
26
+ return isinstance (e , (str , UserString ))
44
27
45
28
46
29
# New candidates that explicitly check for whether the object is an
47
30
# InstanceType before calling isinstance() on the corresponding User*
48
31
# type.
32
+ # InstanceType was only for old-style classes, so absent in Python 3
33
+ # this this is no different than the previous
49
34
50
35
def checkInstanceType_is_Dict (e ):
51
- return isinstance (e , dict ) or \
52
- (isinstance (e , types .InstanceType ) and isinstance (e , UserDict ))
36
+ return isinstance (e , (dict , UserDict ))
53
37
54
38
def checkInstanceType_is_List (e ):
55
- return isinstance (e , list ) \
56
- or (isinstance (e , types .InstanceType ) and isinstance (e , UserList ))
57
-
58
- if UnicodeType is not None :
59
- def checkInstanceType_is_String (e ):
60
- return isinstance (e , str ) \
61
- or isinstance (e , unicode ) \
62
- or (isinstance (e , types .InstanceType ) and isinstance (e , UserString ))
63
- else :
64
- def checkInstanceType_is_String (e ):
65
- return isinstance (e , str ) \
66
- or (isinstance (e , types .InstanceType ) and isinstance (e , UserString ))
39
+ return isinstance (e , (list , UserList ))
67
40
41
+ def checkInstanceType_is_String (e ):
42
+ return isinstance (e , (str , UserString ))
68
43
69
44
70
45
# Improved candidates that cache the type(e) result in a variable
71
46
# before doing any checks.
72
47
73
48
def cache_type_e_is_Dict (e ):
74
49
t = type (e )
75
- return t is dict or \
76
- (t is types .InstanceType and isinstance (e , UserDict ))
50
+ return t is dict or isinstance (e , UserDict )
77
51
78
52
def cache_type_e_is_List (e ):
79
53
t = type (e )
80
- return t is list \
81
- or (t is types .InstanceType and isinstance (e , UserList ))
82
-
83
- if UnicodeType is not None :
84
- def cache_type_e_is_String (e ):
85
- t = type (e )
86
- return t is str \
87
- or t is unicode \
88
- or (t is types .InstanceType and isinstance (e , UserString ))
89
- else :
90
- def cache_type_e_is_String (e ):
91
- t = type (e )
92
- return t is str \
93
- or (t is types .InstanceType and isinstance (e , UserString ))
54
+ return t is list or isinstance (e , UserList )
94
55
56
+ def cache_type_e_is_String (e ):
57
+ t = type (e )
58
+ return t is str or isinstance (e , UserString )
95
59
96
60
97
61
# Improved candidates that cache the type(e) result in a variable
@@ -100,26 +64,15 @@ def cache_type_e_is_String(e):
100
64
101
65
def global_cache_type_e_is_Dict (e ):
102
66
t = type (e )
103
- return t is DictType or \
104
- (t is InstanceType and isinstance (e , UserDict ))
67
+ return t is DictType or isinstance (e , UserDict )
105
68
106
69
def global_cache_type_e_is_List (e ):
107
70
t = type (e )
108
- return t is ListType \
109
- or (t is InstanceType and isinstance (e , UserList ))
110
-
111
- if UnicodeType is not None :
112
- def global_cache_type_e_is_String (e ):
113
- t = type (e )
114
- return t is StringType \
115
- or t is UnicodeType \
116
- or (t is InstanceType and isinstance (e , UserString ))
117
- else :
118
- def global_cache_type_e_is_String (e ):
119
- t = type (e )
120
- return t is StringType \
121
- or (t is InstanceType and isinstance (e , UserString ))
71
+ return t is ListType or isinstance (e , UserList )
122
72
73
+ def global_cache_type_e_is_String (e ):
74
+ t = type (e )
75
+ return t is StringType or isinstance (e , UserString )
123
76
124
77
125
78
# Alternative that uses a myType() function to map the User* objects
@@ -131,20 +84,11 @@ def global_cache_type_e_is_String(e):
131
84
UserString : str ,
132
85
}
133
86
134
- if UnicodeType is not None :
135
- def myType (obj ):
136
- t = type (obj )
137
- if t is types .InstanceType :
138
- t = instanceTypeMap .get (obj .__class__ , t )
139
- elif t is unicode :
140
- t = str
141
- return t
142
- else :
143
- def myType (obj ):
144
- t = type (obj )
145
- if t is types .InstanceType :
146
- t = instanceTypeMap .get (obj .__class__ , t )
147
- return t
87
+ def myType (obj ):
88
+ t = type (obj )
89
+ if t is types .InstanceType :
90
+ t = instanceTypeMap .get (obj .__class__ , t )
91
+ return t
148
92
149
93
def myType_is_Dict (e ):
150
94
return myType (e ) is dict
0 commit comments