Skip to content

Commit 923e2d9

Browse files
committed
[GR-34604] Improve compatibility with package setuptools
PullRequest: graalpython/2107
2 parents 93313f8 + c80c28a commit 923e2d9

File tree

13 files changed

+169
-24
lines changed

13 files changed

+169
-24
lines changed

graalpython/com.oracle.graal.python.parser.antlr/postprocess.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
1+
# Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved.
22
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
33
#
44
# The Universal Permissive License (UPL), Version 1.0
@@ -43,7 +43,7 @@
4343

4444
COPYRIGHT_HEADER = """\
4545
/*
46-
* Copyright (c) 2017-2021, Oracle and/or its affiliates.
46+
* Copyright (c) 2017-2022, Oracle and/or its affiliates.
4747
* Copyright (c) 2014 by Bart Kiers
4848
*
4949
* The MIT License (MIT)

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/parser/FuncDefTests.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -203,6 +203,11 @@ public void functionDef22() throws Exception {
203203
"print(test())\n");
204204
}
205205

206+
@Test
207+
public void annotations01() throws Exception {
208+
checkScopeAndTree();
209+
}
210+
206211
@Test
207212
public void decorator01() throws Exception {
208213
checkScopeAndTree();
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from __future__ import annotations
2+
3+
4+
def b(x: int | str) -> int | str:
5+
return 10
6+
7+
print(b.__annotations__)
8+
9+
x: int | str = 10
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Scope: []
2+
Kind: Module
3+
FrameDescriptor: Empty
4+
CellVars: Empty
5+
FreeVars: Empty
6+
Scope: b
7+
Kind: Function
8+
FrameDescriptor: [x, <return_val>]
9+
CellVars: Empty
10+
FreeVars: Empty
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
ModuleRootNode Name: <module 'annotations01'> SourceSection: [0,130]`from __future__ impo...`
2+
Signature: varArgs=False, varKeywordArgs=False, noArguments=True, positionalOnly=True, requiresKeywordArgs=False
3+
FreeVars: None
4+
NeedsCellFrame: False
5+
FrameDescriptor: Empty
6+
Documentation: None
7+
InnerRootNode SourceSection: [0,130]`from __future__ impo...`
8+
ExpressionWithSideEffects SourceSection: [0,129]`from __future__ impo...`
9+
ImportFromNode SourceSection: [0,34]`from __future__ impo...`
10+
Importee: __future__
11+
Level: 0
12+
FromList: annotations
13+
WriteNameNodeGen SourceSection: None
14+
Identifier: annotations
15+
EmptyNode SourceSection: None
16+
IsBuiltinClassProfile SourceSection: None
17+
GetClassNodeGen SourceSection: None
18+
IsBuiltinClassProfile SourceSection: None
19+
GetClassNodeGen SourceSection: None
20+
WriteNameNodeGen SourceSection: [37,86]`def b(x: int | str) ...`
21+
Identifier: b
22+
FunctionDefinitionNode Name: b SourceSection: None
23+
Arguments: None
24+
KwArguments: None
25+
Documentation: None
26+
FreeVarSlots: None
27+
ExecutionSlots:
28+
FreeVarsSlots: None
29+
CellVarsSlots: None
30+
FunctionRootNode SourceSection: [37,86]`def b(x: int | str) ...`
31+
Name: b
32+
Signature: varArgs=False, varKeywordArgs=False, noArguments=False, positionalOnly=True, requiresKeywordArgs=False
33+
Param Names: x
34+
CelVars: None
35+
FreeVars: None
36+
NeedsCellFrame: False
37+
FrameDescriptor: 2 slots [x, <return_val>]
38+
ExecutionSlots:
39+
FreeVarsSlots: None
40+
CellVarsSlots: None
41+
InnerRootNode SourceSection: [37,86]`def b(x: int | str) ...`
42+
ReturnTargetNode SourceSection: [37,86]`def b(x: int | str) ...`
43+
Body: BlockNode SourceSection: None
44+
WriteLocalVariableNodeGen SourceSection: None
45+
Identifier: x
46+
Frame: [0,x,Illegal]
47+
ArgumentExpressionNode SourceSection: None
48+
ReadIndexedArgumentNodeGen SourceSection: None
49+
Index: 0
50+
FunctionBodyNode SourceSection: [75,84]`return 10`
51+
FrameReturnNode SourceSection: [75,84]`return 10`
52+
IntegerLiteralNode SourceSection: [82,84]`10`
53+
Value: 10
54+
Return Expresssion: ReadLocalVariableNodeGen SourceSection: None
55+
Frame: [1,<return_val>,Object]
56+
ExpressionStatementNode SourceSection: [86,110]`print(b.__annotation...`
57+
PythonCallUnary SourceSection: [86,110]`print(b.__annotation...`
58+
CallUnaryMethodNodeGen SourceSection: None
59+
ReadNameNodeGen SourceSection: [86,91]`print`
60+
Identifier: print
61+
IsBuiltinClassProfile SourceSection: None
62+
GetClassNodeGen SourceSection: None
63+
GetAttributeNode SourceSection: [92,109]`b.__annotations__`
64+
GetFixedAttributeNodeGen SourceSection: None
65+
Key: __annotations__
66+
LookupAndCallNonReversibleBinaryNodeGen SourceSection: None
67+
Op: __getattribute__
68+
ReadNameNodeGen SourceSection: [92,93]`b`
69+
Identifier: b
70+
IsBuiltinClassProfile SourceSection: None
71+
GetClassNodeGen SourceSection: None
72+
ExpressionWithSideEffects SourceSection: None
73+
WriteNameNodeGen SourceSection: [112,129]`x: int | str = 10`
74+
Identifier: x
75+
IntegerLiteralNode SourceSection: [127,129]`10`
76+
Value: 10
77+
SetItemNodeGen SourceSection: [0,0]Empty
78+
ReadNameNodeGen SourceSection: None
79+
Identifier: __annotations__
80+
IsBuiltinClassProfile SourceSection: None
81+
GetClassNodeGen SourceSection: None
82+
StringLiteralNode SourceSection: [0,0]Empty
83+
StringLiteralNode SourceSection: [115,124]`int | str`
84+
EmptyNode SourceSection: None

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -269,4 +269,17 @@ Object setSentinel() {
269269
return sentinelLock;
270270
}
271271
}
272+
273+
@Builtin(name = "interrupt_main", minNumOfPositionalArgs = 1, declaresExplicitSelf = true, doc = "interrupt_main()\n" +
274+
"\n" +
275+
"Raise a KeyboardInterrupt in the main thread.\n" +
276+
"A subthread can use this function to interrupt the main thread.")
277+
@GenerateNodeFactory
278+
abstract static class InterruptMainThreadNode extends PythonUnaryBuiltinNode {
279+
@Specialization
280+
Object getCount(@SuppressWarnings("unused") PythonModule self) {
281+
// TODO: implement me
282+
return PNone.NONE;
283+
}
284+
}
272285
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/BuiltinNames.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -54,6 +54,7 @@ public abstract class BuiltinNames {
5454
public static final String __MAIN__ = "__main__";
5555
public static final String __BUILTINS__ = "__builtins__";
5656
public static final String __DEBUG__ = "__debug__";
57+
public static final String __FUTURE__ = "__future__";
5758

5859
// sys
5960
public static final String TRACEBACKLIMIT = "tracebacklimit";

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/parser/PythonSSTNodeFactory.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
import com.oracle.graal.python.parser.ScopeInfo.ScopeKind;
6464
import com.oracle.graal.python.parser.sst.AnnAssignmentSSTNode;
6565
import com.oracle.graal.python.parser.sst.AnnotationSSTNode;
66+
import com.oracle.graal.python.parser.sst.ArgDefListBuilder;
6667
import com.oracle.graal.python.parser.sst.ArgListBuilder;
6768
import com.oracle.graal.python.parser.sst.AssignmentSSTNode;
6869
import com.oracle.graal.python.parser.sst.AugAssignmentSSTNode;
@@ -103,6 +104,8 @@
103104
import com.oracle.truffle.api.source.Source;
104105
import com.oracle.truffle.api.source.SourceSection;
105106

107+
import static com.oracle.graal.python.nodes.BuiltinNames.__FUTURE__;
108+
106109
public final class PythonSSTNodeFactory {
107110

108111
/**
@@ -117,6 +120,7 @@ public interface FStringExprParser {
117120
private final Source source;
118121
private final PythonParser.ParserErrorCallback errors;
119122
private FStringExprParser fStringExprParser;
123+
private boolean futureAnnotations = false;
120124

121125
public PythonSSTNodeFactory(PythonParser.ParserErrorCallback errors, Source source, FStringExprParser fStringExprParser) {
122126
this.errors = errors;
@@ -162,6 +166,9 @@ public SSTNode createImport(String name, String asName, int startOffset, int end
162166
public SSTNode createImportFrom(String from, String[][] asNames, int startOffset, int endOffset) {
163167
if (asNames != null) {
164168
for (String[] asName : asNames) {
169+
if (__FUTURE__.equals(from) && asName[0].equals("annotations")) {
170+
futureAnnotations = true;
171+
}
165172
scopeEnvironment.createLocal(asName[1] == null ? asName[0] : asName[1]);
166173
}
167174
} else {
@@ -173,6 +180,21 @@ public SSTNode createImportFrom(String from, String[][] asNames, int startOffset
173180
return new ImportFromSSTNode(scopeEnvironment.getCurrentScope(), from, asNames, startOffset, endOffset);
174181
}
175182

183+
public SSTNode createAnnotationType(SSTNode type) {
184+
SSTNode annotType = type;
185+
if (futureAnnotations && type != null) {
186+
final String value = source.getCharacters().subSequence(type.getStartOffset() - 1, type.getEndOffset() + 1).toString();
187+
annotType = createStringLiteral(new String[]{value}, type.getStartOffset(), type.getEndOffset());
188+
}
189+
return annotType;
190+
}
191+
192+
public FunctionDefSSTNode createFunctionDef(ScopeInfo functionScope, String name, String enclosingClassName, ArgDefListBuilder argBuilder, SSTNode body, SSTNode resultAnnotation, int startOffset,
193+
int endOffset) {
194+
SSTNode annotation = createAnnotationType(resultAnnotation);
195+
return new FunctionDefSSTNode(functionScope, name, enclosingClassName, argBuilder, body, annotation, startOffset, endOffset);
196+
}
197+
176198
public String mangleNameInCurrentScope(String name) {
177199
if (cannotBeMangled(name)) {
178200
return name;
@@ -339,6 +361,7 @@ public SSTNode createAnnAssignment(AnnotationSSTNode annotation, SSTNode rhs, in
339361
}
340362

341363
public AnnotationSSTNode createAnnotation(SSTNode lhs, SSTNode type, int start, int end) {
364+
SSTNode annotType = createAnnotationType(type);
342365
// checking if the annotation has the right target
343366
if (!(lhs instanceof VarLookupSSTNode || lhs instanceof GetAttributeSSTNode || lhs instanceof SubscriptSSTNode)) {
344367
if (lhs instanceof CollectionSSTNode) {
@@ -354,7 +377,7 @@ public AnnotationSSTNode createAnnotation(SSTNode lhs, SSTNode type, int start,
354377
if (!scopeEnvironment.getCurrentScope().hasAnnotations()) {
355378
scopeEnvironment.getCurrentScope().setHasAnnotations(true);
356379
}
357-
return new AnnotationSSTNode(lhs, type, start, end);
380+
return new AnnotationSSTNode(lhs, annotType, start, end);
358381
}
359382

360383
public SSTNode createAugAssignment(SSTNode lhs, String operation, SSTNode rhs, int startOffset, int endOffset) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/parser/antlr/Python3.g4

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017-2020, Oracle and/or its affiliates.
2+
* Copyright (c) 2017-2022, Oracle and/or its affiliates.
33
* Copyright (c) 2014 by Bart Kiers
44
*
55
* The MIT License (MIT)
@@ -580,11 +580,11 @@ funcdef
580580
}
581581
s = suite
582582
{
583-
SSTNode funcDef = new FunctionDefSSTNode(scopeEnvironment.getCurrentScope(), name, enclosingClassName, $parameters.result, optimize($s.result), resultType, getStartIndex(_localctx), getStopIndex(((FuncdefContext)_localctx).s));
584-
scopeEnvironment.popScope();
585-
loopState = savedLoopState;
586-
push(funcDef);
587-
}
583+
SSTNode funcDef = factory.createFunctionDef(scopeEnvironment.getCurrentScope(), name, enclosingClassName, $parameters.result, optimize($s.result), resultType, getStartIndex(_localctx), getStopIndex(((FuncdefContext)_localctx).s));
584+
scopeEnvironment.popScope();
585+
loopState = savedLoopState;
586+
push(funcDef);
587+
}
588588
;
589589

590590
parameters returns [ArgDefListBuilder result]
@@ -639,7 +639,7 @@ defparameter [ArgDefListBuilder args]
639639
if (name != null) {
640640
name = factory.mangleNameInCurrentScope(name);
641641
}
642-
ArgDefListBuilder.AddParamResult result = args.addParam(name, type, defValue);
642+
ArgDefListBuilder.AddParamResult result = args.addParam(name, factory.createAnnotationType(type), defValue);
643643
switch(result) {
644644
case NONDEFAULT_FOLLOWS_DEFAULT:
645645
throw new PythonRecognitionException("non-default argument follows default argument", this, _input, $ctx, getCurrentToken());

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/parser/antlr/Python3Lexer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017-2021, Oracle and/or its affiliates.
2+
* Copyright (c) 2017-2022, Oracle and/or its affiliates.
33
* Copyright (c) 2014 by Bart Kiers
44
*
55
* The MIT License (MIT)

0 commit comments

Comments
 (0)