Skip to content

Commit f771ec5

Browse files
committed
Adding PYTHONPYCACHEPREFIX / PyCachePrefix option.
1 parent 50c4659 commit f771ec5

File tree

5 files changed

+14
-6
lines changed

5 files changed

+14
-6
lines changed

CHANGELOG.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ This changelog summarizes major changes between GraalVM versions of the Python
44
language runtime. The main focus is on user-observable behavior of the engine.
55

66
## Version 20.1.1
7-
* When a `*.py` file is imported, `*.pyc` file is created. It contains binary data to speed up parsing.
8-
7+
* When a `*.py` file is imported, `*.pyc` file is created. It contains binary data to speed up parsing.
8+
* Adding option `PyCachePrefix`, which is equivalent to PYTHONPYCACHEPREFIX environment variable.
9+
* Adding optin `DontWriteBytecodeFlag`. Equivalent to the Python -B flag. Don't write bytecode files.
10+
* Command option -B works
911

1012
## Version 20.1.0
1113

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,10 @@ protected void launch(Builder contextBuilder) {
377377
verboseFlag = verboseFlag || System.getenv("PYTHONVERBOSE") != null;
378378
unbufferedIO = unbufferedIO || System.getenv("PYTHONUNBUFFERED") != null;
379379
dontWriteBytecode = dontWriteBytecode || System.getenv("PYTHONDONTWRITEBYTECODE") != null;
380+
String cachePrefix = System.getenv("PYTHONPYCACHEPREFIX");
381+
if (cachePrefix != null) {
382+
contextBuilder.option("python.PyCachePrefix", cachePrefix);
383+
}
380384
}
381385

382386
String executable = getContextOptionIfSetViaCommandLine("python.Executable");
@@ -552,8 +556,7 @@ protected String getLanguageId() {
552556
protected void printHelp(OptionCategory maxCategory) {
553557
print("usage: python [option] ... (-c cmd | file) [arg] ...\n" +
554558
"Options and arguments (and corresponding environment variables):\n" +
555-
"-B : on CPython, this disables writing .py[co] files on import;\n" +
556-
" GraalPython does not use bytecode, and thus this flag has no effect\n" +
559+
"-B : this disables writing .py[co] files on import\n" +
557560
"-c cmd : program passed in as string (terminates option list)\n" +
558561
// "-d : debug output from parser; also PYTHONDEBUG=x\n" +
559562
"-E : ignore PYTHON* environment variables (such as PYTHONPATH)\n" +

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,8 @@ public void postInitialize(PythonCore core) {
192192
sys.setAttribute("_base_executable", context.getOption(PythonOptions.Executable));
193193
}
194194
sys.setAttribute("dont_write_bytecode", context.getOption(PythonOptions.DontWriteBytecodeFlag));
195+
String pycachePrefix = context.getOption(PythonOptions.PyCachePrefix);
196+
sys.setAttribute("pycache_prefix", pycachePrefix.isEmpty() ? PNone.NONE : pycachePrefix);
195197
sys.setAttribute("__flags__", core.factory().createTuple(new Object[]{
196198
false, // bytes_warning
197199
!context.getOption(PythonOptions.PythonOptimizeFlag), // debug

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
@@ -105,6 +105,9 @@ private PythonOptions() {
105105
@Option(category = OptionCategory.USER, help = "Equivalent to the Python -B flag. Don't write bytecode files.", stability = OptionStability.STABLE) //
106106
public static final OptionKey<Boolean> DontWriteBytecodeFlag = new OptionKey<>(false);
107107

108+
@Option(category = OptionCategory.USER, help = "If this is set, GraalPython will write .pyc files in a mirror directory tree at this path, instead of in __pycache__ directories within the source tree. Equivalent to setting the PYTHONPYCACHEPREFIX environment variable for the standard launcher.", stability = OptionStability.STABLE) //
109+
public static final OptionKey<String> PyCachePrefix = new OptionKey<>("");
110+
108111
@Option(category = OptionCategory.INTERNAL, help = "Set the location of C API home. Overrides any environment variables or Java options.", stability = OptionStability.STABLE) //
109112
public static final OptionKey<String> CAPI = new OptionKey<>("");
110113

graalpython/lib-graalpython/sys.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,6 @@ def make_hash_info_class():
149149
# continue prompt for interactive shell
150150
ps2 = "... "
151151

152-
pycache_prefix = None
153-
154152
# Stub audit hooks implementation for PEP 578
155153
def audit(str, *args):
156154
pass

0 commit comments

Comments
 (0)