Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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]", "<test>", "exec")

def test_starred_expr_no_iterable(self):
self.assertRaises(TypeError, lambda : [1, *None])
self.assertRaises(TypeError, lambda : [1, *1])
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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,
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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__);
Expand All @@ -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;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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}.
Expand Down
Loading