Skip to content

Commit c69b88c

Browse files
committed
[GR-68779] Move Builtin annotation to the annotations package
PullRequest: graalpython/3971
2 parents 7e69e05 + 778c2fb commit c69b88c

File tree

297 files changed

+560
-559
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

297 files changed

+560
-559
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/Builtin.java renamed to graalpython/com.oracle.graal.python.annotations/src/com/oracle/graal/python/annotations/Builtin.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,12 @@
2323
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
2424
* OF THE POSSIBILITY OF SUCH DAMAGE.
2525
*/
26-
package com.oracle.graal.python.builtins;
26+
package com.oracle.graal.python.annotations;
2727

2828
import java.lang.annotation.Repeatable;
2929
import java.lang.annotation.Retention;
3030
import java.lang.annotation.RetentionPolicy;
3131

32-
import com.oracle.graal.python.nodes.StringLiterals;
33-
3432
@Retention(RetentionPolicy.RUNTIME)
3533
@Repeatable(value = Builtins.class)
3634
public @interface Builtin {
@@ -89,17 +87,17 @@
8987
*/
9088
boolean declaresExplicitSelf() default false;
9189

92-
String raiseErrorName() default StringLiterals.J_EMPTY_STRING;
90+
String raiseErrorName() default "";
9391

9492
boolean forceSplitDirectCalls() default false;
9593

9694
/**
9795
* If set to {@code true}, then this builtin will be initialized and registered in
98-
* {@link PythonBuiltins#initialize(Python3Core)}. Otherwise, it will be ignored even when it
99-
* has a generated node factory. This is useful when we want to initialize the builtin manually
100-
* in different way (e.g., wrap it in method, descriptor, ...). By convention set this to
101-
* {@code false} also for builtins declared outside of {@link PythonBuiltins} subclass to
102-
* document the intent to not initialize them automatically.
96+
* PythonBuiltins#initialize. Otherwise, it will be ignored even when it has a generated node
97+
* factory. This is useful when we want to initialize the builtin manually in different way
98+
* (e.g., wrap it in method, descriptor, ...). By convention set this to {@code false} also for
99+
* builtins declared outside of PythonBuiltins subclass to document the intent to not initialize
100+
* them automatically.
103101
*/
104102
boolean autoRegister() default true;
105103
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/Builtins.java renamed to graalpython/com.oracle.graal.python.annotations/src/com/oracle/graal/python/annotations/Builtins.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2020, 2025, 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
@@ -38,7 +38,7 @@
3838
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3939
* SOFTWARE.
4040
*/
41-
package com.oracle.graal.python.builtins;
41+
package com.oracle.graal.python.annotations;
4242

4343
import java.lang.annotation.Retention;
4444
import java.lang.annotation.RetentionPolicy;
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright (c) 2021, 2025, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* The Universal Permissive License (UPL), Version 1.0
6+
*
7+
* Subject to the condition set forth below, permission is hereby granted to any
8+
* person obtaining a copy of this software, associated documentation and/or
9+
* data (collectively the "Software"), free of charge and under any and all
10+
* copyright rights in the Software, and any and all patent rights owned or
11+
* freely licensable by each licensor hereunder covering either (i) the
12+
* unmodified Software as contributed to or provided by such licensor, or (ii)
13+
* the Larger Works (as defined below), to deal in both
14+
*
15+
* (a) the Software, and
16+
*
17+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
18+
* one is included with the Software each a "Larger Work" to which the Software
19+
* is contributed by such licensors),
20+
*
21+
* without restriction, including without limitation the rights to copy, create
22+
* derivative works of, display, perform, and distribute the Software and make,
23+
* use, sell, offer for sale, import, export, have made, and have sold the
24+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
25+
* either these or other terms.
26+
*
27+
* This license is subject to the following condition:
28+
*
29+
* The above copyright notice and either this complete permission notice or at a
30+
* minimum a reference to the UPL must be included in all copies or substantial
31+
* portions of the Software.
32+
*
33+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39+
* SOFTWARE.
40+
*/
41+
package com.oracle.graal.python.annotations;
42+
43+
import java.util.Locale;
44+
45+
public enum PythonOS {
46+
PLATFORM_LINUX("linux", "Linux"),
47+
PLATFORM_DARWIN("darwin", "Darwin"),
48+
PLATFORM_WIN32("win32", "Windows"),
49+
PLATFORM_ANY(null, null);
50+
51+
public static final String SUPPORTED_PLATFORMS = "linux/amd64, linux/aarch64, macos/amd64, macos/aarch64, and windows/amd64";
52+
53+
private final String name;
54+
private final String uname;
55+
56+
PythonOS(String name, String uname) {
57+
this.name = name;
58+
this.uname = uname;
59+
}
60+
61+
public String getName() {
62+
return name;
63+
}
64+
65+
public String getUname() {
66+
return uname;
67+
}
68+
69+
public static final PythonOS internalCurrent;
70+
71+
static {
72+
String property = System.getProperty("os.name", "").toLowerCase(Locale.ROOT);
73+
if (property.contains("linux")) {
74+
internalCurrent = PLATFORM_LINUX;
75+
} else if (property.contains("mac") || property.contains("darwin")) {
76+
internalCurrent = PLATFORM_DARWIN;
77+
} else if (property.contains("windows")) {
78+
internalCurrent = PLATFORM_WIN32;
79+
} else {
80+
internalCurrent = PLATFORM_ANY;
81+
}
82+
}
83+
84+
public static boolean isUnsupported() {
85+
return internalCurrent == PLATFORM_ANY;
86+
}
87+
88+
}

