[Mesa-dev] [PATCH 06/26] python: Better iterate over dictionaries

Eric Engestrom eric.engestrom at intel.com
Thu Jul 5 14:06:01 UTC 2018


On Thursday, 2018-07-05 15:17:37 +0200, Mathieu Bridon wrote:
> In Python 2, dictionaries have 2 sets of methods to iterate over their
> keys and values: keys()/values()/items() and iterkeys()/itervalues()/iteritems().
> 
> The former return lists while the latter return iterators.
> 
> Python 3 dropped the method which return lists, and renamed the methods
> returning iterators to keys()/values()/items().
> 
> Using those names makes the scripts compatible with both Python 2 and 3.
> 
> Signed-off-by: Mathieu Bridon <bochecha at daitauha.fr>

Reviewed-by: Eric Engestrom <eric.engestrom at intel.com>

> ---
>  src/amd/vulkan/radv_entrypoints_gen.py       |  2 +-
>  src/compiler/nir/nir_algebraic.py            |  2 +-
>  src/compiler/nir/nir_builder_opcodes_h.py    |  4 ++--
>  src/compiler/nir/nir_constant_expressions.py |  4 ++--
>  src/compiler/nir/nir_intrinsics_c.py         |  2 +-
>  src/compiler/nir/nir_opcodes_c.py            |  2 +-
>  src/compiler/nir/nir_opcodes_h.py            |  2 +-
>  src/intel/genxml/gen_bits_header.py          | 10 +++++-----
>  src/intel/vulkan/anv_entrypoints_gen.py      |  2 +-
>  src/mapi/glapi/gen/gl_XML.py                 | 12 ++++++------
>  src/mapi/glapi/gen/gl_gentable.py            |  4 ++--
>  src/mesa/drivers/dri/i965/brw_oa.py          |  4 ++--
>  12 files changed, 25 insertions(+), 25 deletions(-)
> 
> diff --git a/src/amd/vulkan/radv_entrypoints_gen.py b/src/amd/vulkan/radv_entrypoints_gen.py
> index bef0c447f6..9c4dfd02a0 100644
> --- a/src/amd/vulkan/radv_entrypoints_gen.py
> +++ b/src/amd/vulkan/radv_entrypoints_gen.py
> @@ -433,7 +433,7 @@ def get_entrypoints(doc, entrypoints_to_defines, start_index):
>              e_clone.name = e.name
>              entrypoints[e.name] = e_clone
>  
> -    return [e for e in entrypoints.itervalues() if e.enabled]
> +    return [e for e in entrypoints.values() if e.enabled]
>  
>  
>  def get_entrypoints_defines(doc):
> diff --git a/src/compiler/nir/nir_algebraic.py b/src/compiler/nir/nir_algebraic.py
> index 847c59dbd8..8c0b530f69 100644
> --- a/src/compiler/nir/nir_algebraic.py
> +++ b/src/compiler/nir/nir_algebraic.py
> @@ -512,7 +512,7 @@ struct transform {
>  
>  #endif
>  
> -% for (opcode, xform_list) in xform_dict.iteritems():
> +% for (opcode, xform_list) in xform_dict.items():
>  % for xform in xform_list:
>     ${xform.search.render()}
>     ${xform.replace.render()}
> diff --git a/src/compiler/nir/nir_builder_opcodes_h.py b/src/compiler/nir/nir_builder_opcodes_h.py
> index 72cf5b4549..e600093e9f 100644
> --- a/src/compiler/nir/nir_builder_opcodes_h.py
> +++ b/src/compiler/nir/nir_builder_opcodes_h.py
> @@ -34,7 +34,7 @@ def src_list(num_srcs):
>     return ', '.join('src' + str(i) if i < num_srcs else 'NULL' for i in range(4))
>  %>
>  
> -% for name, opcode in sorted(opcodes.iteritems()):
> +% for name, opcode in sorted(opcodes.items()):
>  static inline nir_ssa_def *
>  nir_${name}(nir_builder *build, ${src_decl_list(opcode.num_inputs)})
>  {
> @@ -55,7 +55,7 @@ nir_load_system_value(nir_builder *build, nir_intrinsic_op op, int index)
>     return &load->dest.ssa;
>  }
>  
> -% for name, opcode in filter(lambda v: v[1].sysval, sorted(INTR_OPCODES.iteritems())):
> +% for name, opcode in filter(lambda v: v[1].sysval, sorted(INTR_OPCODES.items())):
>  static inline nir_ssa_def *
>  nir_${name}(nir_builder *build)
>  {
> diff --git a/src/compiler/nir/nir_constant_expressions.py b/src/compiler/nir/nir_constant_expressions.py
> index 35dffe70ce..118af9f781 100644
> --- a/src/compiler/nir/nir_constant_expressions.py
> +++ b/src/compiler/nir/nir_constant_expressions.py
> @@ -387,7 +387,7 @@ struct bool32_vec {
>     % endif
>  </%def>
>  
> -% for name, op in sorted(opcodes.iteritems()):
> +% for name, op in sorted(opcodes.items()):
>  static nir_const_value
>  evaluate_${name}(MAYBE_UNUSED unsigned num_components,
>                   ${"UNUSED" if op_bit_sizes(op) is None else ""} unsigned bit_size,
> @@ -420,7 +420,7 @@ nir_eval_const_opcode(nir_op op, unsigned num_components,
>                        unsigned bit_width, nir_const_value *src)
>  {
>     switch (op) {
> -% for name in sorted(opcodes.iterkeys()):
> +% for name in sorted(opcodes.keys()):
>     case nir_op_${name}:
>        return evaluate_${name}(num_components, bit_width, src);
>  % endfor
> diff --git a/src/compiler/nir/nir_intrinsics_c.py b/src/compiler/nir/nir_intrinsics_c.py
> index 9604fcdf62..98af67c38a 100644
> --- a/src/compiler/nir/nir_intrinsics_c.py
> +++ b/src/compiler/nir/nir_intrinsics_c.py
> @@ -25,7 +25,7 @@ template = """\
>  #include "nir.h"
>  
>  const nir_intrinsic_info nir_intrinsic_infos[nir_num_intrinsics] = {
> -% for name, opcode in sorted(INTR_OPCODES.iteritems()):
> +% for name, opcode in sorted(INTR_OPCODES.items()):
>  {
>     .name = "${name}",
>     .num_srcs = ${opcode.num_srcs},
> diff --git a/src/compiler/nir/nir_opcodes_c.py b/src/compiler/nir/nir_opcodes_c.py
> index 108e144b5f..4603cd3d74 100644
> --- a/src/compiler/nir/nir_opcodes_c.py
> +++ b/src/compiler/nir/nir_opcodes_c.py
> @@ -116,7 +116,7 @@ nir_type_conversion_op(nir_alu_type src, nir_alu_type dst, nir_rounding_mode rnd
>  }
>  
>  const nir_op_info nir_op_infos[nir_num_opcodes] = {
> -% for name, opcode in sorted(opcodes.iteritems()):
> +% for name, opcode in sorted(opcodes.items()):
>  {
>     .name = "${name}",
>     .num_inputs = ${opcode.num_inputs},
> diff --git a/src/compiler/nir/nir_opcodes_h.py b/src/compiler/nir/nir_opcodes_h.py
> index 8ad17c84d4..6b4e2fe052 100644
> --- a/src/compiler/nir/nir_opcodes_h.py
> +++ b/src/compiler/nir/nir_opcodes_h.py
> @@ -29,7 +29,7 @@ template = """\
>  #ifndef _NIR_OPCODES_
>  #define _NIR_OPCODES_
>  
> -<% opcode_names = sorted(opcodes.iterkeys()) %>
> +<% opcode_names = sorted(opcodes.keys()) %>
>  
>  typedef enum {
>  % for name in opcode_names:
> diff --git a/src/intel/genxml/gen_bits_header.py b/src/intel/genxml/gen_bits_header.py
> index faba79dcd3..e31e9ff103 100644
> --- a/src/intel/genxml/gen_bits_header.py
> +++ b/src/intel/genxml/gen_bits_header.py
> @@ -108,13 +108,13 @@ ${item.token_name}_${prop}(const struct gen_device_info *devinfo)
>  #ifdef __cplusplus
>  extern "C" {
>  #endif
> -% for _, container in sorted(containers.iteritems(), key=itemgetter(0)):
> +% for _, container in sorted(containers.items(), key=itemgetter(0)):
>  
>  /* ${container.name} */
>  
>  ${emit_per_gen_prop_func(container, 'length')}
>  
> -% for _, field in sorted(container.fields.iteritems(), key=itemgetter(0)):
> +% for _, field in sorted(container.fields.items(), key=itemgetter(0)):
>  
>  /* ${container.name}::${field.name} */
>  
> @@ -220,7 +220,7 @@ class Container(object):
>  
>      def iter_prop(self, prop):
>          if prop == 'length':
> -            return self.length_by_gen.iteritems()
> +            return self.length_by_gen.items()
>          else:
>              raise ValueError('Invalid property: "{0}"'.format(prop))
>  
> @@ -253,9 +253,9 @@ class Field(object):
>  
>      def iter_prop(self, prop):
>          if prop == 'bits':
> -            return self.bits_by_gen.iteritems()
> +            return self.bits_by_gen.items()
>          elif prop == 'start':
> -            return self.start_by_gen.iteritems()
> +            return self.start_by_gen.items()
>          else:
>              raise ValueError('Invalid property: "{0}"'.format(prop))
>  
> diff --git a/src/intel/vulkan/anv_entrypoints_gen.py b/src/intel/vulkan/anv_entrypoints_gen.py
> index db35069850..8a37336496 100644
> --- a/src/intel/vulkan/anv_entrypoints_gen.py
> +++ b/src/intel/vulkan/anv_entrypoints_gen.py
> @@ -504,7 +504,7 @@ def get_entrypoints(doc, entrypoints_to_defines, start_index):
>              assert e.core_version is None
>              e.extensions.append(ext)
>  
> -    return [e for e in entrypoints.itervalues() if e.enabled]
> +    return [e for e in entrypoints.values() if e.enabled]
>  
>  
>  def get_entrypoints_defines(doc):
> diff --git a/src/mapi/glapi/gen/gl_XML.py b/src/mapi/glapi/gen/gl_XML.py
> index 7cb276ec85..fcf8e62d3f 100644
> --- a/src/mapi/glapi/gen/gl_XML.py
> +++ b/src/mapi/glapi/gen/gl_XML.py
> @@ -881,7 +881,7 @@ class gl_api(object):
>      def filter_functions(self, entry_point_list):
>          """Filter out entry points not in entry_point_list."""
>          functions_by_name = {}
> -        for func in self.functions_by_name.itervalues():
> +        for func in self.functions_by_name.values():
>              entry_points = [ent for ent in func.entry_points if ent in entry_point_list]
>              if entry_points:
>                  func.filter_entry_points(entry_points)
> @@ -894,7 +894,7 @@ class gl_api(object):
>          optionally, not in the given version of the given API).
>          """
>          functions_by_name = {}
> -        for func in self.functions_by_name.itervalues():
> +        for func in self.functions_by_name.values():
>              entry_points = func.entry_points_for_api_version(api, version)
>              if entry_points:
>                  func.filter_entry_points(entry_points)
> @@ -1003,13 +1003,13 @@ class gl_api(object):
>  
>      def functionIterateByOffset(self):
>          max_offset = -1
> -        for func in self.functions_by_name.itervalues():
> +        for func in self.functions_by_name.values():
>              if func.offset > max_offset:
>                  max_offset = func.offset
>  
>  
>          temp = [None for i in range(0, max_offset + 1)]
> -        for func in self.functions_by_name.itervalues():
> +        for func in self.functions_by_name.values():
>              if func.offset != -1:
>                  temp[ func.offset ] = func
>  
> @@ -1023,7 +1023,7 @@ class gl_api(object):
>  
>  
>      def functionIterateAll(self):
> -        return self.functions_by_name.itervalues()
> +        return self.functions_by_name.values()
>  
>  
>      def enumIterateByName(self):
> @@ -1064,7 +1064,7 @@ class gl_api(object):
>  
>  
>      def typeIterate(self):
> -        return self.types_by_name.itervalues()
> +        return self.types_by_name.values()
>  
>  
>      def find_type( self, type_name ):
> diff --git a/src/mapi/glapi/gen/gl_gentable.py b/src/mapi/glapi/gen/gl_gentable.py
> index 73fa6c6daa..49206b1167 100644
> --- a/src/mapi/glapi/gen/gl_gentable.py
> +++ b/src/mapi/glapi/gen/gl_gentable.py
> @@ -202,13 +202,13 @@ class PrintCode(gl_XML.gl_print_base):
>  
>          # Determine how many functions have a defined offset.
>          func_count = 0
> -        for f in api.functions_by_name.itervalues():
> +        for f in api.functions_by_name.values():
>              if f.offset != -1:
>                  func_count += 1
>  
>          # Build the mapping from offset to function name.
>          funcnames = [None] * func_count
> -        for f in api.functions_by_name.itervalues():
> +        for f in api.functions_by_name.values():
>              if f.offset != -1:
>                  if not (funcnames[f.offset] is None):
>                      raise Exception("Function table has more than one function with same offset (offset %d, func %s)" % (f.offset, f.name))
> diff --git a/src/mesa/drivers/dri/i965/brw_oa.py b/src/mesa/drivers/dri/i965/brw_oa.py
> index 4c70f253d7..75382558e6 100644
> --- a/src/mesa/drivers/dri/i965/brw_oa.py
> +++ b/src/mesa/drivers/dri/i965/brw_oa.py
> @@ -653,7 +653,7 @@ def main():
>  
>              c("\n")
>              register_lengths = compute_register_lengths(set);
> -            for reg_type, reg_length in register_lengths.iteritems():
> +            for reg_type, reg_length in register_lengths.items():
>                  c("static struct brw_perf_query_register_prog {0}_{1}_{2}[{3}];".format(gen.chipset,
>                                                                                          set.underscore_name,
>                                                                                          reg_type, reg_length))
> @@ -692,7 +692,7 @@ def main():
>                      .c_offset = 46,
>                  """))
>  
> -            for reg_type, reg_length in register_lengths.iteritems():
> +            for reg_type, reg_length in register_lengths.items():
>                  c(".{0} = {1}_{2}_{3},".format(reg_type, gen.chipset, set.underscore_name, reg_type))
>                  c(".n_{0} = 0, /* Determined at runtime */".format(reg_type))
>  
> -- 
> 2.17.1
> 
> _______________________________________________
> mesa-dev mailing list
> mesa-dev at lists.freedesktop.org
> https://lists.freedesktop.org/mailman/listinfo/mesa-dev


More information about the mesa-dev mailing list