Skip to content

Commit 7040ecc

Browse files
authored
Merge pull request SCons#4540 from mwichmann/bench-update
Forward port benchmark for type checking
2 parents 13b75d0 + 8b14980 commit 7040ecc

File tree

1 file changed

+27
-83
lines changed

1 file changed

+27
-83
lines changed

bench/is_types.py

Lines changed: 27 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -5,93 +5,57 @@
55
# src/engine/SCons/Util.py.
66

77
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+
1710
DictType = dict
1811
ListType = list
1912
StringType = str
20-
try: unicode
21-
except NameError:
22-
UnicodeType = None
23-
else:
24-
UnicodeType = unicode
2513

2614

2715
# The original implementations, pretty straightforward checks for the
2816
# type of the object and whether it's an instance of the corresponding
2917
# User* type.
3018

3119
def original_is_Dict(e):
32-
return isinstance(e, (dict,UserDict))
20+
return isinstance(e, (dict, UserDict))
3321

3422
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))
4324

25+
def original_is_String(e):
26+
return isinstance(e, (str, UserString))
4427

4528

4629
# New candidates that explicitly check for whether the object is an
4730
# InstanceType before calling isinstance() on the corresponding User*
4831
# type.
32+
# InstanceType was only for old-style classes, so absent in Python 3
33+
# this this is no different than the previous
4934

5035
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))
5337

5438
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))
6740

41+
def checkInstanceType_is_String(e):
42+
return isinstance(e, (str, UserString))
6843

6944

7045
# Improved candidates that cache the type(e) result in a variable
7146
# before doing any checks.
7247

7348
def cache_type_e_is_Dict(e):
7449
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)
7751

7852
def cache_type_e_is_List(e):
7953
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)
9455

56+
def cache_type_e_is_String(e):
57+
t = type(e)
58+
return t is str or isinstance(e, UserString)
9559

9660

9761
# Improved candidates that cache the type(e) result in a variable
@@ -100,26 +64,15 @@ def cache_type_e_is_String(e):
10064

10165
def global_cache_type_e_is_Dict(e):
10266
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)
10568

10669
def global_cache_type_e_is_List(e):
10770
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)
12272

73+
def global_cache_type_e_is_String(e):
74+
t = type(e)
75+
return t is StringType or isinstance(e, UserString)
12376

12477

12578
# Alternative that uses a myType() function to map the User* objects
@@ -131,20 +84,11 @@ def global_cache_type_e_is_String(e):
13184
UserString : str,
13285
}
13386

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
14892

14993
def myType_is_Dict(e):
15094
return myType(e) is dict

0 commit comments

Comments
 (0)