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
19 changes: 19 additions & 0 deletions graalpython/com.oracle.graal.python.test/src/tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@
import types


def assert_raises(err, fn, *args, **kwargs):
raised = False
try:
fn(*args, **kwargs)
except err:
raised = True
assert raised


def test_incomplete_input_error():
flags = 0x4000 | 0x200 # PyCF_ALLOW_INCOMPLETE_INPUT | PyCF_DONT_IMPLY_DEDENT

Expand Down Expand Up @@ -152,6 +161,16 @@ def newhook(value):
sys.displayhook = oldhook


def test_compile_checks_mode_before_source():
assert_raises(ValueError, compile, None, "<input>", "x")
assert_raises(ValueError, compile, "\x00", "<input>", "x")
assert_raises(ValueError, compile, b"\xff", "<input>", "x")
assert_raises(ValueError, compile, b"\x00", "<input>", "x")
assert_raises(ValueError, compile, None, "<input>", "x", ast.PyCF_ONLY_AST)
assert_raises(ValueError, compile, None, "<input>", "func_type")
assert_raises(ValueError, compile, ast.parse("1"), "<input>", "func_type")


def test_underscore_in_numbers():
assert eval('1_0') == 10
assert eval('0b1_1') == 0b11
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1011,16 +1011,11 @@ Object doCompile(VirtualFrame frame, TruffleString expression, TruffleString fil
Object compile(TruffleString expression, TruffleString filename, TruffleString mode, int flags, @SuppressWarnings("unused") boolean dontInherit, int optimize, int featureVersion) {
checkFlags(flags);
checkOptimize(optimize, optimize);
InputType type = getParserInputType(mode, flags);
checkSource(expression);

TruffleString code = expression;
PythonContext context = getContext();
InputType type = getParserInputType(mode, flags);
if (type == InputType.FUNCTION_TYPE) {
if ((flags & PyCF_ONLY_AST) == 0) {
throw PRaiseNode.raiseStatic(this, ValueError, ErrorMessages.COMPILE_MODE_FUNC_TYPE_REQUIED_FLAG_ONLY_AST);
}
}
if (lstrip && !code.isEmpty()) {
int c = code.codePointAtIndexUncached(0, TS_ENCODING);
if (c == ' ' || c == '\t') {
Expand Down Expand Up @@ -1087,11 +1082,12 @@ Object generic(VirtualFrame frame, Object wSource, Object wFilename, TruffleStri
}
checkFlags(flags);
checkOptimize(optimize, optimize);
InputType type = getParserInputType(mode, flags);

Object saved = BoundaryCallContext.enter(frame, boundaryCallData);
try {
if (AstModuleBuiltins.isAst(context, wSource)) {
ModTy mod = AstModuleBuiltins.obj2sst(inliningTarget, context, wSource, getParserInputType(mode, flags));
ModTy mod = AstModuleBuiltins.obj2sst(inliningTarget, context, wSource, type);
if ((flags & PyCF_ONLY_AST) != 0) {
if ((flags & PyCF_OPTIMIZED_AST) != PyCF_OPTIMIZED_AST) {
return wSource;
Expand Down Expand Up @@ -1156,6 +1152,9 @@ private InputType getParserInputType(TruffleString mode, int flags) {
} else if (mode.equalsUncached(StringLiterals.T_SINGLE, TS_ENCODING)) {
return InputType.SINGLE;
} else if (mode.equalsUncached(StringLiterals.T_FUNC_TYPE, TS_ENCODING)) {
if ((flags & PyCF_ONLY_AST) == 0) {
throw PRaiseNode.raiseStatic(this, ValueError, ErrorMessages.COMPILE_MODE_FUNC_TYPE_REQUIED_FLAG_ONLY_AST);
}
return InputType.FUNCTION_TYPE;
} else {
if ((flags & PyCF_ONLY_AST) != 0) {
Expand Down