Skip to content

Commit e938f0b

Browse files
committed
[GR-40094] Fix codeobject inspection and creation under bytecode interpreter
PullRequest: graalpython/2364
2 parents 9548c2f + 9a73b34 commit e938f0b

File tree

55 files changed

+1163
-679
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+1163
-679
lines changed

ci.jsonnet

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{ "overlay": "31fa7a976122ea476d023bc2304fbdb61c97b47e" }
1+
{ "overlay": "2f16c4826b16c64e35f81862b2676a2ad596f0a7" }

graalpython/com.oracle.graal.python.pegparser.test/src/com/oracle/graal/python/pegparser/ErrorTests.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@
4040
*/
4141
package com.oracle.graal.python.pegparser;
4242

43-
import org.junit.Test;
44-
4543
import java.util.EnumSet;
4644

45+
import org.junit.Test;
46+
4747
/**
4848
* Testing invalid rules. If the test method has the same names, but different number, then it tests
4949
* different alts in the rule.
@@ -334,4 +334,9 @@ public void canNotBeUsedWithinAnnotation() {
334334
checkSyntaxErrorMessage("i: (yield from f) = 3\n", "'yield expression' can not be used within an annotation", EnumSet.of(FutureFeature.ANNOTATIONS));
335335
checkSyntaxErrorMessage("i: (x:=42) = 3\n", "'named expression' can not be used within an annotation", EnumSet.of(FutureFeature.ANNOTATIONS));
336336
}
337+
338+
@Test
339+
public void testDeprecationInvalidEscape() {
340+
checkDeprecationWarning("'\\z'", "invalid escape sequence '\\z'");
341+
}
337342
}

graalpython/com.oracle.graal.python.pegparser.test/src/com/oracle/graal/python/pegparser/ParserTestBase.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,15 @@ public void checkSyntaxErrorMessage(String source, String expectedMessage, Input
151151
assertEquals("The expected message:\n\"" + expectedMessage + "\"\n was not found. The message is: \n\"" + error.getMessage() + "\"", error.getMessage(), expectedMessage);
152152
}
153153

154+
public void checkDeprecationWarning(String source, String expectedMessage) {
155+
ModTy node = parse(source, getFileName(), InputType.FILE);
156+
if (node != null) {
157+
ScopeEnvironment.analyze(node, errorCallback, EMPTY_FUTURE);
158+
}
159+
assertTrue("Expected a DeprecationWarning.", errorCallback.hasWarnings());
160+
assertEquals(expectedMessage, errorCallback.getWarnings().get(0).getMessage());
161+
}
162+
154163
public void checkIndentationError(String source) {
155164
ModTy node = parse(source, getFileName(), InputType.FILE);
156165
if (node != null) {
@@ -225,6 +234,11 @@ public void onError(ErrorCallback.ErrorType type, SourceRange sourceRange, Strin
225234
errors.add(String.format("%s[%d:%d]:%s", type.name(), sourceRange.startOffset, sourceRange.endOffset, message));
226235
}
227236

237+
@Override
238+
public void warnDeprecation(SourceRange sourceRange, String message) {
239+
fail("Unexpected deprecation warning");
240+
}
241+
228242
@Override
229243
public void reportIncompleteSource(int line) {
230244
fail("Unexpected call to reportIncompleteSource");

graalpython/com.oracle.graal.python.pegparser.test/src/com/oracle/graal/python/pegparser/TestErrorCallbackImpl.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,34 @@
4848
public class TestErrorCallbackImpl implements ErrorCallback {
4949

5050
private final List<Error> errors = new ArrayList<>(1);
51+
private final List<Error> warnings = new ArrayList<>(0);
5152

5253
public List<Error> getErrors() {
5354
return errors;
5455
}
5556

57+
public List<Error> getWarnings() {
58+
return warnings;
59+
}
60+
5661
public boolean hasErrors() {
5762
return !errors.isEmpty();
5863
}
5964

65+
public boolean hasWarnings() {
66+
return !warnings.isEmpty();
67+
}
68+
6069
@Override
6170
public void onError(ErrorType errorType, SourceRange sourceRange, String message) {
6271
errors.add(new Error(errorType, sourceRange, message));
6372
}
6473

74+
@Override
75+
public void warnDeprecation(SourceRange sourceRange, String message) {
76+
warnings.add(new Error(ErrorType.Syntax, sourceRange, message));
77+
}
78+
6579
@Override
6680
public void reportIncompleteSource(int line) {
6781
throw new IncompleteSourceException(line);
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
Module[0, 17]
2-
Assign[0, 17]
1+
Module[0, 5]
2+
Assign[0, 5]
33
LHS: Name[0, 1] Value: "a" Store
4-
RHS: LONG[4, 5] Value: 1
5-
TypeComment: int
4+
RHS: LONG[4, 5] Value: 1

graalpython/com.oracle.graal.python.pegparser/src/com/oracle/graal/python/pegparser/ErrorCallback.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,17 @@ public enum ErrorType {
5959

6060
void onError(ErrorType errorType, SourceRange sourceRange, String message);
6161

62+
void warnDeprecation(SourceRange sourceRange, String message);
63+
6264
default void onError(ErrorType errorType, SourceRange sourceRange, String message, Object... arguments) {
6365
onError(errorType, sourceRange, String.format(message, arguments));
6466
}
6567

6668
default void onError(SourceRange sourceRange, String message, Object... arguments) {
6769
onError(ErrorType.Generic, sourceRange, String.format(message, arguments));
6870
}
71+
72+
default void warnDeprecation(SourceRange sourceRange, String message, Object... arguments) {
73+
warnDeprecation(sourceRange, String.format(message, arguments));
74+
}
6975
}

graalpython/com.oracle.graal.python.pegparser/src/com/oracle/graal/python/pegparser/ParserTokenizer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public ParserTokenizer(ErrorCallback errorCallback, byte[] code, InputType type,
6666
}
6767

6868
private static EnumSet<Tokenizer.Flag> getTokenizerFlags(InputType type, boolean interactiveTerminal) {
69-
EnumSet<Tokenizer.Flag> flags = EnumSet.of(Tokenizer.Flag.TYPE_COMMENT);
69+
EnumSet<Tokenizer.Flag> flags = EnumSet.noneOf(Tokenizer.Flag.class);
7070
if (type == InputType.FILE) {
7171
flags.add(Tokenizer.Flag.EXEC_INPUT);
7272
} else if (type == InputType.SINGLE && interactiveTerminal) {

graalpython/com.oracle.graal.python.pegparser/src/com/oracle/graal/python/pegparser/sst/StringLiteralUtils.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@ public static <T> ExprTy createStringLiteral(String[] values, SourceRange[] sour
127127

128128
text = text.substring(strStartIndex, strEndIndex);
129129
if (isBytes) {
130+
for (int i = 0; i < text.length(); i++) {
131+
if (text.charAt(i) >= 0x80) {
132+
errorCallback.onError(ErrorCallback.ErrorType.Syntax, sourceRange, "bytes can only contain ASCII literal characters");
133+
}
134+
}
130135
if (sb != null || isFormatString) {
131136
errorCallback.onError(sourceRange, CANNOT_MIX_MESSAGE);
132137
if (sb != null) {
@@ -427,7 +432,7 @@ public static int createTokens(ArrayList<Token> tokens, ErrorCallback errorCallb
427432
// treated as \N escape sequence
428433
index++;
429434
} else if (lookahead(text, index, len, '{')) {
430-
warnInvalidEscapeSequence(errorCallback, text.charAt(index + 1));
435+
warnInvalidEscapeSequence(errorCallback, textSourceRange, text.charAt(index + 1));
431436
} else if (lookahead(text, index, len, 'N', '{')) {
432437
// skip escape sequence \N{...}, it should not be treated as an
433438
// expression inside f-string, but \\N{...} should be left
@@ -938,7 +943,7 @@ public static StringBuilder decodeEscapes(ErrorCallback errors, String string, S
938943
charList.append(chr);
939944
if (!wasDeprecationWarning) {
940945
wasDeprecationWarning = true;
941-
warnInvalidEscapeSequence(errors, chr);
946+
warnInvalidEscapeSequence(errors, sourceRange, chr);
942947
}
943948
}
944949
}
@@ -1052,7 +1057,7 @@ private static <T> T unescapeString(SourceRange sourceRange, ErrorCallback error
10521057
default:
10531058
if (!wasDeprecationWarning) {
10541059
wasDeprecationWarning = true;
1055-
warnInvalidEscapeSequence(errorCallback, nextChar);
1060+
warnInvalidEscapeSequence(errorCallback, sourceRange, nextChar);
10561061
}
10571062
sb.appendCodePoint(ch);
10581063
sb.appendCodePoint(nextChar);
@@ -1193,10 +1198,8 @@ public static int getCodePoint(String charName) {
11931198
return -1;
11941199
}
11951200

1196-
// TODO
1197-
@SuppressWarnings("unused")
1198-
public static void warnInvalidEscapeSequence(ErrorCallback errorCallback, char nextChar) {
1199-
// errorCallback.warn("invalid escape sequence '\\%c'", nextChar);
1201+
public static void warnInvalidEscapeSequence(ErrorCallback errorCallback, SourceRange sourceRange, char nextChar) {
1202+
errorCallback.warnDeprecation(sourceRange, "invalid escape sequence '\\%c'", nextChar);
12001203
}
12011204

12021205
private static final String UNICODE_ERROR = "'unicodeescape' codec can't decode bytes in position %d-%d:";

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/compiler/CompilerTests.java

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,14 @@
4040
*/
4141
package com.oracle.graal.python.test.compiler;
4242

