python: Replace dict.has_key with 'key in dict'
authorAndreas Sandberg <andreas.sandberg@arm.com>
Fri, 25 Jan 2019 12:12:38 +0000 (12:12 +0000)
committerAndreas Sandberg <andreas.sandberg@arm.com>
Tue, 12 Feb 2019 17:36:12 +0000 (17:36 +0000)
Python 3 has removed dict.has_key in favour of 'key in dict'.

Change-Id: I9852a5f57d672bea815308eb647a0ce45624fad5
Signed-off-by: Andreas Sandberg <andreas.sandberg@arm.com>
Reviewed-on: https://gem5-review.googlesource.com/c/15987
Reviewed-by: Giacomo Travaglini <giacomo.travaglini@arm.com>
src/SConscript
src/arch/isa_parser.py
src/arch/x86/isa/macroop.isa
src/mem/slicc/ast/PeekStatementAST.py
src/mem/slicc/symbols/StateMachine.py
src/python/m5/SimObject.py
src/python/m5/params.py
src/python/m5/util/multidict.py
src/unittest/genini.py

index dcb08a3045936be59ff69e043c8992f4c062db5b..81b6cd3f1d65814e0b9514c945bcaff96f9e786b 100644 (file)
@@ -1338,7 +1338,7 @@ def identifyTarget(t):
     ext = t.split('.')[-1]
     if ext in target_types:
         return ext
-    if obj2target.has_key(ext):
+    if ext in obj2target:
         return obj2target[ext]
     match = re.search(r'/tests/([^/]+)/', t)
     if match and match.group(1) in target_types:
index 16004c0092d28d79c2b967d4801ab6dad3d44b31..48bc23fa37e062b4aa285e8018d804f8b6babfb2 100755 (executable)
@@ -142,7 +142,7 @@ class Template(object):
             del myDict['snippets']
 
             snippetLabels = [l for l in labelRE.findall(template)
-                             if d.snippets.has_key(l)]
+                             if l in d.snippets]
 
             snippets = dict([(s, self.parser.mungeSnippet(d.snippets[s]))
                              for s in snippetLabels])
