Skip to content

Commit

Permalink
Add an ABC for the visitors passed to pyc.visit().
Browse files Browse the repository at this point in the history
To make a subsequent change clearer.

PiperOrigin-RevId: 652453548
  • Loading branch information
tjgq authored and copybara-github committed Jul 15, 2024
1 parent 029c393 commit 563c4e5
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 7 deletions.
4 changes: 2 additions & 2 deletions pytype/blocks/process_blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def _is_function_def(fn_code):
return True


class CollectAnnotationTargetsVisitor:
class CollectAnnotationTargetsVisitor(pyc.CodeVisitor):
"""Collect opcodes that might have annotations attached."""

def __init__(self):
Expand Down Expand Up @@ -68,7 +68,7 @@ def visit_code(self, code):
return code


class FunctionDefVisitor:
class FunctionDefVisitor(pyc.CodeVisitor):
"""Add metadata to function definition opcodes."""

def __init__(self, param_annotations):
Expand Down
2 changes: 1 addition & 1 deletion pytype/constant_folding.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ def resolve(self, op):
return f


class _FoldConstants:
class _FoldConstants(pyc.CodeVisitor):
"""Fold constant literals in pyc code."""

def visit_code(self, code):
Expand Down
13 changes: 11 additions & 2 deletions pytype/pyc/pyc.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Functions for generating, reading and parsing pyc."""

import abc
import copy

from pycnite import pyc
Expand All @@ -11,6 +12,14 @@
CompileError = compiler.CompileError


# The abstract base class for a code visitor passed to pyc.visit.
class CodeVisitor(abc.ABC):

@abc.abstractmethod
def visit_code(self, code):
...


def parse_pyc_string(data):
"""Parse pyc data from a string.
Expand All @@ -23,7 +32,7 @@ def parse_pyc_string(data):
return pyc.loads(data)


class AdjustFilename:
class AdjustFilename(CodeVisitor):
"""Visitor for changing co_filename in a code object."""

def __init__(self, filename):
Expand Down Expand Up @@ -71,7 +80,7 @@ def compile_src(src, filename, python_version, python_exe, mode="exec"):
_VISIT_CACHE = {}


def visit(c, visitor):
def visit(c, visitor: CodeVisitor):
"""Recursively process constants in a pyc using a visitor."""
if hasattr(c, "co_consts"):
# This is a CodeType object (because it has co_consts). Visit co_consts,
Expand Down
4 changes: 2 additions & 2 deletions pytype/vm_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from pytype.errors import error_types
from pytype.overlays import metaclass
from pytype.pyc import opcodes
from pytype.pyc import pyc
from pytype.pytd import mro
from pytype.pytd import pytd
from pytype.pytd import slots
Expand Down Expand Up @@ -72,7 +73,7 @@ def __repr__(self):
return f"Block({self.type}: {self.op_index} level={self.level})"


class FindIgnoredTypeComments:
class FindIgnoredTypeComments(pyc.CodeVisitor):
"""A visitor that finds type comments that will be ignored."""

def __init__(self, type_comments):
Expand All @@ -82,7 +83,6 @@ def __init__(self, type_comments):
self._ignored_type_lines = set(type_comments)

def visit_code(self, code):
"""Interface for pyc.visit."""
for op in code.code_iter:
# Make sure we have attached the type comment to an opcode.
if isinstance(op, blocks.STORE_OPCODES):
Expand Down

0 comments on commit 563c4e5

Please sign in to comment.