python: Better check for keys in dicts
authorMathieu Bridon <bochecha@daitauha.fr>
Thu, 5 Jul 2018 13:17:35 +0000 (15:17 +0200)
committerDylan Baker <dylan@pnwbakers.com>
Tue, 24 Jul 2018 18:07:04 +0000 (11:07 -0700)
Python 3 lost the dict.has_key() method. Instead it requires using the
"in" operator.

This is also compatible with Python 2.

Signed-off-by: Mathieu Bridon <bochecha@daitauha.fr>
Reviewed-by: Eric Engestrom <eric.engestrom@intel.com>
Reviewed-by: Dylan Baker <dylan@pnwbakers.com>
Reviewed-by: Ian Romanick <ian.d.romanick@intel.com>
src/mapi/glapi/gen/glX_XML.py
src/mapi/glapi/gen/glX_proto_send.py
src/mapi/glapi/gen/glX_proto_size.py
src/mapi/glapi/gen/gl_XML.py
src/mapi/glapi/gen/gl_procs.py
src/mapi/mapi_abi.py
src/util/xmlpool/gen_xmlpool.py

index d09370a508fa475e4de77773d756eb8c5b522a2a..e10c8b0a097fe3130365a7802b7ad80fc0da81e0 100644 (file)
@@ -64,7 +64,7 @@ class glx_enum(gl_XML.gl_enum):
                 else:
                     mode = 1
 
-                if not self.functions.has_key(n):
+                if n not in self.functions:
                     self.functions[ n ] = [c, mode]
 
         return
index f199e9a0a162ce1ed65c129d616bb72808f65182..7ab1eb4a70d9ddacdb34cb44e694e8c28280b3b7 100644 (file)
@@ -842,7 +842,7 @@ generic_%u_byte( GLint rop, const void * ptr )
 
 
     def printPixelFunction(self, f):
-        if self.pixel_stubs.has_key( f.name ):
+        if f.name in self.pixel_stubs:
             # Normally gl_function::get_parameter_string could be
             # used.  However, this call needs to have the missing
             # dimensions (e.g., a fake height value for
index 284ee70e6184fff31e5ab5aaa3ff9488727d4a09..18a6f2e50241659ce2b0d0cff1d30beb418a1a8e 100644 (file)
@@ -71,7 +71,7 @@ class glx_enum_function(object):
         for enum_name in enum_dict:
             e = enum_dict[ enum_name ]
 
-            if e.functions.has_key( match_name ):
+            if match_name in e.functions:
                 [count, mode] = e.functions[ match_name ]
 
                 if mode_set and mode != self.mode:
@@ -79,11 +79,11 @@ class glx_enum_function(object):
 
                 self.mode = mode
 
-                if self.enums.has_key( e.value ):
+                if e.value in self.enums:
                     if e.name not in self.enums[ e.value ]:
                         self.enums[ e.value ].append( e )
                 else:
-                    if not self.count.has_key( count ):
+                    if count not in self.count:
                         self.count[ count ] = []
 
                     self.enums[ e.value ] = [ e ]
@@ -131,7 +131,7 @@ class glx_enum_function(object):
         for a in self.enums:
             count += 1
 
-        if self.count.has_key(-1):
+        if -1 in self.count:
             return 0
 
         # Determine if there is some mask M, such that M = (2^N) - 1,
@@ -356,7 +356,7 @@ class PrintGlxSizeStubs_c(PrintGlxSizeStubs_common):
 
             if (ef.is_set() and self.emit_set) or (not ef.is_set() and self.emit_get):
                 sig = ef.signature()
-                if enum_sigs.has_key( sig ):
+                if sig in enum_sigs:
                     aliases.append( [func.name, enum_sigs[ sig ]] )
                 else:
                     enum_sigs[ sig ] = func.name
@@ -477,10 +477,10 @@ class PrintGlxReqSize_c(PrintGlxReqSize_common):
 
             sig = ef.signature()
 
-            if not enum_functions.has_key(func.name):
+            if func.name not in enum_functions:
                 enum_functions[ func.name ] = sig
 
-            if not enum_sigs.has_key( sig ):
+            if sig not in enum_sigs:
                 enum_sigs[ sig ] = ef
 
 
@@ -496,7 +496,7 @@ class PrintGlxReqSize_c(PrintGlxReqSize_common):
             if func.server_handcode: continue
             if not func.has_variable_size_request(): continue
 
