[Mesa-dev] [PATCH 08/26] python: Better use iterators

Eric Engestrom eric.engestrom at intel.com
Thu Jul 5 14:37:46 UTC 2018


On Thursday, 2018-07-05 15:17:39 +0200, Mathieu Bridon wrote:
> In Python 2, iterators had a .next() method.
> 
> In Python 3, instead they have a .__next__() method, which is
> automatically called by the next() builtin.
> 
> In addition, it is better to use the iter() builtin to create an
> iterator, rather than calling its __iter__() method.

Not all that familiar with that stuff, so Cc'ing Dylan who I think has
dealt with __next__() et al in the past.

> 
> These were also introduced in Python 2.6, so using it makes the script
> compatible with Python 2 and 3.
> 
> Signed-off-by: Mathieu Bridon <bochecha at daitauha.fr>
> ---
>  src/compiler/glsl/ir_expression_operation.py |  4 +++-
>  src/compiler/nir/nir_algebraic.py            |  4 ++--
>  src/mapi/glapi/gen/glX_XML.py                | 17 +++++++++--------
>  src/mapi/glapi/gen/gl_XML.py                 | 12 ++++++------
>  4 files changed, 20 insertions(+), 17 deletions(-)
> 
> diff --git a/src/compiler/glsl/ir_expression_operation.py b/src/compiler/glsl/ir_expression_operation.py
> index d8542925a0..b3dac3da3f 100644
> --- a/src/compiler/glsl/ir_expression_operation.py
> +++ b/src/compiler/glsl/ir_expression_operation.py
> @@ -62,7 +62,7 @@ class type_signature_iter(object):
>     def __iter__(self):
>        return self
>  
> -   def next(self):
> +   def __next__(self):
>        if self.i < len(self.source_types):
>           i = self.i
>           self.i += 1
> @@ -76,6 +76,8 @@ class type_signature_iter(object):
>        else:
>           raise StopIteration()
>  
> +   next = __next__
> +
>  
>  uint_type = type("unsigned", "u", "GLSL_TYPE_UINT")
>  int_type = type("int", "i", "GLSL_TYPE_INT")
> diff --git a/src/compiler/nir/nir_algebraic.py b/src/compiler/nir/nir_algebraic.py
> index 8c0b530f69..fda72d3c69 100644
> --- a/src/compiler/nir/nir_algebraic.py
> +++ b/src/compiler/nir/nir_algebraic.py
> @@ -56,7 +56,7 @@ class VarSet(object):
>     def __getitem__(self, name):
>        if name not in self.names:
>           assert not self.immutable, "Unknown replacement variable: " + name
> -         self.names[name] = self.ids.next()
> +         self.names[name] = next(self.ids)
>  
>        return self.names[name]
>  
> @@ -468,7 +468,7 @@ condition_list = ['true']
>  
>  class SearchAndReplace(object):
>     def __init__(self, transform):
> -      self.id = _optimization_ids.next()
> +      self.id = next(_optimization_ids)
>  
>        search = transform[0]
>        replace = transform[1]
> diff --git a/src/mapi/glapi/gen/glX_XML.py b/src/mapi/glapi/gen/glX_XML.py
> index bbcecd6023..4ec8cd766f 100644
> --- a/src/mapi/glapi/gen/glX_XML.py
> +++ b/src/mapi/glapi/gen/glX_XML.py
> @@ -296,7 +296,7 @@ class glx_function(gl_XML.gl_function):
>          parameters.extend( temp[1] )
>          if include_variable_parameters:
>              parameters.extend( temp[2] )
> -        return parameters.__iter__()
> +        return iter(parameters)
>  
>  
>      def parameterIterateCounters(self):
> @@ -304,7 +304,7 @@ class glx_function(gl_XML.gl_function):
>          for name in self.counter_list:
>              temp.append( self.parameters_by_name[ name ] )
>  
> -        return temp.__iter__()
> +        return iter(temp)
>  
>  
>      def parameterIterateOutputs(self):
> @@ -547,13 +547,14 @@ class glx_function_iterator(object):
>          return self
>  
>  
> -    def next(self):
> -        f = self.iterator.next()
> +    def __next__(self):
> +        while True:
> +            f = next(self.iterator)
>  
> -        if f.client_supported_for_indirect():
> -            return f
> -        else:
> -            return self.next()
> +            if f.client_supported_for_indirect():
> +                return f
> +
> +    next = __next__
>  
>  
>  class glx_api(gl_XML.gl_api):
> diff --git a/src/mapi/glapi/gen/gl_XML.py b/src/mapi/glapi/gen/gl_XML.py
> index 3a4f59221e..bfbb1ec6e0 100644
> --- a/src/mapi/glapi/gen/gl_XML.py
> +++ b/src/mapi/glapi/gen/gl_XML.py
> @@ -782,9 +782,9 @@ class gl_function( gl_item ):
>  
>      def parameterIterator(self, name = None):
>          if name is not None:
> -            return self.entry_point_parameters[name].__iter__();
> +            return iter(self.entry_point_parameters[name]);
>          else:
> -            return self.parameters.__iter__();
> +            return iter(self.parameters);
>  
>  
>      def get_parameter_string(self, entrypoint = None):
> @@ -996,7 +996,7 @@ class gl_api(object):
>                  for name in names:
>                      functions.append(lists[func_cat_type][key][name])
>  
> -        return functions.__iter__()
> +        return iter(functions)
>  
>  
>      def functionIterateByOffset(self):
> @@ -1017,7 +1017,7 @@ class gl_api(object):
>              if temp[i]:
>                  list.append(temp[i])
>  
> -        return list.__iter__();
> +        return iter(list);
>  
>  
>      def functionIterateAll(self):
> @@ -1031,7 +1031,7 @@ class gl_api(object):
>          for enum in keys:
>              list.append( self.enums_by_name[ enum ] )
>  
> -        return list.__iter__()
> +        return iter(list)
>  
>  
>      def categoryIterate(self):
> @@ -1049,7 +1049,7 @@ class gl_api(object):
>              for key in keys:
>                  list.append(self.categories[cat_type][key])
>  
> -        return list.__iter__()
> +        return iter(list)
>  
>  
>      def get_category_for_name( self, name ):
> -- 
> 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