Skip to content

Commit 9104425

Browse files
committed
[GR-13824] Fix installation of NumPy in a venv
PullRequest: graalpython/403
2 parents fa18c45 + b5c5bff commit 9104425

File tree

6 files changed

+49
-13
lines changed

6 files changed

+49
-13
lines changed

graalpython/com.oracle.graal.python.shell/src/com/oracle/graal/python/shell/GraalPythonMain.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ public static void main(String[] args) {
7878
@Override
7979
protected List<String> preprocessArguments(List<String> givenArgs, Map<String, String> polyglotOptions) {
8080
ArrayList<String> unrecognized = new ArrayList<>();
81-
ArrayList<String> inputArgs = new ArrayList<>(getDefaultEnvironmentArgs());
81+
List<String> defaultEnvironmentArgs = getDefaultEnvironmentArgs();
82+
ArrayList<String> inputArgs = new ArrayList<>(defaultEnvironmentArgs);
8283
inputArgs.addAll(givenArgs);
8384
givenArguments = new ArrayList<>(inputArgs);
8485
List<String> arguments = new ArrayList<>(inputArgs);
@@ -142,15 +143,15 @@ protected List<String> preprocessArguments(List<String> givenArgs, Map<String, S
142143
versionAction = VersionAction.PrintAndContinue;
143144
break;
144145
case "-CC":
145-
if (i != 0) {
146-
throw new IllegalArgumentException("-CC must be the first argument");
146+
if (i != defaultEnvironmentArgs.size()) {
147+
throw new IllegalArgumentException("-CC must be the first given argument");
147148
}
148149
GraalPythonCC.main(arguments.subList(i + 1, arguments.size()).toArray(new String[0]));
149150
System.exit(0);
150151
break;
151152
case "-LD":
152-
if (i != 0) {
153-
throw new IllegalArgumentException("-LD must be the first argument");
153+
if (i != defaultEnvironmentArgs.size()) {
154+
throw new IllegalArgumentException("-LD must be the first given argument");
154155
}
155156
GraalPythonLD.main(arguments.subList(i + 1, arguments.size()).toArray(new String[0]));
156157
System.exit(0);

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

Lines changed: 14 additions & 5 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) {
@@ -173,7 +175,13 @@ private void ensureHomeInOptions(Env env) {
173175

174176
if (home != null) {
175177
if (sysPrefix.isEmpty()) {
176-
env.getOptions().set(PythonOptions.SysPrefix, home.getAbsoluteFile().getPath());
178+
sysPrefix = home.getAbsoluteFile().getPath();
179+
env.getOptions().set(PythonOptions.SysPrefix, sysPrefix);
180+
}
181+
182+
if (basePrefix.isEmpty()) {
183+
basePrefix = home.getAbsoluteFile().getPath();
184+
env.getOptions().set(PythonOptions.SysBasePrefix, basePrefix);
177185
}
178186

179187
if (coreHome.isEmpty()) {
@@ -209,8 +217,9 @@ private void ensureHomeInOptions(Env env) {
209217
PythonCore.writeInfo((MessageFormat.format("Updated locations:" +
210218
"\n\tLanguage home: {0}" +
211219
"\n\tSysPrefix: {1}" +
212-
"\n\tCoreHome: {2}" +
213-
"\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)));
214223
}
215224
}
216225

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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,9 +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.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+
context.env_dir,
333+
sys.base_prefix,
332334
context.env_exe,
333335
)
334336
)

0 commit comments

Comments
 (0)