[Jython-checkins] jython: Move current logging methods to PrePy.

jeff.allen jython-checkins at python.org
Thu Aug 8 02:45:47 EDT 2019


https://hg.python.org/jython/rev/011472a1af03
changeset:   8271:011472a1af03
user:        Jeff Allen <ja.py at farowl.co.uk>
date:        Sun Aug 04 12:32:08 2019 +0100
summary:
  Move current logging methods to PrePy.

It should be possible to use logging methods without waking the
sleeping giant that is the Jython Runtime. Also, simplify Options.java,
starting with the verbosity control.

files:
  src/org/python/core/Options.java |  122 ++++++++----------
  src/org/python/core/PrePy.java   |   45 +++++++
  src/org/python/core/Py.java      |   31 ----
  3 files changed, 102 insertions(+), 96 deletions(-)


diff --git a/src/org/python/core/Options.java b/src/org/python/core/Options.java
--- a/src/org/python/core/Options.java
+++ b/src/org/python/core/Options.java
@@ -77,7 +77,7 @@
      * Py.DEBUG for varying levels of informative messages from Jython. Normally
      * this option is set from the command line.
      */
-    public static int verbose = Py.MESSAGE;
+    public static int verbose = PrePy.MESSAGE;
 
     /**
      * Set by the {@code -i} option to the interpreter command, to ask for an interactive session to
@@ -161,16 +161,15 @@
      * CacheBuilderSpec string and affects how the SRE_STATE cache will behave/evict
      * cached PyString -> int[] code points.
      */
-    public static final String sreCacheSpecDefault = "weakKeys,concurrencyLevel=4,maximumWeight=2621440,expireAfterAccess=30s";
+    public static final String sreCacheSpecDefault =
+            "weakKeys,concurrencyLevel=4,maximumWeight=2621440,expireAfterAccess=30s";
     public static String sreCacheSpec = sreCacheSpecDefault;
 
     //
     // ####### END OF OPTIONS
     //
 
-    private Options() {
-        ;
-    }
+    private Options() {}
 
     private static boolean getBooleanOption(String name, boolean defaultValue) {
         String prop = PySystemState.registry.getProperty(name);
@@ -188,74 +187,67 @@
         return prop;
     }
 