43+
import static org.junit.Assert.fail;
44+
4345
import java.io.IOException;
4446
import java.nio.file.Files;
4547
import java.nio.file.Path;
4648
import java.nio.file.Paths;
4749
import java.util.EnumSet;
4850

49-
import com.oracle.graal.python.pegparser.tokenizer.SourceRange;
5051
import org.hamcrest.CoreMatchers;
5152
import org.junit.Assert;
5253
import org.junit.Rule;
@@ -60,10 +61,9 @@
6061
import com.oracle.graal.python.pegparser.InputType;
6162
import com.oracle.graal.python.pegparser.Parser;
6263
import com.oracle.graal.python.pegparser.sst.ModTy;
64+
import com.oracle.graal.python.pegparser.tokenizer.SourceRange;
6365
import com.oracle.graal.python.test.PythonTests;
6466

65-
import static org.junit.Assert.fail;
66-
6767
public class CompilerTests extends PythonTests {
6868
public CompilerTests() {
6969
}
@@ -80,6 +80,11 @@ public void testComplexNumber() {
8080
doTest("-2 + 3j");
8181
}
8282

83+
@Test
84+
public void testMinusFolding() {
85+
doTest("-1 * -7.0");
86+
}
87+
8388
@Test
8489
public void testAssignment() {
8590
doTest("a = 12");
@@ -528,10 +533,40 @@ public void testEmptyDict() {
528533
}
529534

530535
@Test
531-
public void testTupleLiteral() {
536+
public void testTupleLiteralInts() {
532537
doTest("(1, 2, 3)");
533538
}
534539

540+
@Test
541+
public void testTupleLiteralDoubles() {
542+
doTest("(1.0, 2.0, 3.0)");
543+
}
544+
545+
@Test
546+
public void testTupleLiteralBooleans() {
547+
doTest("(False, True)");
548+
}
549+
550+
@Test
551+
public void testTupleLiteralObjects() {
552+
doTest("('a', 1, None)");
553+
}
554+
555+
@Test
556+
public void testTupleLiteralMixed() {
557+
doTest("(1, 2, 3.0)");
558+
}
559+
560+
@Test
561+
public void testTupleLiteralNonConstant() {
562+
doTest("(1, 2, [3])");
563+
}
564+
565+
@Test
566+
public void testTupleLiteralMixedIntegers() {
567+
doTest("(1, 17179869184, 3)");
568+
}
569+
535570
@Test
536571
public void testTupleLiteralExpand() {
537572
doTest("(1, 2, 3, *a, 5)");
@@ -826,7 +861,7 @@ private static CodeUnit assemble(String src, InputType type) {
826861
ModTy result = (ModTy) parser.parse();
827862
Compiler compiler = new Compiler(errorCallback);
828863
CompilationUnit cu = compiler.compile(result, EnumSet.noneOf(Compiler.Flags.class), 2);
829-
return cu.assemble(0);
864+
return cu.assemble();
830865
}
831866

832867
private void checkCodeUnit(CodeUnit co) {
@@ -857,6 +892,11 @@ public void reportIncompleteSource(int line) {
857892
public void onError(ErrorType errorType, SourceRange sourceRange, String message) {
858893
throw new SyntaxError(errorType, message);
859894
}
895+
896+
@Override
897+
public void warnDeprecation(SourceRange sourceRange, String message) {
898+
throw new AssertionError("Unexpected warning: " + message);
899+
}
860900
}
861901

862902
private static final class SyntaxError extends RuntimeException {

graalpython/com.oracle.graal.python.test/src/tests/test_code.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,9 @@ def test_module_code():
178178
assert set(code.co_varnames) == set()
179179
assert code.co_filename.endswith("__init__.py")
180180
assert code.co_name.startswith("<module")
181-
if sys.implementation.name == 'graalpy':
182-
assert code.co_firstlineno == 1
181+
# AST interpreter doesn't pass this
182+
if not sys.implementation.name == 'graalpy' or __graalpython__.uses_bytecode_interpreter:
183+
assert code.co_firstlineno == 40
183184
# assert code.co_lnotab == b''
184185
assert code.co_freevars == tuple()
185186
assert code.co_cellvars == tuple()

0 commit comments

Comments
 (0)