Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
24 changes: 24 additions & 0 deletions Lib/test/test_generated_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,30 @@ def test_error_if_plain_with_comment(self):
"""
self.run_cases_test(input, output)

def test_PyStackRef_FromPyObjectNew_with_comment(self):
input = """
inst(OP, (-- value)) {
// Comment is ok
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have more /* ... */ comments as well. And more than one comment as well? something like:

inst(OP, (-- value)) {
    // Comment is ok
	value = ...
}

inst(OP, (-- value)) {
    /* comment is ok */
	value = ...
}

inst(OP, (-- value)) {
    /* 
     comment is ok 
	*/
	value = ...
}

inst(OP, (-- value)) {
	// comment
	// comment
	value = ...
}

inst(OP, (-- value)) {
	// comment
	value = ...
	// comment
}

(I don't know which comments are actually accepted). The output being always the same, you can parametrize the test as follows:

output = ... 
line = "value = PyStackRef_FromPyObjectNew(GETITEM(FRAME_CO_CONSTS, oparg));"
for src in [
	("//", line),
	("// comment", line),
	("/* comment */", line),
    ...
]:
	lines = textwrap.indent('\n'.join(src), " " * 4)
	input = f"inst(OP, (-- value)) {{\n{lines}\n}}"
	with self.subTest(src=src):
		self.run_cases_test(input, output)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather we do all these tests explicitly. Example you provided is hard to read IMO.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Considering the rest of the file seems to never parametrize the tests, maybe explicit is better. I personally find it quite explicit but if you think it reads worse, keep the current style!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(However, we still need more tests. To ease debugging, let's create different test functions for each of them as well?)

value = PyStackRef_FromPyObjectNew(GETITEM(FRAME_CO_CONSTS, oparg));
}
"""

output = """
TARGET(OP) {
frame->instr_ptr = next_instr;
next_instr += 1;
INSTRUCTION_STATS(OP);
_PyStackRef value;
// Comment is ok
value = PyStackRef_FromPyObjectNew(GETITEM(FRAME_CO_CONSTS, oparg));
stack_pointer[0] = value;
stack_pointer += 1;
assert(WITHIN_STACK_BOUNDS());
DISPATCH();
}
"""
self.run_cases_test(input, output)

def test_error_if_pop(self):
input = """
inst(OP, (left, right -- res)) {
Expand Down
7 changes: 4 additions & 3 deletions Tools/cases_generator/analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,10 @@ def find_assignment_target(node: parser.InstDef, idx: int) -> list[lexer.Token]:
offset = 0
for tkn in reversed(node.block.tokens[: idx]):
if tkn.kind in {"SEMI", "LBRACE", "RBRACE"}:
return node.block.tokens[idx - offset : idx]
tokens = node.block.tokens[idx - offset : idx]
while tokens and tokens[0].kind == "COMMENT":
tokens = tokens[1:]
return tokens
offset += 1
return []

Expand All @@ -406,8 +409,6 @@ def find_stores_outputs(node: parser.InstDef) -> list[lexer.Token]:
continue
lhs = find_assignment_target(node, idx)
assert lhs
while lhs and lhs[0].kind == "COMMENT":
lhs = lhs[1:]
if len(lhs) != 1 or lhs[0].kind != "IDENTIFIER":
continue
name = lhs[0]
Expand Down
Loading