From ab32e93839f261fab9159bc8f174c954d5da5765 Mon Sep 17 00:00:00 2001 From: Lisa Roach Date: Wed, 27 Apr 2022 13:17:50 -0700 Subject: [PATCH] Get Buck working for 3.10 (#2696) Summary: Pull Request resolved: https://github.com/facebook/buck/pull/2696 On 3.10 some `collections` classes have been moved to `collections.abc` and it causes Buck 1 to crash. This diff fixes those instances. Really pex should be upgraded, but I made an attempt and it was very difficult due to our custom changes to pex. fbshipit-source-id: cadb6e533c94b87d38b75d4b9ffcb3d018ffe29a --- scripts/slice_trace.py | 1 - third-party/py/pathlib/test_pathlib.py | 7 ++++--- third-party/py/pex/README.facebook | 1 + third-party/py/pex/pex/base.py | 5 ++++- third-party/py/pex/pex/link.py | 7 ++++++- third-party/py/pex/pex/orderedset.py | 8 ++++++-- third-party/py/pywatchman/pywatchman/pybser.py | 14 +++++++++----- third-party/py/pywatchman/tests/tests.py | 1 - third-party/py/setuptools/README.facebook | 2 +- .../py/setuptools/pkg_resources/__init__.py | 2 +- 10 files changed, 32 insertions(+), 16 deletions(-) diff --git a/scripts/slice_trace.py b/scripts/slice_trace.py index 277f33c8ee2..0ab2d6c3fa7 100644 --- a/scripts/slice_trace.py +++ b/scripts/slice_trace.py @@ -16,7 +16,6 @@ import argparse import codecs -import collections import math import os diff --git a/third-party/py/pathlib/test_pathlib.py b/third-party/py/pathlib/test_pathlib.py index 7147e4ee5a3..5522fdcc34d 100755 --- a/third-party/py/pathlib/test_pathlib.py +++ b/third-party/py/pathlib/test_pathlib.py @@ -1,4 +1,3 @@ -import collections import io import os import errno @@ -19,8 +18,10 @@ raise ImportError("unittest2 is required for tests on pre-2.7") try: + import collections.abc as collections_abc from test import support except ImportError: + import collections as collections_abc # Fallback for PY3.2. from test import test_support as support TESTFN = support.TESTFN @@ -1395,7 +1396,7 @@ def _check(glob, expected): P = self.cls p = P(BASE) it = p.glob("fileA") - self.assertIsInstance(it, collections.Iterator) + self.assertIsInstance(it, collections_abc.Iterator) _check(it, ["fileA"]) _check(p.glob("fileB"), []) _check(p.glob("dir*/file*"), ["dirB/fileB", "dirC/fileC"]) @@ -1420,7 +1421,7 @@ def _check(glob, expected): P = self.cls p = P(BASE) it = p.rglob("fileA") - self.assertIsInstance(it, collections.Iterator) + self.assertIsInstance(it, collections_abc.Iterator) # XXX cannot test because of symlink loops in the test setup #_check(it, ["fileA"]) #_check(p.rglob("fileB"), ["dirB/fileB"]) diff --git a/third-party/py/pex/README.facebook b/third-party/py/pex/README.facebook index 81e45a42190..973953270f7 100644 --- a/third-party/py/pex/README.facebook +++ b/third-party/py/pex/README.facebook @@ -16,3 +16,4 @@ Local modifications: - Make pexes work when being invoked from a version of python without site.USER_SITE - Fix concurrency when two instances of zip-safe pex invoked D21508334 - Handle EACCES and EPERM in safe_copy + - Imported from collections.abc instead of collections to support Python 3.10 diff --git a/third-party/py/pex/pex/base.py b/third-party/py/pex/pex/base.py index 7d90443dd3c..2a3d775f9e0 100644 --- a/third-party/py/pex/pex/base.py +++ b/third-party/py/pex/pex/base.py @@ -3,7 +3,10 @@ from __future__ import absolute_import -from collections import Iterable +try: + from collections.abc import Iterable +except ImportError: # For PY3.2 + from collections import Iterable from pkg_resources import Requirement diff --git a/third-party/py/pex/pex/link.py b/third-party/py/pex/pex/link.py index 542f344e09a..5815adc94d2 100644 --- a/third-party/py/pex/pex/link.py +++ b/third-party/py/pex/pex/link.py @@ -5,12 +5,17 @@ import os import posixpath -from collections import Iterable from .compatibility import string as compatible_string from .compatibility import PY3 from .util import Memoizer + +try: + from collections.abc import Iterable +except ImportError: # For PY3.2 + from collections import Iterable + if PY3: import urllib.parse as urlparse else: diff --git a/third-party/py/pex/pex/orderedset.py b/third-party/py/pex/pex/orderedset.py index 3eb5cb45414..1a8db948504 100644 --- a/third-party/py/pex/pex/orderedset.py +++ b/third-party/py/pex/pex/orderedset.py @@ -8,10 +8,14 @@ # modifications # -import collections +try: + import collections.abc as collections_abc +except ImportError: # For PY3.2 + import collections as collections_abc -class OrderedSet(collections.MutableSet): + +class OrderedSet(collections_abc.MutableSet): KEY, PREV, NEXT = range(3) def __init__(self, iterable=None): diff --git a/third-party/py/pywatchman/pywatchman/pybser.py b/third-party/py/pywatchman/pywatchman/pybser.py index acd0b0d00a7..3c9d40f417c 100644 --- a/third-party/py/pywatchman/pywatchman/pybser.py +++ b/third-party/py/pywatchman/pywatchman/pybser.py @@ -32,7 +32,6 @@ # no unicode literals import binascii -import collections import ctypes import struct import sys @@ -41,6 +40,11 @@ compat, ) +try: + import collections.abc as collections_abc +except ImportError: + import collections as collections_abc # Fallback for PY3.2. + BSER_ARRAY = b'\x00' BSER_OBJECT = b'\x01' BSER_BYTESTRING = b'\x02' @@ -177,8 +181,8 @@ def append_recursive(self, val): self.ensure_size(needed) struct.pack_into(b'=cd', self.buf, self.wpos, BSER_REAL, val) self.wpos += needed - elif isinstance(val, collections.Mapping) and \ - isinstance(val, collections.Sized): + elif isinstance(val, collections_abc.Mapping) and \ + isinstance(val, collections_abc.Sized): val_len = len(val) size = _int_size(val_len) needed = 2 + size @@ -205,8 +209,8 @@ def append_recursive(self, val): for k, v in iteritems: self.append_string(k) self.append_recursive(v) - elif isinstance(val, collections.Iterable) and \ - isinstance(val, collections.Sized): + elif isinstance(val, collections_abc.Iterable) and \ + isinstance(val, collections_abc.Sized): val_len = len(val) size = _int_size(val_len) needed = 2 + size diff --git a/third-party/py/pywatchman/tests/tests.py b/third-party/py/pywatchman/tests/tests.py index 02d9a0b6396..7e77da43ede 100755 --- a/third-party/py/pywatchman/tests/tests.py +++ b/third-party/py/pywatchman/tests/tests.py @@ -6,7 +6,6 @@ # no unicode literals import binascii -import collections import inspect import unittest import os diff --git a/third-party/py/setuptools/README.facebook b/third-party/py/setuptools/README.facebook index b439d8530f4..3bce06f8020 100644 --- a/third-party/py/setuptools/README.facebook +++ b/third-party/py/setuptools/README.facebook @@ -1,4 +1,4 @@ Project URL: https://pypi.python.org/packages/source/s/setuptools/setuptools-18.5.tar.gz#md5=533c868f01169a3085177dffe5e768bb Version: 18.5 License: PSF or ZPL -Local modifications: kept only pkg_resources & _markerlib modules +Local modifications: kept only pkg_resources & _markerlib modules, made "import symbol" optional to support python 3.10 diff --git a/third-party/py/setuptools/pkg_resources/__init__.py b/third-party/py/setuptools/pkg_resources/__init__.py index d09e0b6f9af..9e49a925a40 100644 --- a/third-party/py/setuptools/pkg_resources/__init__.py +++ b/third-party/py/setuptools/pkg_resources/__init__.py @@ -29,7 +29,6 @@ import functools import pkgutil import token -import symbol import operator import platform import collections @@ -40,6 +39,7 @@ from pkgutil import get_importer try: + import symbol import _imp except ImportError: # Python 3.2 compatibility