Skip to content

Commit 8a7395b

Browse files
committed
Better handling of corner cases with emtpy bodies.
1 parent f4e30a5 commit 8a7395b

File tree

3 files changed

+59
-25
lines changed

3 files changed

+59
-25
lines changed

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/interop/StandardTagsTests.java

Lines changed: 55 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -73,86 +73,122 @@ private static Context newContext(Engine engine) {
7373

7474
@Test
7575
public void testRootBodyTagModule() throws Exception {
76-
String code = "import time\n" + "time.gmtime()\n";
76+
String code = "import time\n" +
77+
"time.gmtime()\n";
7778
checkRootTagAndRootBodyTag(code);
7879
}
7980

8081
@Test
8182
public void testRootBodyTagGenerator01() throws Exception {
82-
String code = "def test():\n" + " yield 22\n" + "for i in test():\n" + " i\n";
83+
String code = "def test():\n" +
84+
" yield 22\n" +
85+
"for i in test():\n" +
86+
" i\n";
8387
checkRootTagAndRootBodyTag(code);
8488
}
8589

8690
@Test
8791
public void testRootBodyTagGenerator02() throws Exception {
88-
String code = "def test():\n" + " yield 22\n" + " yield 42\n" + " yield 62\n" + "for i in test():\n" + " i\n";
92+
String code = "def test():\n" +
93+
" yield 22\n" +
94+
" yield 42\n" +
95+
" yield 62\n" +
96+
"for i in test():\n" +
97+
" i\n";
8998
checkRootTagAndRootBodyTag(code);
9099
}
91100

92101
@Test
93102
public void testRootBodyTagGenerator03() throws Exception {
94-
String code = "def fn(a, b):\n" + " yield a\n" + " yield b\n" + "for i in fn(3,4):\n" + " i\n";
103+
String code = "def fn(a, b):\n" +
104+
" yield a\n" +
105+
" yield b\n" +
106+
"for i in fn(3,4):\n" +
107+
" i\n";
95108
checkRootTagAndRootBodyTag(code);
96109
}
97110

98111
@Test
99112
public void testRootBodyTagFunction01() throws Exception {
100-
String code = "def test():\n" + " return 22\n" + "test()";
113+
String code = "def test():\n" +
114+
" return 22\n" +
115+
"test()";
101116
checkRootTagAndRootBodyTag(code);
102117
}
103118

104119
@Test
105120
public void testRootBodyTagFunction02() throws Exception {
106-
String code = "def test(a,b):\n" + " return a + b\n" + "test(1, 2)";
121+
String code = "def test(a,b):\n" +
122+
" return a + b\n" +
123+
"test(1, 2)";
107124
HashMap<InstrumentableNode, InstrumentableNode> rootsMap = checkRootTagAndRootBodyTag(code);
108125
InnerRootNode inner = findInnerRootNodeWithEndOffset(rootsMap, 30);
109126
checkBodyPosition(rootsMap, inner, 17, 29);
110127
}
111128

112129
@Test
113130
public void testRootBodyTagFunction03() throws Exception {
114-
String code = "def test(a,b):\n" + " '''This is a simple doc'''\n" + " return a + b\n" + "test(1, 2)";
131+
String code = "def test(a,b):\n" +
132+
" '''This is a simple doc'''\n" +
133+
" return a + b\n" +
134+
"test(1, 2)";
115135
HashMap<InstrumentableNode, InstrumentableNode> rootsMap = checkRootTagAndRootBodyTag(code);
116136
InnerRootNode inner = findInnerRootNodeWithEndOffset(rootsMap, 59);
117137
checkBodyPosition(rootsMap, inner, 46, 58);
118138
}
119139

120140
@Test
121141
public void testRootBodyTagFunction04() throws Exception {
122-
String code = "def test():\n" + " '''Function without body'''\n" + "test()";
123-
HashMap<InstrumentableNode, InstrumentableNode> rootsMap = checkRootTagAndRootBodyTag(code, true);
142+
String code = "def test():\n" +
143+
" '''Function without body'''\n" +
144+
"test()";
145+
HashMap<InstrumentableNode, InstrumentableNode> rootsMap = checkRootTagAndRootBodyTag(code);
124146
InnerRootNode inner = findInnerRootNodeWithEndOffset(rootsMap, 42);
125-
Assert.assertEquals(null, rootsMap.get(inner));
147+
checkBodyPosition(rootsMap, inner, 41, 42);
148+
}
149+
150+
@Test
151+
public void testRootBodyTagFunction05() throws Exception {
152+
String code = "def fn():\n" +
153+
" try:\n" +
154+
" pass\n" +
155+
" except ValueError as va:\n" +
156+
" pass\n" +
157+
"fn()";
158+
HashMap<InstrumentableNode, InstrumentableNode> rootsMap = checkRootTagAndRootBodyTag(code);
159+
InnerRootNode inner = findInnerRootNodeWithEndOffset(rootsMap, 62);
160+
checkBodyPosition(rootsMap, inner, 12, 62);
126161
}
127162

128163
@Test
129164
public void testRootBodyTagLambda01() throws Exception {
130-
String code = "x = lambda a, b, c : a + b + c\n" + "x(5, 6, 2)";
165+
String code = "x = lambda a, b, c : a + b + c\n" +
166+
"x(5, 6, 2)";
131167
checkRootTagAndRootBodyTag(code);
132168
}
133169

134170
@Test
135171
public void testRootBodyTagClass01() throws Exception {
136-
String code = "class MyClass:\n" + " x = 5\n" + "m = MyClass()\n";
172+
String code = "class MyClass:\n" +
173+
" x = 5\n" +
174+
"m = MyClass()\n";
137175
HashMap<InstrumentableNode, InstrumentableNode> rootsMap = checkRootTagAndRootBodyTag(code);
138176
InnerRootNode inner = findInnerRootNodeWithEndOffset(rootsMap, 23);
139177
checkBodyPosition(rootsMap, inner, 17, 22);
140178
}
141179

142180
@Test
143181
public void testRootBodyTagClass02() throws Exception {
144-
String code = "class MyClass:\n" + " '''This is a simple test class'''\n" + " x = 5\n" + "m = MyClass()\n";
182+
String code = "class MyClass:\n" +
183+
" '''This is a simple test class'''\n" +
184+
" x = 5\n" + "m = MyClass()\n";
145185
HashMap<InstrumentableNode, InstrumentableNode> rootsMap = checkRootTagAndRootBodyTag(code);
146186
InnerRootNode inner = findInnerRootNodeWithEndOffset(rootsMap, 59);
147187
checkBodyPosition(rootsMap, inner, 53, 58);
148188

149189
}
150190

151191
private HashMap<InstrumentableNode, InstrumentableNode> checkRootTagAndRootBodyTag(String code) throws Exception {
152-
return checkRootTagAndRootBodyTag(code, false);
153-
}
154-
155-
private HashMap<InstrumentableNode, InstrumentableNode> checkRootTagAndRootBodyTag(String code, boolean possibleEmptyRootTag) throws Exception {
156192
Engine engine = Engine.newBuilder().build();
157193
Context newContext = newContext(engine);
158194
newContext.initialize("python");
@@ -166,10 +202,8 @@ private HashMap<InstrumentableNode, InstrumentableNode> checkRootTagAndRootBodyT
166202

167203
newContext.eval(source);
168204

169-
if (!possibleEmptyRootTag) {
170-
for (InstrumentableNode rootBodyTag : rootsMap.values()) {
171-
assertTrue("There is a RootTag node without RootBodyTag node", rootBodyTag != null);
172-
}
205+
for (InstrumentableNode rootBodyTag : rootsMap.values()) {
206+
assertTrue("There is a RootTag node without RootBodyTag node", rootBodyTag != null);
173207
}
174208
return rootsMap;
175209
}

graalpython/com.oracle.graal.python.test/testData/goldenFiles/ClassDefTests/classDoc01.tast

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ ModuleRootNode Name: <module 'classDoc01'> SourceSection: [0,76]`class Test():
8383
ReturnTargetNode SourceSection: [38,76]`def method():↵ ...`
8484
Body: BlockNode SourceSection: None
8585
BlockNode SourceSection: None
86-
FunctionBodyNode SourceSection: [0,0]Empty
86+
FunctionBodyNode SourceSection: [75,76]`"`
8787
Return Expresssion: ReadLocalVariableNode SourceSection: None
8888
Frame: [1,<return_val>,Illegal]
8989
ReadVariableFromFrameNodeGen SourceSection: None

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/parser/sst/FactorySSTVisitor.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,7 @@ public PNode visit(FunctionDefSSTNode node) {
740740
StatementNode[] st = ((BaseBlockNode) body).getStatements();
741741
if (st.length == 1) {
742742
functionBody = FunctionBodyNode.create();
743-
functionBody.assignSourceSection(source.createUnavailableSection());
743+
functionBody.assignSourceSection(createSourceSection(node.endOffset - 1, node.endOffset));
744744
} else {
745745
if (st.length == 2) {
746746
functionBody = FunctionBodyNode.create(st[1]);
@@ -760,11 +760,11 @@ public PNode visit(FunctionDefSSTNode node) {
760760
}
761761
} else {
762762
functionBody = FunctionBodyNode.create();
763-
functionBody.assignSourceSection(createSourceSection(node.endOffset, node.endOffset));
763+
functionBody.assignSourceSection(createSourceSection(node.endOffset - 1, node.endOffset));
764764
}
765765
} else {
766766
functionBody = createFunctionBody(body);
767-
functionBody.assignSourceSection(source.createUnavailableSection());
767+
functionBody.assignSourceSection(createSourceSection(node.body.startOffset, node.body.endOffset));
768768
}
769769

770770
body = functionBody;

0 commit comments

Comments
 (0)