diff --git a/graalpython/com.oracle.graal.python.test/src/tests/test_metaclass.py b/graalpython/com.oracle.graal.python.test/src/tests/test_metaclass.py index b9840634e5..0a1a1b8a6f 100644 --- a/graalpython/com.oracle.graal.python.test/src/tests/test_metaclass.py +++ b/graalpython/com.oracle.graal.python.test/src/tests/test_metaclass.py @@ -140,3 +140,10 @@ def test_nested_class(): assert A.B.__name__ == "B" assert A.B.__qualname__ == "A.B" assert A.B.__module__ == __name__ + + +def test_type_new_with_non_string_module_name(): + module_name = object() + namespace = {"__name__": module_name} + exec("class Meta(type): pass\ncreated = Meta('', (), {})", namespace) + assert namespace["created"].__module__ is module_name diff --git a/graalpython/com.oracle.graal.python.test/src/tests/test_parser.py b/graalpython/com.oracle.graal.python.test/src/tests/test_parser.py index 044285e870..681cc3df42 100644 --- a/graalpython/com.oracle.graal.python.test/src/tests/test_parser.py +++ b/graalpython/com.oracle.graal.python.test/src/tests/test_parser.py @@ -259,6 +259,9 @@ def test_cannot_assign_other(): assert_raise_syntax_error("def f(**kwargs:(lambda __debug__:0)): pass\n", "cannot assign to __debug__") assert_raise_syntax_error("def f(**__debug__): pass\n", "cannot assign to __debug__") assert_raise_syntax_error("def f(*xx, __debug__): pass\n", "cannot assign to __debug__") + assert_raise_syntax_error("def f[__debug__](): pass\n", "cannot assign to __debug__") + assert_raise_syntax_error("def f[*__debug__](): pass\n", "cannot assign to __debug__") + assert_raise_syntax_error("def f[**__debug__](): pass\n", "cannot assign to __debug__") def test_invalid_assignmetn_to_yield_expression(): if sys.implementation.version.minor >= 8: diff --git a/graalpython/com.oracle.graal.python.test/src/tests/test_starred_expr.py b/graalpython/com.oracle.graal.python.test/src/tests/test_starred_expr.py index 58f1baf34b..3ab105fc66 100644 --- a/graalpython/com.oracle.graal.python.test/src/tests/test_starred_expr.py +++ b/graalpython/com.oracle.graal.python.test/src/tests/test_starred_expr.py @@ -1,4 +1,4 @@ -# Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved. +# Copyright (c) 2020, 2026, Oracle and/or its affiliates. All rights reserved. # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. # # The Universal Permissive License (UPL), Version 1.0 @@ -73,6 +73,10 @@ def bar(): class TestStarredExprErrors(unittest.TestCase): + def test_standalone_starred_assignment_target(self): + with self.assertRaisesRegex(SyntaxError, "starred assignment target must be in a list or tuple"): + compile("*a = [1]", "", "exec") + def test_starred_expr_no_iterable(self): self.assertRaises(TypeError, lambda : [1, *None]) self.assertRaises(TypeError, lambda : [1, *1]) diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/type/TypeNodes.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/type/TypeNodes.java index c98001fafd..bd90546240 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/type/TypeNodes.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/type/TypeNodes.java @@ -1946,7 +1946,6 @@ public abstract static class CreateTypeNode extends Node { public abstract PythonClass execute(VirtualFrame frame, PDict namespaceOrig, TruffleString name, PTuple bases, Object metaclass, PKeyword[] kwds); @Child private ReadAttributeFromObjectNode readAttrNode; - @Child private CastToTruffleStringNode castToStringNode; private ReadAttributeFromObjectNode ensureReadAttrNode() { if (readAttrNode == null) { @@ -1956,14 +1955,6 @@ private ReadAttributeFromObjectNode ensureReadAttrNode() { return readAttrNode; } - private CastToTruffleStringNode ensureCastToStringNode() { - if (castToStringNode == null) { - CompilerDirectives.transferToInterpreterAndInvalidate(); - castToStringNode = insert(CastToTruffleStringNode.create()); - } - return castToStringNode; - } - @Specialization protected PythonClass makeType(VirtualFrame frame, PDict namespaceOrig, TruffleString name, PTuple bases, Object metaclass, PKeyword[] kwds, @Bind Node inliningTarget, @@ -1998,7 +1989,7 @@ protected PythonClass makeType(VirtualFrame frame, PDict namespaceOrig, TruffleS if (moduleAttr == PNone.NO_VALUE) { PythonObject globals = getGlobals.execute(frame, inliningTarget); if (globals != null) { - TruffleString moduleName = getModuleNameFromGlobals(inliningTarget, globals, getItemGlobals); + Object moduleName = getModuleNameFromGlobals(inliningTarget, globals, getItemGlobals); if (moduleName != null) { newType.setAttribute(SpecialAttributeNames.T___MODULE__, moduleName); } @@ -2104,7 +2095,7 @@ protected PythonClass makeType(VirtualFrame frame, PDict namespaceOrig, TruffleS return newType; } - private TruffleString getModuleNameFromGlobals(Node inliningTarget, PythonObject globals, HashingStorageGetItem getItem) { + private Object getModuleNameFromGlobals(Node inliningTarget, PythonObject globals, HashingStorageGetItem getItem) { Object nameAttr; if (globals instanceof PythonModule) { nameAttr = ensureReadAttrNode().execute(globals, SpecialAttributeNames.T___NAME__); @@ -2117,12 +2108,7 @@ private TruffleString getModuleNameFromGlobals(Node inliningTarget, PythonObject if (nameAttr == null || nameAttr == PNone.NO_VALUE) { return null; } - try { - return ensureCastToStringNode().executeCached(nameAttr); - } catch (CannotCastException e) { - CompilerDirectives.transferToInterpreterAndInvalidate(); - throw new IllegalStateException(); - } + return nameAttr; } } diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/compiler/bytecode_dsl/RootNodeCompiler.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/compiler/bytecode_dsl/RootNodeCompiler.java index 95daf47282..9e742e4d79 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/compiler/bytecode_dsl/RootNodeCompiler.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/compiler/bytecode_dsl/RootNodeCompiler.java @@ -3508,6 +3508,7 @@ public Void visit(StmtTy.Assert node) { public void visitTypeParams(TypeParamTy[] typeParams) { b.beginMakeTuple(); for (TypeParamTy typeParam : typeParams) { + currentLocation = typeParam.getSourceRange(); typeParam.accept(this); } b.endMakeTuple(); @@ -3565,6 +3566,11 @@ public Void visit(ExprTy.Subscript node) { return null; } + @Override + public Void visit(ExprTy.Starred node) { + throw ctx.errorCallback.onError(ErrorType.Syntax, node.getSourceRange(), "starred assignment target must be in a list or tuple"); + } + /** * This method unpacks the rhs (a sequence/iterable) to the elements on the lhs * specified by {@code nodes}.