graalpython/com.oracle.graal.python.frozen/freeze_modules.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -543,8 +543,10 @@ def lower_camel_case(str):
543543
*/
544544
package com.oracle.graal.python.builtins.objects.module;
545545
546-
import com.oracle.graal.python.builtins.PythonOS;
546+
import com.oracle.graal.python.PythonLanguage;
547+
import com.oracle.graal.python.annotations.PythonOS;
547548
549+
/** GENERATED BY graalpython/com.oracle.graal.python.frozen/freeze_modules.py */
548550
public final class FrozenModules {"""
549551

550552

@@ -581,7 +583,7 @@ def write_frozen_lookup(out_file, modules):
581583
)
582584
elif module.name == "os.path": # Special case for os.path
583585
out_file.write(u' case "os.path":\n')
584-
out_file.write(u' return PythonOS.getPythonOS() != PythonOS.PLATFORM_WIN32 ? Map.POSIXPATH : Map.NTPATH;\n')
586+
out_file.write(u' return PythonLanguage.getPythonOS() != PythonOS.PLATFORM_WIN32 ? Map.POSIXPATH : Map.NTPATH;\n')
585587
else:
586588
out_file.write(f' case "{module.name}":\n')
587589
out_file.write(f" return Map.{module.symbol};\n")

graalpython/com.oracle.graal.python.processor/src/com/oracle/graal/python/processor/ArgumentClinicProcessor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@
8282

8383
public class ArgumentClinicProcessor extends AbstractProcessor {
8484
private static final boolean LOGGING = false;
85-
private static final String BuiltinAnnotationClass = "com.oracle.graal.python.builtins.Builtin";
85+
private static final String BuiltinAnnotationClass = "com.oracle.graal.python.annotations.Builtin";
8686
private static final String SlotSignatureAnnotationClass = "com.oracle.graal.python.annotations.Slot.SlotSignature";
87-
private static final String BuiltinsAnnotationClass = "com.oracle.graal.python.builtins.Builtins";
87+
private static final String BuiltinsAnnotationClass = "com.oracle.graal.python.annotations.Builtins";
8888
private static final String BUILTINS_BASE_CLASSES_PACKAGE = "com.oracle.graal.python.nodes.function.builtins";
8989

9090
private Element[] clinicBuiltinBaseClasses;

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/SocketTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
import java.util.stream.Collectors;
9898
import java.util.stream.Stream;
9999

100+
import com.oracle.graal.python.PythonLanguage;
100101
import org.junit.Assert;
101102
import org.junit.Before;
102103
import org.junit.Rule;
@@ -108,7 +109,7 @@
108109
import org.junit.runners.Parameterized.Parameter;
109110
import org.junit.runners.Parameterized.Parameters;
110111

111-
import com.oracle.graal.python.builtins.PythonOS;
112+
import com.oracle.graal.python.annotations.PythonOS;
112113
import com.oracle.graal.python.builtins.objects.exception.OSErrorEnum;
113114
import com.oracle.graal.python.runtime.PosixConstants.MandatoryIntConstant;
114115
import com.oracle.graal.python.runtime.PosixSupportLibrary;
@@ -661,7 +662,7 @@ public void dgramSelect() throws PosixException {
661662

662663
@Test
663664
public void streamSelect() throws PosixException {
664-
if (PythonOS.getPythonOS() == PythonOS.PLATFORM_DARWIN) {
665+
if (PythonLanguage.getPythonOS() == PythonOS.PLATFORM_DARWIN) {
665666
// transiently fails on darwin, skip
666667
return;
667668
}

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/builtin/modules/ClinicTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
import com.oracle.graal.python.annotations.ClinicConverterFactory.BuiltinName;
5858
import com.oracle.graal.python.annotations.ClinicConverterFactory.DefaultValue;
5959
import com.oracle.graal.python.annotations.ClinicConverterFactory.UseDefaultForNone;
60-
import com.oracle.graal.python.builtins.Builtin;
60+
import com.oracle.graal.python.annotations.Builtin;
6161
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
6262
import com.oracle.graal.python.builtins.objects.PNone;
6363
import com.oracle.graal.python.builtins.objects.function.PArguments;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/PythonLanguage.java

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@
2525
*/
2626
package com.oracle.graal.python;
2727

28-
import static com.oracle.graal.python.builtins.PythonOS.PLATFORM_WIN32;
29-
import static com.oracle.graal.python.builtins.PythonOS.getPythonOS;
28+
import static com.oracle.graal.python.annotations.PythonOS.PLATFORM_WIN32;
3029
import static com.oracle.graal.python.nodes.BuiltinNames.T__SIGNAL;
3130
import static com.oracle.graal.python.nodes.StringLiterals.J_PY_EXTENSION;
3231
import static com.oracle.graal.python.nodes.StringLiterals.T_PY_EXTENSION;
@@ -51,15 +50,17 @@
5150
import java.util.concurrent.Semaphore;
5251
import java.util.logging.Level;
5352

53+
import com.oracle.truffle.api.exception.AbstractTruffleException;
5454
import org.graalvm.home.Version;
55+
import org.graalvm.nativeimage.ImageInfo;
5556
import org.graalvm.options.OptionDescriptors;
5657
import org.graalvm.options.OptionKey;
5758
import org.graalvm.options.OptionValues;
5859
import org.graalvm.polyglot.SandboxPolicy;
5960

6061
import com.oracle.graal.python.builtins.Python3Core;
6162
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
62-
import com.oracle.graal.python.builtins.PythonOS;
63+
import com.oracle.graal.python.annotations.PythonOS;
6364
import com.oracle.graal.python.builtins.modules.MarshalModuleBuiltins;
6465
import com.oracle.graal.python.builtins.modules.SignalModuleBuiltins;
6566
import com.oracle.graal.python.builtins.objects.PNone;
@@ -1225,4 +1226,51 @@ public Source getOrCreateSource(Function<Object, Source> rootNodeFunction, Objec
12251226
CompilerAsserts.neverPartOfCompilation();
12261227
return sourceCache.computeIfAbsent(key, rootNodeFunction);
12271228
}
1229+
1230+
public static PythonOS getPythonOS() {
1231+
if (PythonOS.internalCurrent == PythonOS.PLATFORM_ANY) {
1232+
if (ImageInfo.inImageBuildtimeCode()) {
1233+
throw new RuntimeException("Native images with GraalPy are only supported on " + PythonOS.SUPPORTED_PLATFORMS + ".");
1234+
}
1235+
String emulated = get(null).getEngineOption(PythonOptions.UnsupportedPlatformEmulates);
1236+
if (!emulated.isEmpty()) {
1237+
switch (emulated) {
1238+
case "linux":
1239+
return PythonOS.PLATFORM_LINUX;
1240+
case "macos":
1241+
return PythonOS.PLATFORM_DARWIN;
1242+
case "windows":
1243+
return PLATFORM_WIN32;
1244+
default:
1245+
throw new UnsupportedPlatform("UnsupportedPlatformEmulates must be exactly one of \"linux\", \"macos\", or \"windows\"");
1246+
}
1247+
} else {
1248+
throw new UnsupportedPlatform("This platform is not currently supported. " +
1249+
"Currently supported platforms are " + PythonOS.SUPPORTED_PLATFORMS + ". " +
1250+
"If you are running on one of these platforms and are receiving this error, that indicates a bug in this build of GraalPy. " +
1251+
"If you are running on a different platform and accept that any functionality that interacts with the system may be " +
1252+
"incorrect and Python native extensions will not work, you can specify the system property \"UnsupportedPlatformEmulates\" " +
1253+
"with a value of either \"linux\", \"macos\", or \"windows\" to continue further and have GraalPy behave as if it were running on " +
1254+
"the OS specified. Loading native libraries will not work and must be disabled using the context options. " +
1255+
"See https://www.graalvm.org/python/docs/ for details on GraalPy modules with both native and Java backends, " +
1256+
"and https://www.graalvm.org/truffle/javadoc/org/graalvm/polyglot/Context.Builder.html to learn about disallowing native access.");
1257+
}
1258+
}
1259+
return PythonOS.internalCurrent;
1260+
}
1261+
1262+
public static void throwIfUnsupported(String msg) {
1263+
if (PythonOS.isUnsupported()) {
1264+
throw new UnsupportedPlatform(msg +
1265+
"\nThis point was reached as earlier platform checks were overridden using the system property UnsupportedPlatformEmulates");
1266+
}
1267+
}
1268+
1269+
public static final class UnsupportedPlatform extends AbstractTruffleException {
1270+
public UnsupportedPlatform(String msg) {
1271+
super(msg);
1272+
}
1273+
1274+
private static final long serialVersionUID = 1L;
1275+
}
12281276
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/CoreFunctions.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
*/
2626
package com.oracle.graal.python.builtins;
2727

28+
import com.oracle.graal.python.annotations.PythonOS;
29+
2830
import java.lang.annotation.Retention;
2931
import java.lang.annotation.RetentionPolicy;
3032

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/Python3Core.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
import java.util.logging.Level;
6565

6666
import com.oracle.graal.python.PythonLanguage;
67+
import com.oracle.graal.python.annotations.PythonOS;
6768
import com.oracle.graal.python.builtins.modules.AbcModuleBuiltins;
6869
import com.oracle.graal.python.builtins.modules.ArrayModuleBuiltins;
6970
import com.oracle.graal.python.builtins.modules.AsyncioModuleBuiltins;
@@ -441,7 +442,7 @@ private static TruffleString[] getCoreFiles() {
441442
T__SYSCONFIG,
442443
T_JAVA,
443444
toTruffleStringUncached("pip_hook"));
444-
if (PythonOS.getPythonOS() == PythonOS.PLATFORM_WIN32) {
445+
if (PythonLanguage.getPythonOS() == PythonOS.PLATFORM_WIN32) {
445446
coreFiles = new ArrayList<>(coreFiles);
446447
coreFiles.add(toTruffleStringUncached("_nt"));
447448
}
@@ -463,7 +464,7 @@ private static TruffleString[] getCoreFiles() {
463464
}
464465

465466
private static void filterBuiltins(List<PythonBuiltins> builtins) {
466-
PythonOS currentOs = PythonOS.getPythonOS();
467+
PythonOS currentOs = PythonLanguage.getPythonOS();
467468
List<PythonBuiltins> toRemove = new ArrayList<>();
468469
for (PythonBuiltins builtin : builtins) {
469470
if (builtin == null) {

0 commit comments

Comments
 (0)