index 33e559c11504b3141284943a191ad1090c86d7b1..7d729618aaedb5a5eff41e7936ae8ec904d96c7e 100644 (file)
@@ -333,7 +333,7 @@ let {{
     noModRMString = "env.setSeg(machInst);\n"
     def genMacroop(Name, env):
         blocks = OutputBlocks()
-        if not macroopDict.has_key(Name):
+        if not Name in macroopDict:
             raise Exception, "Unrecognized instruction: %s" % Name
         macroop = macroopDict[Name]
         if not macroop.declared:
index 00d26e908907ce0673591fb476b40f55122f8e0b..6cadb31306e9f5972566518f352d636cafef574b 100644 (file)
@@ -71,7 +71,7 @@ class PeekStatementAST(StatementAST):
     }
 ''')
 
-        if self.pairs.has_key("block_on"):
+        if "block_on" in self.pairs:
             address_field = self.pairs['block_on']
             code('''
     if (m_is_blocking &&
@@ -82,7 +82,7 @@ class PeekStatementAST(StatementAST):
     }
             ''')
 
-        if self.pairs.has_key("wake_up"):
+        if "wake_up" in self.pairs:
             address_field = self.pairs['wake_up']
             code('''
     if (m_waiting_buffers.count(in_msg_ptr->m_$address_field) > 0) {
index 330cc0369a3160e264ecf0f602c2378f69884600..03e624b117caf4fa26dd0e1a8aed325156eba589 100644 (file)
@@ -239,7 +239,7 @@ class $py_ident(RubyController):
             if param.rvalue is not None:
                 dflt_str = str(param.rvalue.inline()) + ', '
 
-            if python_class_map.has_key(param.type_ast.type.c_ident):
+            if param.type_ast.type.c_ident in python_class_map:
                 python_type = python_class_map[param.type_ast.type.c_ident]
                 code('${{param.ident}} = Param.${{python_type}}(${dflt_str}"")')
 
@@ -1109,7 +1109,7 @@ ${ident}_Controller::wakeup()
         for port in self.in_ports:
             code.indent()
             code('// ${ident}InPort $port')
-            if port.pairs.has_key("rank"):
+            if "rank" in port.pairs:
                 code('m_cur_in_port = ${{port.pairs["rank"]}};')
             else:
                 code('m_cur_in_port = 0;')
index 97cf6d09688939fe6eb9689b9e7659b405d178fc..f553fd664bf196c22a645b9ef209866211f6374c 100644 (file)
@@ -534,7 +534,7 @@ class MetaSimObject(type):
                 cls._new_port(key, val)
 
             # init-time-only keywords
-            elif cls.init_keywords.has_key(key):
+            elif key in cls.init_keywords:
                 cls._set_keyword(key, val, cls.init_keywords[key])
 
             # default: use normal path (ends up in __setattr__)
@@ -613,11 +613,11 @@ class MetaSimObject(type):
             type.__setattr__(cls, attr, value)
             return
 
-        if cls.keywords.has_key(attr):
+        if attr in cls.keywords:
             cls._set_keyword(attr, value, cls.keywords[attr])
             return
 
-        if cls._ports.has_key(attr):
+        if attr in cls._ports:
             cls._cls_get_port_ref(attr).connect(value)
             return
 
@@ -652,10 +652,10 @@ class MetaSimObject(type):
         if attr == 'cxx_namespaces':
             return cls.cxx_class_path[:-1]
 
-        if cls._values.has_key(attr):
+        if attr in cls._values:
             return cls._values[attr]
 
-        if cls._children.has_key(attr):
+        if attr in cls._children:
             return cls._children[attr]
 
         raise AttributeError(
@@ -1158,7 +1158,7 @@ class SimObject(object):
             # create a new dict and use that.
             memo_dict = {}
             kwargs['_memo'] = memo_dict
-        elif memo_dict.has_key(self):
+        elif self in memo_dict:
             # clone already done & memoized
             return memo_dict[self]
         return self.__class__(_ancestor = self, **kwargs)
@@ -1174,13 +1174,13 @@ class SimObject(object):
         return ref
 
     def __getattr__(self, attr):
-        if self._ports.has_key(attr):
+        if attr in self._ports:
             return self._get_port_ref(attr)
 
-        if self._values.has_key(attr):
+        if attr in self._values:
             return self._values[attr]
 
-        if self._children.has_key(attr):
+        if attr in self._children:
             return self._children[attr]
 
         # If the attribute exists on the C++ object, transparently
@@ -1206,7 +1206,7 @@ class SimObject(object):
             object.__setattr__(self, attr, value)
             return
 
-        if self._ports.has_key(attr):
+        if attr in self._ports:
             # set up port connection
             self._get_port_ref(attr).connect(value)
             return
@@ -1294,7 +1294,7 @@ class SimObject(object):
         if child.has_parent():
             warn("add_child('%s'): child '%s' already has parent", name,
                 child.get_name())
-        if self._children.has_key(name):
+        if name in self._children:
             # This code path had an undiscovered bug that would make it fail
             # at runtime. It had been here for a long time and was only
             # exposed by a buggy script. Changes here will probably not be
index 0a563b8f26292b3a9c28e3f118e11c23c3d74c33..09aaa5af7480e864b39db88845314bd6d766d8ba 100644 (file)
@@ -157,12 +157,12 @@ class ParamDesc(object):
             else:
                 raise TypeError('too many arguments')
 
-        if kwargs.has_key('desc'):
+        if 'desc' in kwargs:
             assert(not hasattr(self, 'desc'))
             self.desc = kwargs['desc']
             del kwargs['desc']
 
-        if kwargs.has_key('default'):
+        if 'default' in kwargs:
             assert(not hasattr(self, 'default'))
             self.default = kwargs['default']
             del kwargs['default']
@@ -1224,14 +1224,14 @@ class MetaEnum(MetaParamValue):
         return cls
 
     def __init__(cls, name, bases, init_dict):
-        if init_dict.has_key('map'):
+        if 'map' in init_dict:
             if not isinstance(cls.map, dict):
                 raise TypeError("Enum-derived class attribute 'map' " \
                       "must be of type dict")
             # build list of value strings from map
             cls.vals = cls.map.keys()
             cls.vals.sort()
-        elif init_dict.has_key('vals'):
+        elif 'vals' in init_dict:
             if not isinstance(cls.vals, list):
                 raise TypeError("Enum-derived class attribute 'vals' " \
                       "must be of type list")
@@ -1855,7 +1855,7 @@ class PortRef(object):
                   "cannot splice in new peers\n", self)
 
     def clone(self, simobj, memo):
-        if memo.has_key(self):
+        if self in memo:
             return memo[self]
         newRef = copy.copy(self)
         memo[self] = newRef
@@ -1978,7 +1978,7 @@ class VectorPortRef(object):
             self._get_next().connect(other)
 
     def clone(self, simobj, memo):
-        if memo.has_key(self):
+        if self in memo:
             return memo[self]
         newRef = copy.copy(self)
         memo[self] = newRef
index 28090a2519f307015f401c6f9a39f2eb97a7699c..58898a5d2b9ef870ce8a89f6d97cf2134c4d525e 100644 (file)
@@ -43,7 +43,7 @@ class multidict(object):
         return `dict(self.items())`
 
     def __contains__(self, key):
-        return self.local.has_key(key) or self.parent.has_key(key)
+        return key in self.local or key in self.parent
 
     def __delitem__(self, key):
         try:
index f10ad6874f31602c891170b5bd8830560f0d75ae..ea2763a340087a5c12a4d9aca6dfa7d68f920b58 100755 (executable)
@@ -70,7 +70,7 @@ for path in pathlist:
 for arg in args:
     m5execfile(arg, globals())
 
-if globals().has_key('root') and isinstance(root, Root):
+if 'root' in globals() and isinstance(root, Root):
     instantiate(root)
 else:
     print("Instantiation skipped: no root object found.")