[Jython-checkins] jython: Fixing an issue relating to the site module

alan.kennedy jython-checkins at python.org
Sun Nov 9 19:02:16 CET 2014


https://hg.python.org/jython/rev/fcd4dfc05813
changeset:   7412:fcd4dfc05813
user:        Alan Kennedy <alan at xhaus.com>
date:        Sun Nov 09 18:01:46 2014 +0000
summary:
  Fixing an issue relating to the site module

files:
  NEWS                                   |   1 +
  src/com/xhaus/modjy/ModjyJServlet.java |  41 ++++++++-----
  2 files changed, 26 insertions(+), 16 deletions(-)


diff --git a/NEWS b/NEWS
--- a/NEWS
+++ b/NEWS
@@ -2,6 +2,7 @@
 
 Jython 2.7b3
   Bugs Fixed
+    - [ 2225 ] Jython+django-jython - no module named site
     - [ 2108 ] Cannot set attribute to instances of AST/PythonTree (blocks pyflakes)
     - [ 1497 ] ast classes do not have appropiate base classes
     - [ 1980 ] ast.Eq, ast.Gt, ast.GtE, ast.In, ast.Is, ast.IsNot, ast.Lt, ast.LtE, ast.NotEq and ast.NotIn should be subclasses of ast.cmpop
diff --git a/src/com/xhaus/modjy/ModjyJServlet.java b/src/com/xhaus/modjy/ModjyJServlet.java
--- a/src/com/xhaus/modjy/ModjyJServlet.java
+++ b/src/com/xhaus/modjy/ModjyJServlet.java
@@ -33,7 +33,7 @@
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
-import org.python.core.imp;
+import org.python.core.Options;
 import org.python.core.Py;
 import org.python.core.PyException;
 import org.python.core.PyObject;
@@ -103,6 +103,9 @@
     public void init() throws ServletException {
         try {
             Properties props = readConfiguration();
+            // We check for site packages settings before initialising the runtime
+            // https://hg.python.org/jython/rev/51b28cc2c43d
+            checkSitePackages(props);
             PythonInterpreter.initialize(System.getProperties(), props, new String[0]);
             PySystemState systemState = new PySystemState();
             interp = new PythonInterpreter(null, systemState);
@@ -149,8 +152,9 @@
     }
 
     /**
-     * Setup the modjy environment, i.e. 1. Find the location of the modjy.jar file and add it to
-     * sys.path 2. Process the WEB-INF/lib-python directory, if it exists
+     * Setup the modjy environment, i.e.
+     *    1. Find the location of the modjy.jar file and add it to sys.path
+     *    2. Process the WEB-INF/lib-python directory, if it exists
      *
      * @param interp
      *            - The PythonInterpreter used to service requests
@@ -163,22 +167,22 @@
                                     Properties props,
                                     PySystemState systemState) throws PyException {
         processPythonLib(interp, systemState);
-        checkSitePackages(props);
     }
 
     /**
      * Check if the user has requested to initialise the jython installation "site-packages".
+     * The value defaults to true, i.e. load site packages
      *
      * @param props
      *            - The properties from which config options are found
      */
     protected void checkSitePackages(Properties props) throws PyException {
+        boolean loadSitePackages = true;
         String loadSitePackagesParam = props.getProperty(LOAD_SITE_PACKAGES_PARAM);
-        boolean loadSitePackages = true;
-        if (loadSitePackagesParam != null && loadSitePackagesParam.trim().compareTo("0") == 0)
+        if (loadSitePackagesParam != null && loadSitePackagesParam.trim().compareTo("0") == 0) {
             loadSitePackages = false;
-        if (loadSitePackages)
-            imp.load("site");
+        }
+        Options.importSite = loadSitePackages;
     }
 
     /**
@@ -192,17 +196,21 @@
     protected void processPythonLib(PythonInterpreter interp, PySystemState systemState) {
         // Add the lib-python directory to sys.path
         String pythonLibPath = getServletContext().getRealPath(LIB_PYTHON);
-        if (pythonLibPath == null)
+        if (pythonLibPath == null) {
             return;
+        }
         File pythonLib = new File(pythonLibPath);
-        if (!pythonLib.exists())
+        if (!pythonLib.exists()) {
             return;
+        }
         systemState.path.append(new PyString(pythonLibPath));
         // Now check for .pth files in lib-python and process each one
         String[] libPythonContents = pythonLib.list();
-        for (String libPythonContent : libPythonContents)
-            if (libPythonContent.endsWith(PTH_FILE_EXTENSION))
+        for (String libPythonContent : libPythonContents) {
+            if (libPythonContent.endsWith(PTH_FILE_EXTENSION)) {
                 processPthFile(interp, systemState, pythonLibPath, libPythonContent);
+            }
+        }
     }
 
     /**
@@ -227,12 +235,13 @@
             String line;
             while ((line = lineReader.readLine()) != null) {
                 line = line.trim();
-                if (line.length() == 0)
+                if (line.length() == 0) {
                     continue;
-                if (line.startsWith("#"))
+                }
+                if (line.startsWith("#")) {
                     continue;
-                if (line.startsWith("import"))
-                {
+                }
+                if (line.startsWith("import")) {
                     interp.exec(line);
                     continue;
                 }

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


More information about the Jython-checkins mailing list