-            if enum_functions.has_key(func.name):
+            if func.name in enum_functions:
                 sig = enum_functions[func.name]
                 ef = enum_sigs[ sig ]
 
@@ -621,7 +621,7 @@ class PrintGlxReqSize_c(PrintGlxReqSize_common):
         # already be emitted, don't emit this function.  Instead, add
         # it to the list of function aliases.
 
-        if self.counter_sigs.has_key(sig):
+        if sig in self.counter_sigs:
             n = self.counter_sigs[sig];
             alias = [f.name, n]
         else:
index bfeacf13863765c5f3aefee521f13d19b01236d9..bea6801db3c60c85c6727e46f1b0ea145c2f2b97 100644 (file)
@@ -943,7 +943,7 @@ class gl_api(object):
                 temp_name = child.get( "name" )
                 self.category_dict[ temp_name ] = [cat_name, cat_number]
 
-                if self.functions_by_name.has_key( func_name ):
+                if func_name in self.functions_by_name:
                     func = self.functions_by_name[ func_name ]
                     func.process_element( child )
                 else:
@@ -980,7 +980,7 @@ class gl_api(object):
             if (cat == None) or (cat == cat_name):
                 [func_cat_type, key] = classify_category(cat_name, cat_number)
 
-                if not lists[func_cat_type].has_key(key):
+                if key not in lists[func_cat_type]:
                     lists[func_cat_type][key] = {}
 
                 lists[func_cat_type][key][func.name] = func
@@ -1057,7 +1057,7 @@ class gl_api(object):
 
 
     def get_category_for_name( self, name ):
-        if self.category_dict.has_key(name):
+        if name in self.category_dict:
             return self.category_dict[name]
         else:
             return ["<unknown category>", None]
index a7bc909ce2824f588a0934129d92fc2b98d7fbe6..5718f42ab61fd12cbc3ad2feb4fc78c316145867 100644 (file)
@@ -135,7 +135,7 @@ typedef struct {
                 for n in func.entry_points:
                     cat, num = api.get_category_for_name(n)
                     if (cat.startswith("es") or cat.startswith("GL_OES")):
-                        if not categories.has_key(cat):
+                        if cat not in categories:
                             categories[cat] = []
                         proto = 'GLAPI %s GLAPIENTRY %s(%s);' \
                                         % (func.return_type, "gl" + n, func.get_parameter_string(n))
index 0a49c06ff2c6b65f81785de8f0e055016db16def..826721479d546ddd7ced71ac7705e5fb6058b4b8 100644 (file)
@@ -168,7 +168,7 @@ def abi_parse_xml(xml):
             else:
                 attrs['handcode'] = None
 
-            if entry_dict.has_key(name):
+            if name in entry_dict:
                 raise Exception('%s is duplicated' % (name))
 
             cols = []
@@ -180,8 +180,7 @@ def abi_parse_xml(xml):
             ent = ABIEntry(cols, attrs, func)
             entry_dict[ent.name] = ent
 
-    entries = entry_dict.values()
-    entries.sort()
+    entries = sorted(entry_dict.values())
 
     return entries
 
@@ -246,12 +245,11 @@ def abi_parse(filename):
             raise Exception('invalid slot in %s' % (line))
 
         ent = ABIEntry(cols, attrs)
-        if entry_dict.has_key(ent.name):
+        if ent.name in entry_dict:
             raise Exception('%s is duplicated' % (ent.name))
         entry_dict[ent.name] = ent
 
-    entries = entry_dict.values()
-    entries.sort()
+    entries = sorted(entry_dict.values())
 
     return entries
 
index 8336e0f952356586fcf9f92018d51b41ef6b3cb0..886c1854f3a8c4793a7e173fd632a71fa17b6343 100644 (file)
@@ -42,7 +42,7 @@ def escapeCString (s):
                 # open quote
                 q = u'\u201d'
             r = r + q
-        elif escapeSeqs.has_key(s[i]):
+        elif s[i] in escapeSeqs:
             r = r + escapeSeqs[s[i]]
         else:
             r = r + s[i]
@@ -90,7 +90,7 @@ def expandCString (s):
                 escape = False
                 r = r + chr(num)
         else:
-            if escapeSeqs.has_key(s[i]):
+            if s[i] in escapeSeqs:
                 r = r + escapeSeqs[s[i]]
                 escape = False
             elif s[i] >= '0' and s[i] <= '7':