Skip to content

Commit

Permalink
DebugWipeFinder fixes for 3.12
Browse files Browse the repository at this point in the history
Summary:
X-link: facebookincubator/zstrong#662

find_module has been deprecated for some time. Instead use find_spec

Reviewed By: itamaro

Differential Revision: D52495680

fbshipit-source-id: 9b827a783af2d858816a8d197530874f680502f3
  • Loading branch information
fried authored and facebook-github-bot committed Jan 4, 2024
1 parent 3b45167 commit 161b335
Showing 1 changed file with 10 additions and 25 deletions.
35 changes: 10 additions & 25 deletions build/fbcode_builder/CMake/fb_py_test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,8 @@
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
Expand Down Expand Up @@ -82,7 +79,7 @@ def include(self, path):
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
Expand All @@ -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),
Expand All @@ -126,7 +110,7 @@ class DebugWipeLoader(SourceFileLoader):
"""

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''
Expand All @@ -136,7 +120,8 @@ def get_code(self, fullname):
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):
Expand Down

0 comments on commit 161b335

Please sign in to comment.