[Mesa-dev] [PATCH 07/26] python: Better sort dictionary keys/values

Ian Romanick idr at freedesktop.org
Fri Jul 6 17:17:06 UTC 2018


This patch is

Reviewed-by: Ian Romanick <ian.d.romanick at intel.com>

On 07/06/2018 03:17 AM, Mathieu Bridon wrote:
> In Python 2, dict.keys() and dict.values() both return a list, which can
> be sorted in two ways:
> 
> * l.sort() modifies the list in-place;
> * sorted(l) returns a new, sorted list;
> 
> In Python 3, dict.keys() and dict.values() do not return lists any more,
> but iterators. Iterators do not have a .sort() method.
> 
> This commit moves the build scripts to using sorted() on dict keys and
> values, which makes them compatible with both Python 2 and Python 3.
> 
> Signed-off-by: Mathieu Bridon <bochecha at daitauha.fr>
> ---
>  src/mapi/glapi/gen/glX_proto_send.py |  3 +--
>  src/mapi/glapi/gen/glX_proto_size.py |  6 ++----
>  src/mapi/glapi/gen/gl_XML.py         | 12 ++++--------
>  src/mapi/glapi/gen/gl_procs.py       |  3 +--
>  src/mapi/mapi_abi.py                 |  6 ++----
>  5 files changed, 10 insertions(+), 20 deletions(-)
> 
> diff --git a/src/mapi/glapi/gen/glX_proto_send.py b/src/mapi/glapi/gen/glX_proto_send.py
> index c389872044..fba2f0cc1e 100644
> --- a/src/mapi/glapi/gen/glX_proto_send.py
> +++ b/src/mapi/glapi/gen/glX_proto_send.py
> @@ -391,8 +391,7 @@ static const struct proc_pair
>     const char *name;
>     _glapi_proc proc;
>  } proc_pairs[%d] = {""" % len(procs))
> -        names = procs.keys()
> -        names.sort()
> +        names = sorted(procs.keys())
>          for i in xrange(len(names)):
>              comma = ',' if i < len(names) - 1 else ''
>              print('   { "%s", (_glapi_proc) gl%s }%s' % (names[i], procs[names[i]], comma))
> diff --git a/src/mapi/glapi/gen/glX_proto_size.py b/src/mapi/glapi/gen/glX_proto_size.py
> index 18a6f2e502..2a843c3e24 100644
> --- a/src/mapi/glapi/gen/glX_proto_size.py
> +++ b/src/mapi/glapi/gen/glX_proto_size.py
> @@ -208,8 +208,7 @@ class glx_enum_function(object):
>                  for enum_obj in self.enums[e]:
>                      list[ enum_obj.priority() ] = enum_obj.name
>  
> -                keys = list.keys()
> -                keys.sort()
> +                keys = sorted(list.keys())
>                  for k in keys:
>                      j = list[k]
>                      if first:
> @@ -275,8 +274,7 @@ class glx_server_enum_function(glx_enum_function):
>              o = f.offset_of( param_name )
>              foo[o] = param_name
>  
> -        keys = foo.keys()
> -        keys.sort()
> +        keys = sorted(foo.keys())
>          for o in keys:
>              p = f.parameters_by_name[ foo[o] ]
>  
> diff --git a/src/mapi/glapi/gen/gl_XML.py b/src/mapi/glapi/gen/gl_XML.py
> index 20057cf9c4..7bd5a1f4e4 100644
> --- a/src/mapi/glapi/gen/gl_XML.py
> +++ b/src/mapi/glapi/gen/gl_XML.py
> @@ -988,12 +988,10 @@ class gl_api(object):
>  
>          functions = []
>          for func_cat_type in range(0,4):
> -            keys = lists[func_cat_type].keys()
> -            keys.sort()
> +            keys = sorted(lists[func_cat_type].keys())
>  
>              for key in keys:
> -                names = lists[func_cat_type][key].keys()
> -                names.sort()
> +                names = sorted(lists[func_cat_type][key].keys())
>  
>                  for name in names:
>                      functions.append(lists[func_cat_type][key][name])
> @@ -1027,8 +1025,7 @@ class gl_api(object):
>  
>  
>      def enumIterateByName(self):
> -        keys = self.enums_by_name.keys()
> -        keys.sort()
> +        keys = sorted(self.enums_by_name.keys())
>  
>          list = []
>          for enum in keys:
> @@ -1047,8 +1044,7 @@ class gl_api(object):
>  
>          list = []
>          for cat_type in range(0,4):
> -            keys = self.categories[cat_type].keys()
> -            keys.sort()
> +            keys = sorted(self.categories[cat_type].keys())
>  
>              for key in keys:
>                  list.append(self.categories[cat_type][key])
> diff --git a/src/mapi/glapi/gen/gl_procs.py b/src/mapi/glapi/gen/gl_procs.py
> index 5718f42ab6..4bd3321610 100644
> --- a/src/mapi/glapi/gen/gl_procs.py
> +++ b/src/mapi/glapi/gen/gl_procs.py
> @@ -144,8 +144,7 @@ typedef struct {
>                  print('')
>                  print('/* OpenGL ES specific prototypes */')
>                  print('')
> -                keys = categories.keys()
> -                keys.sort()
> +                keys = sorted(categories.keys())
>                  for key in keys:
>                      print('/* category %s */' % key)
>                      print("\n".join(categories[key]))
> diff --git a/src/mapi/mapi_abi.py b/src/mapi/mapi_abi.py
> index e3f65547bf..826721479d 100644
> --- a/src/mapi/mapi_abi.py
> +++ b/src/mapi/mapi_abi.py
> @@ -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
>  
> @@ -250,8 +249,7 @@ def abi_parse(filename):
>              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
>  
> 



More information about the mesa-dev mailing list