Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove inspect.stack() from @rule parsing to fix import time regression in py3 #7447

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions src/python/pants/engine/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import inspect
import itertools
import logging
import sys
from abc import abstractproperty
from builtins import bytes, str
from types import GeneratorType
Expand All @@ -34,14 +35,13 @@
class _RuleVisitor(ast.NodeVisitor):
"""Pull `Get` calls out of an @rule body and validate `yield` statements."""

def __init__(self, func, func_node, func_source, orig_indent, frame, parents_table):
def __init__(self, func, func_node, func_source, orig_indent, parents_table):
super(_RuleVisitor, self).__init__()
self._gets = []
self._func = func
self._func_node = func_node
self._func_source = func_source
self._orig_indent = orig_indent
self._frame = frame
self._parents_table = parents_table
self._yields_in_assignments = set()

Expand All @@ -51,7 +51,8 @@ def gets(self):

def _generate_ast_error_message(self, node, msg):
# This is the location info of the start of the decorated @rule.
filename, line_number, _, context_lines, _ = inspect.getframeinfo(self._frame, context=4)
filename = inspect.getsourcefile(self._func)
source_lines, line_number = inspect.getsourcelines(self._func)

# The asttokens library is able to keep track of line numbers and column offsets for us -- the
# stdlib ast library only provides these relative to each parent node.
Expand Down Expand Up @@ -82,14 +83,14 @@ def _generate_ast_error_message(self, node, msg):

The rule defined by function `{func_name}` begins at:
{filename}:{line_number}:{orig_indent}
{context_lines}
{source_lines}
""".format(func_name=self._func.__name__, msg=msg,
filename=filename, line_number=line_number, orig_indent=self._orig_indent,
node_line_number=node_file_line,
node_col=fully_indented_node_col,
node_text=indented_node_text,
# Strip any leading or trailing newlines from the start of the rule body.
context_lines=''.join(context_lines).strip('\n')))
source_lines=''.join(source_lines).strip('\n')))

class YieldVisitError(Exception): pass

Expand Down Expand Up @@ -242,16 +243,19 @@ def wrapper(func):
if not inspect.isfunction(func):
raise ValueError('The @rule decorator must be applied innermost of all decorators.')

caller_frame = inspect.stack()[1][0]
owning_module = sys.modules[func.__module__]
source = inspect.getsource(func)
beginning_indent = _get_starting_indent(source)
if beginning_indent:
source = "\n".join(line[beginning_indent:] for line in source.split("\n"))
module_ast = ast.parse(source)

def resolve_type(name):
resolved = caller_frame.f_globals.get(name) or caller_frame.f_builtins.get(name)
if not isinstance(resolved, type):
resolved = getattr(owning_module, name, None) or owning_module.__builtins__.get(name, None)
if resolved is None:
raise ValueError('Could not resolve type `{}` in module {}'
.format(name, owning_module.__name__))
elif not isinstance(resolved, type):
raise ValueError('Expected a `type` constructor, but got: {}'.format(name))
return resolved

Expand All @@ -270,7 +274,6 @@ def resolve_type(name):
func_node=rule_func_node,
func_source=source,
orig_indent=beginning_indent,
frame=caller_frame,
parents_table=parents_table,
)
rule_visitor.visit(rule_func_node)
Expand Down
8 changes: 5 additions & 3 deletions tests/python/pants_test/engine/test_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -737,12 +737,14 @@ def g():

The rule defined by function `g` begins at:
test_rules.py:{rule_lineno}:{rule_col}
with self.assertRaises(_RuleVisitor.YieldVisitError) as cm:
@rule(A, [])
def g():
""".format(lineno=(sys._getframe().f_lineno - 20),
# This is a yield statement without an assignment, and not at the end.
yield Get(B, D, D())
yield A()
""".format(lineno=(sys._getframe().f_lineno - 22),
col=8,
rule_lineno=(sys._getframe().f_lineno - 25),
rule_lineno=(sys._getframe().f_lineno - 27),
rule_col=6))

def create_full_graph(self, rules, validate=True):
Expand Down