Skip to content

Commit ba3c2a8

Browse files
committed
introduce _sysconfig builtin for truffle land config options
- patch _thread with dummy_thread when threads are disable (needed by setuptools)
1 parent ab29fe0 commit ba3c2a8

File tree

6 files changed

+161
-25
lines changed

6 files changed

+161
-25
lines changed

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@
4040
import re
4141
import sys
4242

43-
try:
43+
import _sysconfig
44+
45+
if _sysconfig.get_config_var('WITH_THREAD'):
4446
import threading
4547
import unittest
4648
from test import support
@@ -508,6 +510,3 @@ def test_state_after_timeout(self):
508510
lock.release()
509511
self.assertFalse(lock.locked())
510512
self.assertTrue(lock.acquire(blocking=False))
511-
512-
except (ModuleNotFoundError, ImportError) as e:
513-
print(e)

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
import com.oracle.graal.python.builtins.modules.SignalModuleBuiltins;
7777
import com.oracle.graal.python.builtins.modules.SocketModuleBuiltins;
7878
import com.oracle.graal.python.builtins.modules.StringModuleBuiltins;
79+
import com.oracle.graal.python.builtins.modules.SysConfigModuleBuiltins;
7980
import com.oracle.graal.python.builtins.modules.SysModuleBuiltins;
8081
import com.oracle.graal.python.builtins.modules.ThreadModuleBuiltins;
8182
import com.oracle.graal.python.builtins.modules.TimeModuleBuiltins;
@@ -206,12 +207,9 @@ private static final String[] initializeCoreFiles() {
206207
"_locale",
207208
"_sre",
208209
"function",
209-
"_socket"));
210-
211-
// threads
212-
if (PythonLanguage.WITH_THREADS) {
213-
coreFiles.add("_thread");
214-
}
210+
"_sysconfig",
211+
"_socket",
212+
"_thread"));
215213

