Skip to content

Commit e7b7818

Browse files
committed
Skip or avoid windows jdk21 test paths that trigger native access
1 parent 2a0aaff commit e7b7818

13 files changed

Lines changed: 125 additions & 38 deletions

File tree

graalpython/com.oracle.graal.python.test/src/tests/test_indirect_call.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
import sys
4040
import os
4141

42-
from tests.cpyext import CPyExtTestCase, CPyExtType
4342
from tests.util import has_capi, needs_capi
4443

4544
# synchronize with Java implementation of __graalpython__.indirect_call_tester
@@ -72,6 +71,8 @@ def was_stack_walk(new_value):
7271

7372

7473
if has_capi():
74+
from tests.cpyext import CPyExtType
75+
7576
IndirectCApiCallTester = CPyExtType(
7677
'IndirectCApiCallTester',
7778
code='''

graalpython/com.oracle.graal.python.test/src/tests/test_startup.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,22 @@
4545

4646
# Both lists should remain as small as possible to avoid adding overhead to startup
4747
IS_WINDOWS = platform.system() == 'Windows'
48-
WINDOWS_CORE_MODULES = ['_nt', '_winapi', '_overlapped', 'winreg', '_winreg'] if IS_WINDOWS else []
49-
WINDOWS_FULL_STARTUP_MODULES = [
50-
'_datetime',
51-
'datetime',
52-
'_ctypes',
53-
'_struct',
54-
'struct',
55-
'ctypes._endian',
56-
'ctypes',
57-
'ctypes.wintypes',
58-
] if IS_WINDOWS else []
48+
49+
if IS_WINDOWS and sys.implementation.name == 'graalpy' and __graalpython__.native_access_is_available():
50+
WINDOWS_CORE_MODULES = ['_nt', '_winapi', '_overlapped', 'winreg', '_winreg']
51+
WINDOWS_FULL_STARTUP_MODULES = [
52+
'_datetime',
53+
'datetime',
54+
'_ctypes',
55+
'_struct',
56+
'struct',
57+
'ctypes._endian',
58+
'ctypes',
59+
'ctypes.wintypes',
60+
]
61+
else:
62+
WINDOWS_CORE_MODULES = []
63+
WINDOWS_FULL_STARTUP_MODULES = ['_winapi'] if IS_WINDOWS else []
5964

6065
expected_nosite_startup_modules = [
6166
'_frozen_importlib',
@@ -90,11 +95,11 @@ def test_startup_nosite(self):
9095
result = subprocess.check_output([sys.executable, '--log.level=FINE', '-S', '-v', '-c', 'print("Hello")'], stderr=subprocess.STDOUT, text=True)
9196
assert 'Hello' in result
9297
imports = re.findall(r"import '(\S+)'", result)
93-
self.assertEqual(expected_nosite_startup_modules, imports)
98+
self.assertEqual(sorted(expected_nosite_startup_modules), sorted(imports))
9499

95100
@unittest.skipUnless(sys.implementation.name == 'graalpy', "GraalPy-specific test")
96101
def test_startup_full(self):
97102
result = subprocess.check_output([sys.executable, '--log.level=FINE', '-s', '-v', '-c', 'print("Hello")'], stderr=subprocess.STDOUT, text=True)
98103
assert 'Hello' in result
99104
imports = re.findall(r"import '(\S+)'", result)
100-
self.assertEqual(expected_full_startup_modules, imports)
105+
self.assertEqual(sorted(expected_full_startup_modules), sorted(imports))

graalpython/com.oracle.graal.python.test/src/tests/test_sys_settrace.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
import signal
4444
from tests import util
4545
import builtins
46-
import asyncio
4746

4847

4948
GRAALPY_POSIX_BACKEND_IS_JAVA = (
@@ -520,6 +519,15 @@ def func():
520519
self.assert_events(self.events, events)
521520

522521
class AsyncTracingEventsUnitTest(TracingEventsUnitTest):
522+
@staticmethod
523+
def run_awaitable(awaitable):
524+
iterator = awaitable.__await__()
525+
try:
526+
while True:
527+
next(iterator)
528+
except StopIteration as e:
529+
return e.value
530+
523531
def async_trace(self, frame, event, arg):
524532
code = frame.f_code
525533
name = code.co_name
@@ -539,7 +547,7 @@ async def async_wrapper(self, afunc, names):
539547
def trace_async_function(self, afunc, names):
540548
self.first_line = afunc.__code__.co_firstlineno
541549
self.events = []
542-
asyncio.run(self.async_wrapper(afunc, names))
550+
self.run_awaitable(self.async_wrapper(afunc, names))
543551

544552
@unittest.skipIf(
545553
sys.implementation.name == "graalpy",

graalpython/com.oracle.graal.python.test/src/tests/test_wheel.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@
5252

5353
# cannot be moved to tagged as it uses testlib_helper
5454
@unittest.skipIf(os.environ.get("GITHUB_CI"), "Skip on Github CI")
55+
@unittest.skipIf(
56+
sys.implementation.name == "graalpy" and not __graalpython__.native_access_is_available(),
57+
"Skipped because importing the repaired wheel requires native access support",
58+
)
5559
class TestWheelBuildAndRun(unittest.TestCase):
5660
def test_build_install_and_run(self):
5761
# Build a C library and a wheel that depends on it. Then run it through repair_wheel to vendor the library in and verify that it can be imported

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,12 +1051,14 @@ private void initializePython3Core(TruffleString coreHome) {
10511051
}
10521052

10531053
private void initializeWindowsCoreFiles(TruffleString coreHome) {
1054-
assert !ImageInfo.inImageBuildtimeCode();
1055-
loadFile(toTruffleStringUncached("_nt"), coreHome);
1056-
loadFile(toTruffleStringUncached("_winapi"), toTruffleStringUncached("modules/_winapi"), coreHome);
1057-
loadFile(toTruffleStringUncached("_overlapped"), toTruffleStringUncached("modules/_overlapped"), coreHome);
1058-
loadFile(toTruffleStringUncached("winreg"), toTruffleStringUncached("modules/winreg"), coreHome);
1059-
loadFile(toTruffleStringUncached("_winreg"), toTruffleStringUncached("modules/_winreg"), coreHome);
1054+
if (PythonLanguage.getPythonOS() == PythonOS.PLATFORM_WIN32 && getContext().isNativeAccessAllowed()) {
1055+
assert !ImageInfo.inImageBuildtimeCode();
1056+
loadFile(toTruffleStringUncached("_nt"), coreHome);
1057+
loadFile(toTruffleStringUncached("_winapi"), toTruffleStringUncached("modules/_winapi"), coreHome);
1058+
loadFile(toTruffleStringUncached("_overlapped"), toTruffleStringUncached("modules/_overlapped"), coreHome);
1059+
loadFile(toTruffleStringUncached("winreg"), toTruffleStringUncached("modules/winreg"), coreHome);
1060+
loadFile(toTruffleStringUncached("_winreg"), toTruffleStringUncached("modules/_winreg"), coreHome);
1061+
}
10601062
}
10611063

10621064
/**

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -928,6 +928,16 @@ private static int which() {
928928
}
929929
}
930930

931+
@Builtin(name = "native_access_is_available", minNumOfPositionalArgs = 0)
932+
@GenerateNodeFactory
933+
public abstract static class NativeAccessNode extends PythonBuiltinNode {
934+
@TruffleBoundary
935+
@Specialization
936+
static boolean isAvailable(@Bind PythonContext context) {
937+
return context.isNativeAccessAllowed();
938+
}
939+
}
940+
931941
@Builtin(name = "posix_module_backend", minNumOfPositionalArgs = 0)
932942
@GenerateNodeFactory
933943
public abstract static class PosixModuleBackendNode extends PythonBuiltinNode {

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,10 @@ public void postInitialize0(Python3Core core) {
707707
sys.setAttribute(T___UNRAISABLEHOOK__, sys.getAttribute(T_UNRAISABLEHOOK));
708708
sys.setAttribute(T___DISPLAYHOOK__, sys.getAttribute(T_DISPLAYHOOK));
709709
sys.setAttribute(T___BREAKPOINTHOOK__, sys.getAttribute(T_BREAKPOINTHOOK));
710+
711+
if (!context.isNativeAccessAllowed()) {
712+
sys.setAttribute(tsInternedLiteral("getrefcount"), PNone.NO_VALUE);
713+
}
710714
}
711715

712716
private static PFrozenSet createStdLibModulesSet(PythonLanguage language) {
@@ -1054,8 +1058,7 @@ public abstract static class GetrefcountNode extends PythonUnaryBuiltinNode {
10541058
static long doGeneric(Object object,
10551059
@Bind PythonContext context) {
10561060
if (!context.isNativeAccessAllowed()) {
1057-
CompilerDirectives.transferToInterpreterAndInvalidate();
1058-
throw new RuntimeException(ErrorMessages.NATIVE_ACCESS_NOT_ALLOWED.toJavaStringUncached());
1061+
throw CompilerDirectives.shouldNotReachHere();
10591062
}
10601063
if (context.getCApiState() != CApiState.INITIALIZED) {
10611064
if (object instanceof PythonAbstractObject pythonAbstractObject) {

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

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2022, 2025, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2022, 2026, 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
@@ -42,16 +42,62 @@
4242

4343
import java.util.List;
4444

45+
import com.oracle.graal.python.annotations.Builtin;
46+
import com.oracle.graal.python.annotations.PythonOS;
4547
import com.oracle.graal.python.builtins.CoreFunctions;
48+
import com.oracle.graal.python.builtins.Python3Core;
4649
import com.oracle.graal.python.builtins.PythonBuiltins;
47-
import com.oracle.graal.python.annotations.PythonOS;
50+
import com.oracle.graal.python.builtins.objects.PNone;
51+
import com.oracle.graal.python.builtins.objects.str.StringUtils;
4852
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
53+
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
54+
import com.oracle.graal.python.runtime.PythonContext;
55+
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
56+
import com.oracle.truffle.api.dsl.Bind;
57+
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
4958
import com.oracle.truffle.api.dsl.NodeFactory;
59+
import com.oracle.truffle.api.dsl.Specialization;
5060

5161
@CoreFunctions(defineModule = "_winapi", os = PythonOS.PLATFORM_WIN32)
5262
public final class WinapiModuleBuiltins extends PythonBuiltins {
63+
@Override
64+
public void initialize(Python3Core core) {
65+
super.initialize(core);
66+
addBuiltinConstant("NULL", 0);
67+
addBuiltinConstant("INFINITE", 0xFFFFFFFFL);
68+
}
69+
5370
@Override
5471
protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFactories() {
55-
return List.of();
72+
return WinapiModuleBuiltinsFactory.getFactories();
73+
}
74+
75+
// Managed fallback for native kernel32.GetACP call so encodings work sandboxed
76+
@Builtin(name = "GetACP", minNumOfPositionalArgs = 0)
77+
@GenerateNodeFactory
78+
abstract static class GetfullpathnameNode extends PythonBuiltinNode {
79+
@TruffleBoundary
80+
@Specialization
81+
int getacp(@Bind PythonContext context) {
82+
var ts = SysModuleBuiltins.GetFileSystemEncodingNode.getFileSystemEncoding();
83+
var cp = StringUtils.toLowerCase(ts.toJavaStringUncached());
84+
if (cp.startsWith("cp")) {
85+
try {
86+
return Integer.valueOf(cp.substring(2));
87+
} catch (Exception e) {
88+
// pass
89+
}
90+
}
91+
return 0;
92+
}
93+
}
94+
95+
@Builtin(name = "CloseHandle", minNumOfPositionalArgs = 1)
96+
@GenerateNodeFactory
97+
abstract static class CloseHandleNode extends PythonBuiltinNode {
98+
@Specialization
99+
static Object closeHandle(@SuppressWarnings("unused") Object handle) {
100+
return PNone.NONE;
101+
}
56102
}
57103
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/PythonContext.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1261,7 +1261,7 @@ public PythonContext(PythonLanguage language, TruffleLanguage.Env env) {
12611261
this.in = env.in();
12621262
this.out = env.out();
12631263
this.err = env.err();
1264-
this.nativeAccessAllowed = env.isNativeAccessAllowed() && !PythonOS.isUnsupported();
1264+
this.nativeAccessAllowed = env.isNativeAccessAllowed() && !PythonOS.isUnsupported() && NativeAccessSupport.isAvailable();
12651265
}
12661266

12671267
private static final ContextReference<PythonContext> REFERENCE = ContextReference.create(PythonLanguage.class);
@@ -1439,7 +1439,7 @@ public void setEnv(TruffleLanguage.Env newEnv) {
14391439
err = env.err();
14401440
posixSupport.setEnv(env);
14411441
optionValues = PythonOptions.createOptionValuesStorage(newEnv);
1442-
nativeAccessAllowed = newEnv.isNativeAccessAllowed() && !PythonOS.isUnsupported();
1442+
nativeAccessAllowed = newEnv.isNativeAccessAllowed() && !PythonOS.isUnsupported() && NativeAccessSupport.isAvailable();
14431443
}
14441444

14451445
/**

graalpython/lib-graalpython/modules/_winapi.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@
4444
raise ImportError("win32 only")
4545

4646

47+
if not __graalpython__.native_access_is_available():
48+
raise ImportError("needs native access")
49+
50+
4751
_POINTER_BITS = 64
4852

4953

0 commit comments

Comments
 (0)