Skip to content

Commit 22bb7e0

Browse files
committed
Bytecode DSL: fix some tagged tests: errors from super, add syntax warnings, __annotations__ assignments
1 parent 5b2ba8b commit 22bb7e0

File tree

8 files changed

+295
-265
lines changed

8 files changed

+295
-265
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/SysModuleBuiltins.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -611,8 +611,8 @@ public void postInitialize0(Python3Core core) {
611611
}
612612
sys.setAttribute(tsLiteral("dont_write_bytecode"), context.getOption(PythonOptions.DontWriteBytecodeFlag));
613613
TruffleString pycachePrefix = context.getOption(PythonOptions.PyCachePrefix);
614-
if (pycachePrefix.isEmpty() && PythonOptions.ENABLE_BYTECODE_DSL_INTERPRETER && System.getenv("CI") == null) {
615-
pycachePrefix = PythonUtils.toTruffleStringUncached("__bci_dsl_pycache__");
614+
if (pycachePrefix.isEmpty() && PythonOptions.ENABLE_BYTECODE_DSL_INTERPRETER && System.getenv("GRAALPY_BYTECODE_DSL_PYTHONPYCACHEPREFIX") != null) {
615+
pycachePrefix = PythonUtils.toTruffleStringUncached(System.getenv("GRAALPY_BYTECODE_DSL_PYTHONPYCACHEPREFIX"));
616616
}
617617
sys.setAttribute(tsLiteral("pycache_prefix"), pycachePrefix.isEmpty() ? PNone.NONE : pycachePrefix);
618618
sys.setAttribute(tsLiteral("_stdlib_dir"), stdlibHome);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/superobject/SuperBuiltins.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ private PNone initFromLocalFrame(VirtualFrame frame, Node inliningTarget, SuperO
336336
private PNone initFromLocalFrame(VirtualFrame frame, Node inliningTarget, SuperObject self, PBytecodeDSLRootNode rootNode, Frame localFrame, CellBuiltins.GetRefNode getRefNode,
337337
PRaiseNode raiseNode) {
338338
PCell classCell = rootNode.readClassCell(localFrame);
339-
if (classCell == null) {
339+
if (!rootNode.hasSelf() || classCell == null) {
340340
throw raiseNode.raise(inliningTarget, RuntimeError, ErrorMessages.SUPER_NO_CLASS);
341341
}
342342
Object cls = getRefNode.execute(inliningTarget, classCell);
@@ -346,7 +346,7 @@ private PNone initFromLocalFrame(VirtualFrame frame, Node inliningTarget, SuperO
346346
}
347347
Object obj = rootNode.readSelf(localFrame);
348348
if (obj == null) {
349-
throw raiseNode.raise(inliningTarget, RuntimeError, ErrorMessages.NO_ARGS, "super()");
349+
throw raiseNode.raise(inliningTarget, RuntimeError, ErrorMessages.SUPER_ARG0_DELETED);
350350
}
351351
return init(frame, self, cls, obj, inliningTarget, raiseNode);
352352
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/compiler/Compiler.java

Lines changed: 13 additions & 173 deletions
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,16 @@
157157
import static com.oracle.graal.python.compiler.OpCodes.UNPACK_SEQUENCE;
158158
import static com.oracle.graal.python.compiler.OpCodes.UNWRAP_EXC;
159159
import static com.oracle.graal.python.compiler.OpCodes.YIELD_VALUE;
160+
import static com.oracle.graal.python.compiler.SSTUtils.checkCaller;
161+
import static com.oracle.graal.python.compiler.SSTUtils.checkCompare;
162+
import static com.oracle.graal.python.compiler.SSTUtils.checkForbiddenArgs;
163+
import static com.oracle.graal.python.compiler.SSTUtils.checkIndex;
164+
import static com.oracle.graal.python.compiler.SSTUtils.checkSubscripter;
160165
import static com.oracle.graal.python.nodes.SpecialAttributeNames.J___TYPE_PARAMS__;
161166
import static com.oracle.graal.python.nodes.StringLiterals.T_EMPTY_STRING;
162167
import static com.oracle.graal.python.util.PythonUtils.arrayCopyOf;
163168
import static com.oracle.graal.python.util.PythonUtils.codePointsToTruffleString;
164169
import static com.oracle.graal.python.util.PythonUtils.toTruffleStringUncached;
165-
import static com.oracle.truffle.api.CompilerDirectives.shouldNotReachHere;
166170

167171
import java.util.ArrayList;
168172
import java.util.Collections;
@@ -172,7 +176,6 @@
172176
import java.util.List;
173177

174178
import com.oracle.graal.python.PythonLanguage;
175-
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
176179
import com.oracle.graal.python.builtins.objects.PNone;
177180
import com.oracle.graal.python.compiler.OpCodes.CollectionBits;
178181
import com.oracle.graal.python.compiler.OpCodes.MakeTypeParamKind;
@@ -197,7 +200,6 @@
197200
import com.oracle.graal.python.pegparser.sst.ExceptHandlerTy;
198201
import com.oracle.graal.python.pegparser.sst.ExprContextTy;
199202
import com.oracle.graal.python.pegparser.sst.ExprTy;
200-
import com.oracle.graal.python.pegparser.sst.ExprTy.Constant;
201203
import com.oracle.graal.python.pegparser.sst.ExprTy.Tuple;
202204
import com.oracle.graal.python.pegparser.sst.KeywordTy;
203205
import com.oracle.graal.python.pegparser.sst.MatchCaseTy;
@@ -492,16 +494,7 @@ private void exitScope() {
492494
}
493495

494496
protected final void checkForbiddenName(String id, ExprContextTy context) {
495-
if (context == ExprContextTy.Store) {
496-
if (id.equals("__debug__")) {
497-
throw parserCallbacks.onError(ErrorType.Syntax, unit.currentLocation, "cannot assign to __debug__");
498-
}
499-
}
500-
if (context == ExprContextTy.Del) {
501-
if (id.equals("__debug__")) {
502-
throw parserCallbacks.onError(ErrorType.Syntax, unit.currentLocation, "cannot delete __debug__");
503-
}
504-
}
497+
SSTUtils.checkForbiddenName(parserCallbacks, unit.currentLocation, id, context);
505498
}
506499

507500
private boolean containsAnnotations(StmtTy[] stmts) {
@@ -1286,7 +1279,7 @@ private static boolean hasOnlyPlainArgs(ExprTy[] args, KeywordTy[] keywords) {
12861279
@Override
12871280
public Void visit(ExprTy.Call node) {
12881281
SourceRange savedLocation = setLocation(node);
1289-
checkCaller(node.func);
1282+
checkCaller(parserCallbacks, node.func);
12901283
try {
12911284
// n.b.: we do things completely different from python for calls
12921285
OpCodes op = CALL_FUNCTION_VARARGS;
@@ -1386,7 +1379,7 @@ private void addCompareOp(CmpOpTy op) {
13861379
@Override
13871380
public Void visit(ExprTy.Compare node) {
13881381
SourceRange savedLocation = setLocation(node);
1389-
checkCompare(node);
1382+
checkCompare(parserCallbacks, node);
13901383
try {
13911384
node.left.accept(this);
13921385
if (node.comparators.length == 1) {
@@ -1568,7 +1561,7 @@ public Void visit(ExprTy.JoinedStr node) {
15681561
public Void visit(ExprTy.Lambda node) {
15691562
SourceRange savedLocation = setLocation(node);
15701563
try {
1571-
checkForbiddenArgs(node.args);
1564+
checkForbiddenArgs(parserCallbacks, unit.currentLocation, node.args);
15721565
int makeFunctionFlags = collectDefaults(node.args);
15731566
enterScope("<lambda>", CompilationScope.Lambda, node, node.args, node.getSourceRange());
15741567
/* Make None the first constant, so the lambda can't have a docstring. */
@@ -1862,8 +1855,8 @@ public Void visit(ExprTy.Starred node) {
18621855
public Void visit(ExprTy.Subscript node) {
18631856
SourceRange savedLocation = setLocation(node);
18641857
if (node.context == ExprContextTy.Load) {
1865-
checkSubscripter(node.value);
1866-
checkIndex(node.value, node.slice);
1858+
checkSubscripter(parserCallbacks, node.value);
1859+
checkIndex(parserCallbacks, node.value, node.slice);
18671860
}
18681861
try {
18691862
node.value.accept(this);
@@ -2660,7 +2653,7 @@ public Void visit(StmtTy.FunctionDef node) {
26602653

26612654
private Void visitFunctionDef(StmtTy node, String name, ArgumentsTy args, StmtTy[] body, ExprTy[] decoratorList, ExprTy returns, TypeParamTy[] typeParams, boolean isAsync) {
26622655
setLocation(node);
2663-
checkForbiddenArgs(args);
2656+
checkForbiddenArgs(parserCallbacks, unit.currentLocation, args);
26642657

26652658
// visit decorators
26662659
visitSequence(decoratorList);
@@ -2831,32 +2824,6 @@ private int collectDefaults(ArgumentsTy args) {
28312824
return makeFunctionFlags;
28322825
}
28332826

2834-
private void checkForbiddenArgs(ArgumentsTy args) {
2835-
if (args != null) {
2836-
if (args.posOnlyArgs != null) {
2837-
for (ArgTy arg : args.posOnlyArgs) {
2838-
checkForbiddenName(arg.arg, ExprContextTy.Store);
2839-
}
2840-
}
2841-
if (args.args != null) {
2842-
for (ArgTy arg : args.args) {
2843-
checkForbiddenName(arg.arg, ExprContextTy.Store);
2844-
}
2845-
}
2846-
if (args.kwOnlyArgs != null) {
2847-
for (ArgTy arg : args.kwOnlyArgs) {
2848-
checkForbiddenName(arg.arg, ExprContextTy.Store);
2849-
}
2850-
}
2851-
if (args.varArg != null) {
2852-
checkForbiddenName(args.varArg.arg, ExprContextTy.Store);
2853-
}
2854-
if (args.kwArg != null) {
2855-
checkForbiddenName(args.kwArg.arg, ExprContextTy.Store);
2856-
}
2857-
}
2858-
}
2859-
28602827
@Override
28612828
public Void visit(StmtTy.Global node) {
28622829
setLocation(node);
@@ -2885,7 +2852,7 @@ private void jumpIf(ExprTy test, Block next, boolean jumpIfTrue) {
28852852
// TODO Optimize for various test types, such as short-circuit operators
28862853
// See compiler_jump_if in CPython
28872854
if (test instanceof ExprTy.Compare) {
2888-
checkCompare((ExprTy.Compare) test);
2855+
checkCompare(parserCallbacks, (ExprTy.Compare) test);
28892856
}
28902857
test.accept(this);
28912858
if (jumpIfTrue) {
@@ -4197,133 +4164,6 @@ private void warn(SSTNode node, String message, Object... arguments) {
41974164
parserCallbacks.onWarning(WarningType.Syntax, node.getSourceRange(), message, arguments);
41984165
}
41994166

4200-
private void checkCompare(ExprTy.Compare node) {
4201-
ExprTy leftExpr = node.left;
4202-
boolean left = checkIsArg(leftExpr);
4203-
int n = node.ops == null ? 0 : node.ops.length;
4204-
for (int i = 0; i < n; ++i) {
4205-
CmpOpTy op = node.ops[i];
4206-
ExprTy rightExpr = node.comparators[i];
4207-
boolean right = checkIsArg(rightExpr);
4208-
if (op == CmpOpTy.Is || op == CmpOpTy.IsNot) {
4209-
if (!right || !left) {
4210-
ExprTy literal = !left ? leftExpr : rightExpr;
4211-
warn(node, op == CmpOpTy.Is ? "\"is\" with '%s' literal. Did you mean \"==\"?" : "\"is not\" with '%s' literal. Did you mean \"!=\"?", inferType(literal).getName());
4212-
}
4213-
}
4214-
left = right;
4215-
leftExpr = rightExpr;
4216-
}
4217-
}
4218-
4219-
private static boolean checkIsArg(ExprTy e) {
4220-
if (e instanceof ExprTy.Constant) {
4221-
ConstantValue.Kind kind = ((Constant) e).value.kind;
4222-
return kind == Kind.NONE || kind == Kind.BOOLEAN || kind == Kind.ELLIPSIS;
4223-
}
4224-
return true;
4225-
}
4226-
4227-
private static PythonBuiltinClassType inferType(ExprTy e) {
4228-
if (e instanceof ExprTy.Tuple) {
4229-
return PythonBuiltinClassType.PTuple;
4230-
}
4231-
if (e instanceof ExprTy.List || e instanceof ExprTy.ListComp) {
4232-
return PythonBuiltinClassType.PList;
4233-
}
4234-
if (e instanceof ExprTy.Dict || e instanceof ExprTy.DictComp) {
4235-
return PythonBuiltinClassType.PDict;
4236-
}
4237-
if (e instanceof ExprTy.Set || e instanceof ExprTy.SetComp) {
4238-
return PythonBuiltinClassType.PSet;
4239-
}
4240-
if (e instanceof ExprTy.GeneratorExp) {
4241-
return PythonBuiltinClassType.PGenerator;
4242-
}
4243-
if (e instanceof ExprTy.Lambda) {
4244-
return PythonBuiltinClassType.PFunction;
4245-
}
4246-
if (e instanceof ExprTy.JoinedStr || e instanceof ExprTy.FormattedValue) {
4247-
return PythonBuiltinClassType.PString;
4248-
}
4249-
if (e instanceof ExprTy.Constant) {
4250-
switch (((ExprTy.Constant) e).value.kind) {
4251-
case NONE:
4252-
return PythonBuiltinClassType.PNone;
4253-
case ELLIPSIS:
4254-
return PythonBuiltinClassType.PEllipsis;
4255-
case BOOLEAN:
4256-
return PythonBuiltinClassType.Boolean;
4257-
case DOUBLE:
4258-
return PythonBuiltinClassType.PFloat;
4259-
case COMPLEX:
4260-
return PythonBuiltinClassType.PComplex;
4261-
case LONG:
4262-
case BIGINTEGER:
4263-
return PythonBuiltinClassType.PInt;
4264-
case CODEPOINTS:
4265-
return PythonBuiltinClassType.PString;
4266-
case BYTES:
4267-
return PythonBuiltinClassType.PBytes;
4268-
case TUPLE:
4269-
return PythonBuiltinClassType.PTuple;
4270-
case FROZENSET:
4271-
return PythonBuiltinClassType.PFrozenSet;
4272-
default:
4273-
throw shouldNotReachHere("Invalid ConstantValue kind: " + ((ExprTy.Constant) e).value.kind);
4274-
}
4275-
}
4276-
return null;
4277-
}
4278-
4279-
private void checkCaller(ExprTy e) {
4280-
if (e instanceof ExprTy.Constant || e instanceof ExprTy.Tuple || e instanceof ExprTy.List || e instanceof ExprTy.ListComp || e instanceof ExprTy.Dict || e instanceof ExprTy.DictComp ||
4281-
e instanceof ExprTy.Set || e instanceof ExprTy.SetComp || e instanceof ExprTy.GeneratorExp || e instanceof ExprTy.JoinedStr || e instanceof ExprTy.FormattedValue) {
4282-
warn(e, "'%s' object is not callable; perhaps you missed a comma?", inferType(e).getName());
4283-
}
4284-
}
4285-
4286-
private void checkSubscripter(ExprTy e) {
4287-
if (e instanceof ExprTy.Constant) {
4288-
switch (((ExprTy.Constant) e).value.kind) {
4289-
case NONE:
4290-
case ELLIPSIS:
4291-
case BOOLEAN:
4292-
case LONG:
4293-
case BIGINTEGER:
4294-
case DOUBLE:
4295-
case COMPLEX:
4296-
case FROZENSET:
4297-
break;
4298-
default:
4299-
return;
4300-
}
4301-
} else if (!(e instanceof ExprTy.Set || e instanceof ExprTy.SetComp || e instanceof ExprTy.GeneratorExp || e instanceof ExprTy.Lambda)) {
4302-
return;
4303-
}
4304-
warn(e, "'%s' object is not subscriptable; perhaps you missed a comma?", inferType(e).getName());
4305-
}
4306-
4307-
private void checkIndex(ExprTy e, ExprTy s) {
4308-
PythonBuiltinClassType indexType = inferType(s);
4309-
if (indexType == null || indexType == PythonBuiltinClassType.Boolean || indexType == PythonBuiltinClassType.PInt || indexType == PythonBuiltinClassType.PSlice) {
4310-
return;
4311-
}
4312-
if (e instanceof ExprTy.Constant) {
4313-
switch (((ExprTy.Constant) e).value.kind) {
4314-
case CODEPOINTS:
4315-
case BYTES:
4316-
case TUPLE:
4317-
break;
4318-
default:
4319-
return;
4320-
}
4321-
} else if (!(e instanceof ExprTy.Tuple || e instanceof ExprTy.List || e instanceof ExprTy.ListComp || e instanceof ExprTy.JoinedStr || e instanceof ExprTy.FormattedValue)) {
4322-
return;
4323-
}
4324-
warn(e, "%s indices must be integers or slices, not %s; perhaps you missed a comma?", inferType(e).getName(), indexType.getName());
4325-
}
4326-
43274167
public static Parser createParser(String src, ParserCallbacks errorCb, InputType inputType, boolean interactiveTerminal, boolean allowIncompleteInput) {
43284168
EnumSet<AbstractParser.Flags> flags = EnumSet.noneOf(AbstractParser.Flags.class);
43294169
if (interactiveTerminal) {

0 commit comments

Comments
 (0)