Skip to content

Commit 7960c70

Browse files
committed
honour unbufferedIO flag
1 parent e25d91c commit 7960c70

File tree

4 files changed

+16
-10
lines changed

4 files changed

+16
-10
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public static void main(String[] args) {
7171
private boolean noSite = false;
7272
private boolean stdinIsInteractive = System.console() != null;
7373
private boolean runLLI = false;
74+
private boolean unbufferedIO = false;
7475
private VersionAction versionAction = VersionAction.None;
7576
private String sulongLibraryPath = null;
7677
private List<String> givenArguments;
@@ -184,8 +185,7 @@ protected List<String> preprocessArguments(List<String> givenArgs, Map<String, S
184185
inputArgs.remove("-compile-truffle-immediately");
185186
break;
186187
case "-u":
187-
// TODO we currently don't support this option, but needs to be consumed
188-
// due pip/wheel installer.
188+
unbufferedIO = true;
189189
break;
190190
default:
191191
if (!arg.startsWith("-")) {
@@ -340,6 +340,7 @@ protected void launch(Builder contextBuilder) {
340340
inspectFlag = inspectFlag || System.getenv("PYTHONINSPECT") != null;
341341
noUserSite = noUserSite || System.getenv("PYTHONNOUSERSITE") != null;
342342
verboseFlag = verboseFlag || System.getenv("PYTHONVERBOSE") != null;
343+
unbufferedIO = unbufferedIO || System.getenv("PYTHONUNBUFFERED") != null;
343344
}
344345

345346
// The unlikely separator is used because options need to be strings. See
@@ -359,6 +360,7 @@ protected void launch(Builder contextBuilder) {
359360
contextBuilder.option("python.NoUserSiteFlag", Boolean.toString(noUserSite));
360361
contextBuilder.option("python.NoSiteFlag", Boolean.toString(noSite));
361362
contextBuilder.option("python.IgnoreEnvironmentFlag", Boolean.toString(ignoreEnv));
363+
contextBuilder.option("python.UnbufferedIO", Boolean.toString(unbufferedIO));
362364

363365
sulongLibraryPath = System.getenv("SULONG_LIBRARY_PATH");
364366
if (sulongLibraryPath != null) {
@@ -513,8 +515,7 @@ protected void printHelp(OptionCategory maxCategory) {
513515
"-S : don't imply 'import site' on initialization\n" +
514516
// "-t : issue warnings about inconsistent tab usage (-tt: issue errors)\n"
515517
// +
516-
// "-u : unbuffered binary stdout and stderr; also PYTHONUNBUFFERED=x\n" +
517-
// " see man page for details on internal buffering relating to '-u'\n" +
518+
"-u : unbuffered binary stdout and stderr; also PYTHONUNBUFFERED=x\n" +
518519
"-v : verbose (trace import statements); also PYTHONVERBOSE=x\n" +
519520
" can be supplied multiple times to increase verbosity\n" +
520521
"-V : print the Python version number and exit (also --version)\n" +

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ public void postInitialize(PythonCore core) {
177177
sys.setAttribute("graal_python_stdlib_home", PythonOptions.getOption(context, PythonOptions.StdLibHome));
178178
sys.setAttribute("graal_python_opaque_filesystem", PythonOptions.getOption(context, PythonOptions.OpaqueFilesystem));
179179
sys.setAttribute("graal_python_opaque_filesystem_prefix", PythonOptions.getOption(context, PythonOptions.OpaqueFilesystemPrefixes));
180+
sys.setAttribute("graal_python_unbuffered_io", PythonOptions.getOption(context, PythonOptions.UnbufferedIO));
180181
sys.setAttribute("__flags__", core.factory().createTuple(new Object[]{
181182
false, // bytes_warning
182183
!PythonOptions.getFlag(context, 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
@@ -84,6 +84,9 @@ private PythonOptions() {
8484
@Option(category = OptionCategory.USER, help = "Equivalent to the Python -v flag. Turn on verbose mode.") //
8585
public static final OptionKey<Boolean> VerboseFlag = new OptionKey<>(false);
8686

87+
@Option(category = OptionCategory.USER, help = "Equivalent to the Python -u flag. Force stdout and stderr to be unbuffered.") //
88+
public static final OptionKey<Boolean> UnbufferedIO = new OptionKey<>(false);
89+
8790
@Option(category = OptionCategory.DEBUG, help = "Expose internal sources as normal sources, so they will show up in the debugger and stacks") //
8891
public static final OptionKey<Boolean> ExposeInternalSources = new OptionKey<>(false);
8992

graalpython/lib-graalpython/__builtins_patches__.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,13 @@ def open(*args, **kwargs):
8080
sys.stdin = _pyio.TextIOWrapper(_pyio.BufferedReader(sys.stdin), encoding="utf-8", line_buffering=True)
8181
sys.stdin.mode = "r"
8282
sys.__stdin__ = sys.stdin
83-
sys.stdout = _pyio.TextIOWrapper(_pyio.BufferedWriter(sys.stdout), encoding="utf-8", line_buffering=True)
84-
sys.stdout.mode = "w"
85-
sys.__stdout__ = sys.stdout
86-
sys.stderr = _pyio.TextIOWrapper(_pyio.BufferedWriter(sys.stderr), encoding="utf-8", line_buffering=True)
87-
sys.stderr.mode = "w"
88-
sys.__stderr__ = sys.stderr
83+
if not sys.graal_python_unbuffered_io:
84+
sys.stdout = _pyio.TextIOWrapper(_pyio.BufferedWriter(sys.stdout), encoding="utf-8", line_buffering=True)
85+
sys.stdout.mode = "w"
86+
sys.__stdout__ = sys.stdout
87+
sys.stderr = _pyio.TextIOWrapper(_pyio.BufferedWriter(sys.stderr), encoding="utf-8", line_buffering=True)
88+
sys.stderr.mode = "w"
89+
sys.__stderr__ = sys.stderr
8990

9091

9192
# ----------------------------------------------------------------------------------------------------------------------

0 commit comments

Comments
 (0)