Skip to content

Commit b5c5bff

Browse files
committed
disambiguate sys.prefix and sys.base_prefix. This is needed for correct venv behaviour when compiling native modules, for example.
1 parent 8d19acf commit b5c5bff

File tree

5 files changed

+41
-8
lines changed

5 files changed

+41
-8
lines changed

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,14 +146,16 @@ protected PythonContext createContext(Env env) {
146146
private void ensureHomeInOptions(Env env) {
147147
String languageHome = getLanguageHome();
148148
String sysPrefix = env.getOptions().get(PythonOptions.SysPrefix);
149+
String basePrefix = env.getOptions().get(PythonOptions.SysBasePrefix);
149150
String coreHome = env.getOptions().get(PythonOptions.CoreHome);
150151
String stdLibHome = env.getOptions().get(PythonOptions.StdLibHome);
151152

152153
PythonCore.writeInfo((MessageFormat.format("Initial locations:" +
153154
"\n\tLanguage home: {0}" +
154155
"\n\tSysPrefix: {1}" +
155-
"\n\tCoreHome: {2}" +
156-
"\n\tStdLibHome: {3}", languageHome, sysPrefix, coreHome, stdLibHome)));
156+
"\n\tBaseSysPrefix: {2}" +
157+
"\n\tCoreHome: {3}" +
158+
"\n\tStdLibHome: {4}", languageHome, sysPrefix, basePrefix, coreHome, stdLibHome)));
157159

158160
TruffleFile home = null;
159161
if (languageHome != null) {
@@ -177,6 +179,11 @@ private void ensureHomeInOptions(Env env) {
177179
env.getOptions().set(PythonOptions.SysPrefix, sysPrefix);
178180
}
179181

182+
if (basePrefix.isEmpty()) {
183+
basePrefix = home.getAbsoluteFile().getPath();
184+
env.getOptions().set(PythonOptions.SysBasePrefix, basePrefix);
185+
}
186+
180187
if (coreHome.isEmpty()) {
181188
try {
182189
for (TruffleFile f : home.list()) {
@@ -210,8 +217,9 @@ private void ensureHomeInOptions(Env env) {
210217
PythonCore.writeInfo((MessageFormat.format("Updated locations:" +
211218
"\n\tLanguage home: {0}" +
212219
"\n\tSysPrefix: {1}" +
213-
"\n\tCoreHome: {2}" +
214-
"\n\tStdLibHome: {3}", home.getPath(), sysPrefix, coreHome, stdLibHome)));
220+
"\n\tSysBasePrefix: {2}" +
221+
"\n\tCoreHome: {3}" +
222+
"\n\tStdLibHome: {4}", home.getPath(), sysPrefix, basePrefix, coreHome, stdLibHome)));
215223
}
216224
}
217225

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ public class SysModuleBuiltins extends PythonBuiltins {
104104
COMPILE_TIME = compile_time;
105105
}
106106

107-
public static final String[] SYS_PREFIX_ATTRIBUTES = new String[]{"prefix", "exec_prefix", "base_prefix", "base_exec_prefix"};
107+
public static final String[] SYS_PREFIX_ATTRIBUTES = new String[]{"prefix", "exec_prefix"};
108+
public static final String[] BASE_PREFIX_ATTRIBUTES = new String[]{"base_prefix", "base_exec_prefix"};
108109

109110
@Override
110111
protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFactories() {
@@ -159,11 +160,17 @@ public void postInitialize(PythonCore core) {
159160
PythonContext context = core.getContext();
160161
String[] args = context.getEnv().getApplicationArguments();
161162
sys.setAttribute("argv", core.factory().createList(Arrays.copyOf(args, args.length, Object[].class)));
163+
162164
String prefix = PythonCore.getSysPrefix(context.getEnv());
163165
for (String name : SysModuleBuiltins.SYS_PREFIX_ATTRIBUTES) {
164166
sys.setAttribute(name, prefix);
165167
}
166168

169+
String base_prefix = PythonCore.getSysBasePrefix(context.getEnv());
170+
for (String name : SysModuleBuiltins.BASE_PREFIX_ATTRIBUTES) {
171+
sys.setAttribute(name, base_prefix);
172+
}
173+
167174
sys.setAttribute("executable", PythonOptions.getOption(context, PythonOptions.Executable));
168175
sys.setAttribute("graal_python_home", context.getLanguage().getHome());
169176
sys.setAttribute("graal_python_core_home", PythonOptions.getOption(context, PythonOptions.CoreHome));

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,21 @@ public static String getSysPrefix(TruffleLanguage.Env env) {
120120
if (sysPrefix.isEmpty()) {
121121
writeWarning(NO_PREFIX_WARNING);
122122
env.getOptions().set(PythonOptions.SysPrefix, PREFIX);
123-
return LIB_GRAALPYTHON;
123+
return PREFIX;
124+
}
125+
return sysPrefix;
126+
}
127+
128+
@TruffleBoundary
129+
public static String getSysBasePrefix(TruffleLanguage.Env env) {
130+
String sysPrefix = env.getOptions().get(PythonOptions.SysBasePrefix);
131+
if (sysPrefix.isEmpty()) {
132+
String homePrefix = PythonLanguage.getCurrent().getHome();
133+
if (homePrefix.isEmpty()) {
134+
homePrefix = PREFIX;
135+
}
136+
env.getOptions().set(PythonOptions.SysBasePrefix, homePrefix);
137+
return homePrefix;
124138
}
125139
return sysPrefix;
126140
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ private PythonOptions() {
4444
@Option(category = OptionCategory.USER, help = "Set the location of sys.prefix. Overrides any environment variables or Java options.") //
4545
public static final OptionKey<String> SysPrefix = new OptionKey<>("");
4646

47+
@Option(category = OptionCategory.EXPERT, help = "Set the location of sys.base_prefix. Overrides any environment variables or Java options.") //
48+
public static final OptionKey<String> SysBasePrefix = new OptionKey<>("");
49+
4750
@Option(category = OptionCategory.USER, help = "Set the location of lib-graalpython. Overrides any environment variables or Java options.") //
4851
public static final OptionKey<String> CoreHome = new OptionKey<>("");
4952

graalpython/lib-python/3/venv/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,10 +326,11 @@ def replace_variables(self, text, context):
326326
# Truffle change: we need to set some extra options for the launcher to work
327327
text = text.replace(
328328
'__VENV_GRAAL_PYTHON_OPTIONS__',
329-
"--python.CoreHome='%s' --python.StdLibHome='%s' --python.SysPrefix='%s' --python.Executable='%s'" % (
329+
"--python.CoreHome='%s' --python.StdLibHome='%s' --python.SysPrefix='%s' --python.SysBasePrefix='%s' --python.Executable='%s'" % (
330330
sys.graal_python_core_home,
331331
sys.graal_python_stdlib_home,
332-
sys.prefix,
332+
context.env_dir,
333+
sys.base_prefix,
333334
context.env_exe,
334335
)
335336
)

0 commit comments

Comments
 (0)