From 6693d0929f11f7799b6b1185498c658566d9f356 Mon Sep 17 00:00:00 2001 From: Raul Tambre Date: Wed, 19 Feb 2020 20:36:45 +0000 Subject: [PATCH] gclient_eval, gclient_utils: Fix Python 3.9 removal of collections members Various collections module members were removed in Python 3.9 after being deprecated and moved to collections.abc in earlier Python 3 versions. For accessing these members alias collections_abc as: * collections.abc on Python 3 * collections on Python 2 Traceback (most recent call last): File "C:\Google\depot_tools\gclient.py", line 107, in import gclient_eval File "C:\Google\depot_tools\gclient_eval.py", line 11, in import gclient_utils File "C:\Google\depot_tools\gclient_utils.py", line 1201, in class FrozenDict(collections.Mapping): AttributeError: module 'collections' has no attribute 'Mapping' Traceback (most recent call last): File "C:\Google\depot_tools\gclient.py", line 107, in import gclient_eval File "C:\Google\depot_tools\gclient_eval.py", line 25, in class _NodeDict(collections.MutableMapping): AttributeError: module 'collections' has no attribute 'MutableMapping' Bug: 984182 Change-Id: I7a4417978b93e29397e63764e4570a598c075bc0 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/2043879 Auto-Submit: Raul Tambre Commit-Queue: Edward Lesmes Reviewed-by: Edward Lesmes --- gclient_eval.py | 8 +++++--- gclient_utils.py | 8 +++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/gclient_eval.py b/gclient_eval.py index 002ebb45d4..c41ff398d7 100644 --- a/gclient_eval.py +++ b/gclient_eval.py @@ -16,13 +16,15 @@ from third_party import six if six.PY2: # We use cStringIO.StringIO because it is equivalent to Py3's io.StringIO. from cStringIO import StringIO + import collections as collections_abc else: + from collections import abc as collections_abc from io import StringIO # pylint: disable=redefined-builtin basestring = str -class _NodeDict(collections.MutableMapping): +class _NodeDict(collections_abc.MutableMapping): """Dict-like type that also stores information on AST nodes and tokens.""" def __init__(self, data, tokens=None): self.data = collections.OrderedDict(data) @@ -421,7 +423,7 @@ def _StandardizeDeps(deps_dict, vars_dict): new_deps_dict = {} for dep_name, dep_info in deps_dict.items(): dep_name = dep_name.format(**vars_dict) - if not isinstance(dep_info, collections.Mapping): + if not isinstance(dep_info, collections_abc.Mapping): dep_info = {'url': dep_info} dep_info.setdefault('dep_type', 'git') new_deps_dict[dep_name] = dep_info @@ -864,7 +866,7 @@ def GetRevision(gclient_dict, dep_name): elif isinstance(dep, basestring): _, _, revision = dep.partition('@') return revision or None - elif isinstance(dep, collections.Mapping) and 'url' in dep: + elif isinstance(dep, collections_abc.Mapping) and 'url' in dep: _, _, revision = dep['url'].partition('@') return revision or None else: diff --git a/gclient_utils.py b/gclient_utils.py index 90cee87e7d..9f4de36ad1 100644 --- a/gclient_utils.py +++ b/gclient_utils.py @@ -28,9 +28,11 @@ import subprocess2 if sys.version_info.major == 2: from cStringIO import StringIO + import collections as collections_abc import Queue as queue import urlparse else: + from collections import abc as collections_abc from io import StringIO import queue import urllib.parse as urlparse @@ -1187,7 +1189,7 @@ def freeze(obj): Will raise TypeError if you pass an object which is not hashable. """ - if isinstance(obj, collections.Mapping): + if isinstance(obj, collections_abc.Mapping): return FrozenDict((freeze(k), freeze(v)) for k, v in obj.items()) elif isinstance(obj, (list, tuple)): return tuple(freeze(i) for i in obj) @@ -1198,7 +1200,7 @@ def freeze(obj): return obj -class FrozenDict(collections.Mapping): +class FrozenDict(collections_abc.Mapping): """An immutable OrderedDict. Modified From: http://stackoverflow.com/a/2704866 @@ -1212,7 +1214,7 @@ class FrozenDict(collections.Mapping): operator.xor, (hash(i) for i in enumerate(self._d.items())), 0) def __eq__(self, other): - if not isinstance(other, collections.Mapping): + if not isinstance(other, collections_abc.Mapping): return NotImplemented if self is other: return True