216214
return coreFiles.toArray(new String[coreFiles.size()]);
217215
}
@@ -303,7 +301,8 @@ private static final PythonBuiltins[] initializeBuiltins() {
303301
new PosixSubprocessModuleBuiltins(),
304302
new CtypesModuleBuiltins(),
305303
new ReadlineModuleBuiltins(),
306-
new PyExpatModuleBuiltins()));
304+
new PyExpatModuleBuiltins(),
305+
new SysConfigModuleBuiltins()));
307306
if (!TruffleOptions.AOT) {
308307
ServiceLoader<PythonBuiltins> providers = ServiceLoader.load(PythonBuiltins.class);
309308
for (PythonBuiltins builtin : providers) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright (c) 2018, 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.builtins.modules;
42+
43+
import java.util.HashMap;
44+
import java.util.List;
45+
import java.util.Map;
46+
47+
import com.oracle.graal.python.PythonLanguage;
48+
import com.oracle.graal.python.builtins.Builtin;
49+
import com.oracle.graal.python.builtins.CoreFunctions;
50+
import com.oracle.graal.python.builtins.PythonBuiltins;
51+
import com.oracle.graal.python.builtins.objects.dict.PDict;
52+
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
53+
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
54+
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
55+
import com.oracle.truffle.api.dsl.NodeFactory;
56+
import com.oracle.truffle.api.dsl.Specialization;
57+
58+
/**
59+
* this builtin module is used to fill in truffle land config options into the sysconfig python
60+
* module
61+
*/
62+
@CoreFunctions(defineModule = "_sysconfig")
63+
public class SysConfigModuleBuiltins extends PythonBuiltins {
64+
private final static Map<Object, Object> STATIC_CONFIG_OPTIONS = new HashMap<>();
65+
static {
66+
STATIC_CONFIG_OPTIONS.put("WITH_THREAD", PythonLanguage.WITH_THREADS ? 1 : 0);
67+
}
68+
69+
@Override
70+
protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFactories() {
71+
return SysConfigModuleBuiltinsFactory.getFactories();
72+
}
73+
74+
@Builtin(name = "get_config_vars", takesVarArgs = true)
75+
@GenerateNodeFactory
76+
static abstract class GetConfigVarsNode extends PythonBuiltinNode {
77+
@Specialization
78+
PDict select(@SuppressWarnings("unused") Object[] arguments) {
79+
return factory().createDict(STATIC_CONFIG_OPTIONS);
80+
}
81+
}
82+
}

graalpython/lib-graalpython/__builtins_patches__.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,21 @@
3737
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3838
# SOFTWARE.
3939

40-
import sys
40+
import _pyio
41+
import io
42+
43+
import _io
44+
import _sysconfig
4145
import builtins
46+
import sys
47+
4248

4349
# ----------------------------------------------------------------------------------------------------------------------
4450
#
4551
# patch _io
4652
#
4753
# ----------------------------------------------------------------------------------------------------------------------
4854

49-
import _io
50-
import _pyio
51-
import io
52-
53-
5455
@__builtin__
5556
def open(*args, **kwargs):
5657
return _pyio.open(*args, **kwargs)
@@ -72,3 +73,14 @@ def open(*args, **kwargs):
7273

7374

7475
setattr(builtins, 'open', open)
76+
77+
78+
# ----------------------------------------------------------------------------------------------------------------------
79+
#
80+
# patch _thread (needed for setuptools if threading is disabled)
81+
#
82+
# ----------------------------------------------------------------------------------------------------------------------
83+
if not _sysconfig.get_config_var('WITH_THREAD'):
84+
import _dummy_thread
85+
sys.modules["_thread"] = _dummy_thread
86+
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
2+
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3+
#
4+
# The Universal Permissive License (UPL), Version 1.0
5+
#
6+
# Subject to the condition set forth below, permission is hereby granted to any
7+
# person obtaining a copy of this software, associated documentation and/or
8+
# data (collectively the "Software"), free of charge and under any and all
9+
# copyright rights in the Software, and any and all patent rights owned or
10+
# freely licensable by each licensor hereunder covering either (i) the
11+
# unmodified Software as contributed to or provided by such licensor, or (ii)
12+
# the Larger Works (as defined below), to deal in both
13+
#
14+
# (a) the Software, and
15+
#
16+
# (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
17+
# one is included with the Software each a "Larger Work" to which the Software
18+
# is contributed by such licensors),
19+
#
20+
# without restriction, including without limitation the rights to copy, create
21+
# derivative works of, display, perform, and distribute the Software and make,
22+
# use, sell, offer for sale, import, export, have made, and have sold the
23+
# Software and the Larger Work(s), and to sublicense the foregoing rights on
24+
# either these or other terms.
25+
#
26+
# This license is subject to the following condition:
27+
#
28+
# The above copyright notice and either this complete permission notice or at a
29+
# minimum a reference to the UPL must be included in all copies or substantial
30+
# portions of the Software.
31+
#
32+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38+
# SOFTWARE.
39+
40+
41+
def get_config_var(name):
42+
return get_config_vars().get(name)

graalpython/lib-graalpython/_thread.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,17 @@
3737
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3838
# SOFTWARE.
3939

40-
error = RuntimeError
41-
TIMEOUT_MAX = __truffle_get_timeout_max__()
40+
import _sysconfig
41+
if _sysconfig.get_config_var('WITH_THREAD'):
42+
error = RuntimeError
43+
TIMEOUT_MAX = __truffle_get_timeout_max__()
4244

4345

44-
@__builtin__
45-
def allocate_lock():
46-
return LockType()
46+
@__builtin__
47+
def allocate_lock():
48+
return LockType()
4749

4850

49-
def _set_sentinel():
50-
"""Dummy implementation of _thread._set_sentinel()."""
51-
return LockType()
51+
def _set_sentinel():
52+
"""Dummy implementation of _thread._set_sentinel()."""
53+
return LockType()

0 commit comments

Comments
 (0)