-    /**
-     * Initialize the static fields from the registry options.
-     */
+    /** Initialize the static fields from the registry options. */
     public static void setFromRegistry() {
         // Set the more unusual options
-        Options.showJavaExceptions = getBooleanOption(
-                PYTHON_OPTIONS_SHOW_JAVA_EXCEPTIONS, 
-                Options.showJavaExceptions);
-
-        Options.includeJavaStackInExceptions = getBooleanOption(
-        	PYTHON_OPTIONS_INCLUDE_JAVA_STACK_IN_EXCEPTIONS,
-                Options.includeJavaStackInExceptions);
-
-        Options.showPythonProxyExceptions = getBooleanOption(
-                PYTHON_OPTIONS_SHOW_PYTHON_PROXY_EXCEPTIONS,
-                Options.showPythonProxyExceptions);
-
-        Options.respectJavaAccessibility = getBooleanOption(
-                PYTHON_SECURITY_RESPECT_JAVA_ACCESSIBILITY,
-                Options.respectJavaAccessibility);
-
-        Options.proxyDebugDirectory = getStringOption(
-                PYTHON_OPTIONS_PROXY_DEBUG_DIRECTORY, 
-                Options.proxyDebugDirectory);
+        showJavaExceptions =
+                getBooleanOption(PYTHON_OPTIONS_SHOW_JAVA_EXCEPTIONS, showJavaExceptions);
+        includeJavaStackInExceptions = getBooleanOption(
+                PYTHON_OPTIONS_INCLUDE_JAVA_STACK_IN_EXCEPTIONS, includeJavaStackInExceptions);
+        showPythonProxyExceptions = getBooleanOption(
+                PYTHON_OPTIONS_SHOW_PYTHON_PROXY_EXCEPTIONS, showPythonProxyExceptions);
+        respectJavaAccessibility = getBooleanOption(
+                PYTHON_SECURITY_RESPECT_JAVA_ACCESSIBILITY, respectJavaAccessibility);
+        proxyDebugDirectory =
+                getStringOption(PYTHON_OPTIONS_PROXY_DEBUG_DIRECTORY, proxyDebugDirectory);
 
         // verbosity is more complicated:
-        String prop = PySystemState.registry.getProperty(PYTHON_VERBOSE);
-        if (prop != null) {
-            if (prop.equalsIgnoreCase("error")) {
-                Options.verbose = Py.ERROR;
-            } else if (prop.equalsIgnoreCase("warning")) {
-                Options.verbose = Py.WARNING;
-            } else if (prop.equalsIgnoreCase("message")) {
-                Options.verbose = Py.MESSAGE;
-            } else if (prop.equalsIgnoreCase("comment")) {
-                Options.verbose = Py.COMMENT;
-            } else if (prop.equalsIgnoreCase("debug")) {
-                Options.verbose = Py.DEBUG;
-            } else {
-                throw Py.ValueError("Illegal verbose option setting: '" + prop
-                        + "'");
-            }
+        String prop;
+        switch ((prop = getStringOption(PYTHON_VERBOSE, "")).toLowerCase()) {
+            case "":
+                break;
+            case "error":
+                verbose = PrePy.ERROR;
+                break;
+            case "warning":
+                verbose = PrePy.WARNING;
+                break;
+            case "message":
+                verbose = PrePy.MESSAGE;
+                break;
+            case "comment":
+                verbose = PrePy.COMMENT;
+                break;
+            case "debug":
+                verbose = PrePy.DEBUG;
+                break;
+            default:
+                throw new IllegalArgumentException("Invalid verbose option: '" + prop + "'");
         }
 
-        Options.caseok = getBooleanOption(PYTHON_OPTIONS_CASE_OK, 
-                Options.caseok);
-
-        Options.Qnew = getBooleanOption(PYTHON_OPTIONS_Q_NEW, Options.Qnew);
+        caseok = getBooleanOption(PYTHON_OPTIONS_CASE_OK, caseok);
+        Qnew = getBooleanOption(PYTHON_OPTIONS_Q_NEW, Qnew);
 
-        prop = PySystemState.registry.getProperty(PYTHON_DIVISION_WARNING);
-        if (prop != null) {
-            if (prop.equalsIgnoreCase("old")) {
-                Options.division_warning = 0;
-            } else if (prop.equalsIgnoreCase("warn")) {
-                Options.division_warning = 1;
-            } else if (prop.equalsIgnoreCase("warnall")) {
-                Options.division_warning = 2;
-            } else {
-                throw Py.ValueError("Illegal division_warning option "
-                        + "setting: '" + prop + "'");
-            }
+        switch ((prop = getStringOption(PYTHON_DIVISION_WARNING, "")).toLowerCase()) {
+            case "":
+                break;
+            case "old":
+                division_warning = 0;
+                break;
+            case "warn":
+                division_warning = 1;
+                break;
+            case "warnall":
+                division_warning = 2;
+                break;
+            default:
+                throw new IllegalArgumentException(
+                        "Invalid division_warning option: '" + prop + "'");
         }
 
-        Options.sreCacheSpec = getStringOption(PYTHON_SRE_CACHESPEC, 
-                Options.sreCacheSpec);
-        Options.inspect |= getStringOption(PYTHON_INSPECT, "").length() > 0;
-        Options.importSite = getBooleanOption(PYTHON_IMPORT_SITE, 
-                Options.importSite);
-        Options.no_site = !Options.importSite;
+        sreCacheSpec = getStringOption(PYTHON_SRE_CACHESPEC, sreCacheSpec);
+        inspect |= getStringOption(PYTHON_INSPECT, "").length() > 0;
+        importSite = getBooleanOption(PYTHON_IMPORT_SITE, importSite);
+        no_site = !importSite;
     }
 }
diff --git a/src/org/python/core/PrePy.java b/src/org/python/core/PrePy.java
--- a/src/org/python/core/PrePy.java
+++ b/src/org/python/core/PrePy.java
@@ -30,6 +30,51 @@
 // Do not refer to any PyObject, Py export or PySystemState in this class.
 public class PrePy {
 
+    // Logging convenience functions are here so they may work before the Jython runtime starts.
+
+    /** {@link Options#verbose} level indicating an error that prevents correct results. */
+    public static final int ERROR = -1;
+    /** {@link Options#verbose} level indicating an unexpected event, still working correctly. */
+    public static final int WARNING = 0;
+    /** {@link Options#verbose} level for messages that confirm correct functioning. */
+    public static final int MESSAGE = 1;
+    /** {@link Options#verbose} level providing detail during correct functioning. */
+    public static final int COMMENT = 2;
+    /** {@link Options#verbose} level providing detail in support of debugging or tracing. */
+    public static final int DEBUG = 3;
+
+    /** Log a message at a specified level (if that level be not below the threshold). */
+    public static void maybeWrite(String type, String msg, int level) {
+        if (level <= Options.verbose) {
+            System.err.println(type + ": " + msg);
+        }
+    }
+
+    /** Submit a message to logging at the severity level ERROR. */
+    public static void writeError(String type, String msg) {
+        maybeWrite(type, msg, ERROR);
+    }
+
+    /** Submit a message to logging at the severity level WARNING. */
+    public static void writeWarning(String type, String msg) {
+        maybeWrite(type, msg, WARNING);
+    }
+
+    /** Submit a message to logging at the severity level MESSAGE. */
+    public static void writeMessage(String type, String msg) {
+        maybeWrite(type, msg, MESSAGE);
+    }
+
+    /** Submit a message to logging at the severity level COMMENT. */
+    public static void writeComment(String type, String msg) {
+        maybeWrite(type, msg, COMMENT);
+    }
+
+    /** Submit a message to logging at the severity level DEBUG. */
+    public static void writeDebug(String type, String msg) {
+        maybeWrite(type, msg, DEBUG);
+    }
+
     /**
      * Get the System properties if we are allowed to. Configuration values set via
      * {@code -Dprop=value} to the java command will be found here. If a security manager prevents
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
@@ -2319,37 +2319,6 @@
     public static void printResult(PyObject ret) {
         Py.getThreadState().getSystemState().invoke("displayhook", ret);
     }
-    public static final int ERROR = -1;
-    public static final int WARNING = 0;
-    public static final int MESSAGE = 1;
-    public static final int COMMENT = 2;
-    public static final int DEBUG = 3;
-
-    public static void maybeWrite(String type, String msg, int level) {
-        if (level <= Options.verbose) {
-            System.err.println(type + ": " + msg);
-        }
-    }
-
-    public static void writeError(String type, String msg) {
-        maybeWrite(type, msg, ERROR);
-    }
-
-    public static void writeWarning(String type, String msg) {
-        maybeWrite(type, msg, WARNING);
-    }
-
-    public static void writeMessage(String type, String msg) {
-        maybeWrite(type, msg, MESSAGE);
-    }
-
-    public static void writeComment(String type, String msg) {
-        maybeWrite(type, msg, COMMENT);
-    }
-
-    public static void writeDebug(String type, String msg) {
-        maybeWrite(type, msg, DEBUG);
-    }
 
     public static void saveClassFile(String name, ByteArrayOutputStream bytestream) {
         String dirname = Options.proxyDebugDirectory;

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


More information about the Jython-checkins mailing list