[Jython-checkins] jython: Clarify contract of Py.isInteractive and use it in util.jython.run()

jeff.allen jython-checkins at python.org
Fri May 18 14:18:10 EDT 2018


https://hg.python.org/jython/rev/44ac35e36677
changeset:   8164:44ac35e36677
user:        Jeff Allen <ja.py at farowl.co.uk>
date:        Fri May 18 18:41:49 2018 +0100
summary:
  Clarify contract of Py.isInteractive and use it in util.jython.run()

Change motivated by #2656, we avoid a warning in interactive mode but not yet
for file arguments. See also #2686.

files:
  src/org/python/core/Py.java     |  44 +++++++++++---------
  src/org/python/util/jython.java |  17 +++++--
  2 files changed, 37 insertions(+), 24 deletions(-)


diff --git a/src/org/python/core/Py.java b/src/org/python/core/Py.java
--- a/src/org/python/core/Py.java
+++ b/src/org/python/core/Py.java
@@ -1726,31 +1726,37 @@
     }
 
     /**
-     * Check (using the {@link POSIX} library and <code>jnr-posix</code> library) whether we are in
-     * an interactive environment. Amongst other things, this affects the type of console that may
-     * be legitimately installed during system initialisation. Note that the result may vary
-     * according to whether a <code>jnr-posix</code> native library is found along
-     * <code>java.library.path</code>, or the pure Java fall-back is used.
+     * Determine whether <b>standard input</b> is an interactive stream. This is not the same as
+     * deciding whether the interpreter is or should be in interactive mode. Amongst other things,
+     * this affects the type of console that may be legitimately installed during system
+     * initialisation.
+     * <p>
+     * If the Java system property {@code python.launcher.tty} is defined and equal to {@code true}
+     * or {@code false}, then that provides the result. This property is normally supplied by the
+     * launcher. In the absence of this certainty, we try to find outusing {@code isatty()} in the
+     * Posix emulation library. Note that the result may vary according to whether a
+     * <code>jnr-posix</code> native library is found along <code>java.library.path</code>, or the
+     * pure Java fall-back is used.
      *
-     * @return true if (we think) we are in an interactive environment
+     * @return true if (we think) standard input is an interactive stream
      */
     public static boolean isInteractive() {
-        // python.launcher.tty is authoratative; see http://bugs.jython.org/issue2325
-        String isTTY = System.getProperty("python.launcher.tty");
-        if (isTTY != null && isTTY.equals("true")) {
-            return true;
+        String tty = System.getProperty("python.launcher.tty");
+        if (tty != null) {
+            // python.launcher.tty is authoritative; see http://bugs.jython.org/issue2325
+            tty = tty.toLowerCase();
+            if (tty.equals("true")) {
+                return true;
+            } else if (tty.equals("false")) {
+                return false;
+            }
         }
-        if (isTTY != null && isTTY.equals("false")) {
-            return false;
-        }
-        // Decide if System.in is interactive
+        // Base decision on whether System.in is interactive according to OS
         try {
             POSIX posix = POSIXFactory.getPOSIX();
-            FileDescriptor in = FileDescriptor.in;
-            return posix.isatty(in);
-        } catch (SecurityException ex) {
-            return false;
-        }
+            return posix.isatty(FileDescriptor.in);
+        } catch (SecurityException ex) {}
+        return false;
     }
 
     private static final String IMPORT_SITE_ERROR = ""
diff --git a/src/org/python/util/jython.java b/src/org/python/util/jython.java
--- a/src/org/python/util/jython.java
+++ b/src/org/python/util/jython.java
@@ -219,6 +219,7 @@
     }
 
     public static void run(String[] args) {
+
         // Parse the command line options
         CommandLineOptions opts = new CommandLineOptions();
         if (!opts.parse(args)) {
@@ -249,10 +250,13 @@
             addDefault(preProperties, PySystemState.PYTHON_IO_ERRORS, spec[1]);
         }
 
+        // If/when we interact with standard input, will we use a line-editing console?
+        boolean stdinIsInteractive = Py.isInteractive();
+
         // Decide if System.in is interactive
         if (!opts.fixInteractive || opts.interactive) {
             // The options suggest System.in is interactive: but only if isatty() agrees
-            opts.interactive = Py.isInteractive();
+            opts.interactive = stdinIsInteractive;
             if (opts.interactive) {
                 // Set the default console type if nothing else has
                 addDefault(preProperties, "python.console", PYTHON_CONSOLE_CLASS);
@@ -517,9 +521,12 @@
 class CommandLineOptions {
 
     public String filename;
-    public boolean jar, interactive, notice;
+    public boolean jar, notice;
     public boolean runCommand, runModule;
-    public boolean fixInteractive;
+    /** True unless a file, module, jar, or command argument awaits execution. */
+    public boolean interactive = true;
+    /** Eventually go interactive: reflects the -i ("inspect") flag. */
+    public boolean fixInteractive = false;
     public boolean help, version;
     public String[] argv;
     public Properties properties;
@@ -531,8 +538,8 @@
 
     public CommandLineOptions() {
         filename = null;
-        jar = fixInteractive = false;
-        interactive = notice = true;
+        jar = false;
+        notice = true;
         runModule = false;
         properties = new Properties();
         help = version = false;

-- 
Repository URL: https://hg.python.org/jython


More information about the Jython-checkins mailing list