diff --git a/build/fbcode_builder/CMake/fb_py_test_main.py b/build/fbcode_builder/CMake/fb_py_test_main.py index 0ee1e8cc6..2a9b5b4b9 100644 --- a/build/fbcode_builder/CMake/fb_py_test_main.py +++ b/build/fbcode_builder/CMake/fb_py_test_main.py @@ -22,11 +22,8 @@ import time import traceback import unittest import warnings +from importlib.machinery import PathFinder -# Hide warning about importing "imp"; remove once python2 is gone. -with warnings.catch_warnings(): - warnings.filterwarnings("ignore", category=DeprecationWarning) - import imp try: from StringIO import StringIO @@ -82,7 +79,7 @@ class PathMatcher(object): return not self.omit(path) -class DebugWipeFinder(object): +class DebugWipeFinder(PathFinder): """ PEP 302 finder that uses a DebugWipeLoader for all files which do not need coverage @@ -91,28 +88,15 @@ class DebugWipeFinder(object): def __init__(self, matcher): self.matcher = matcher - def find_module(self, fullname, path=None): - _, _, basename = fullname.rpartition(".") - try: - fd, pypath, (_, _, kind) = imp.find_module(basename, path) - except Exception: - # Finding without hooks using the imp module failed. One reason - # could be that there is a zip file on sys.path. The imp module - # does not support loading from there. Leave finding this module to - # the others finders in sys.meta_path. + def find_spec(self, fullname, path=None, target=None): + spec = super().find_spec(fullname, path=path, target=target) + if spec is None or spec.origin is None: return None - - if hasattr(fd, "close"): - fd.close() - if kind != imp.PY_SOURCE: + if not spec.origin.endswith(".py"): return None - if self.matcher.include(pypath): + if self.matcher.include(spec.origin): return None - """ - This is defined to match CPython's PyVarObject struct - """ - class PyVarObject(ctypes.Structure): _fields_ = [ ("ob_refcnt", ctypes.c_long), @@ -126,7 +110,7 @@ class DebugWipeFinder(object): """ def get_code(self, fullname): - code = super(DebugWipeLoader, self).get_code(fullname) + code = super().get_code(fullname) if code: # Ideally we'd do # code.co_lnotab = b'' @@ -136,7 +120,8 @@ class DebugWipeFinder(object): code_impl.ob_size = 0 return code - return DebugWipeLoader(fullname, pypath) + spec.loader = DebugWipeLoader(fullname, spec.origin) + return spec def optimize_for_coverage(cov, include_patterns, omit_patterns):