From: <am...@us...> - 2009-10-15 17:40:30
|
Revision: 6859 http://jython.svn.sourceforge.net/jython/?rev=6859&view=rev Author: amak Date: 2009-10-15 17:40:22 +0000 (Thu, 15 Oct 2009) Log Message: ----------- Checking in support for loading site-packages, through the use of imp.load("site"). Controlled with a "load_site_packages" option, which defaults to true. Modified Paths: -------------- trunk/jython/src/com/xhaus/modjy/ModjyJServlet.java trunk/jython/tests/modjy/java/com/xhaus/modjy/ModjyTestBase.java trunk/jython/tests/modjy/test_apps_dir/lifecycle_tests.py Added Paths: ----------- trunk/jython/tests/modjy/java/com/xhaus/modjy/ModjyTestInterpreterLifecycle.java Removed Paths: ------------- trunk/jython/tests/modjy/java/com/xhaus/modjy/ModjyTestServletLifecycle.java Modified: trunk/jython/src/com/xhaus/modjy/ModjyJServlet.java =================================================================== --- trunk/jython/src/com/xhaus/modjy/ModjyJServlet.java 2009-10-14 06:54:56 UTC (rev 6858) +++ trunk/jython/src/com/xhaus/modjy/ModjyJServlet.java 2009-10-15 17:40:22 UTC (rev 6859) @@ -33,6 +33,7 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import org.python.core.imp; import org.python.core.Py; import org.python.core.PyException; import org.python.core.PyObject; @@ -49,6 +50,8 @@ protected final static String PTH_FILE_EXTENSION = ".pth"; + protected final static String LOAD_SITE_PACKAGES_PARAM = "load_site_packages"; + protected PythonInterpreter interp; protected HttpServlet modjyServlet; @@ -140,7 +143,7 @@ * sys.path 2. Process the WEB-INF/lib-python directory, if it exists * * @param interp - * - The PythinInterpreter used to service requests + * - The PythonInterpreter used to service requests * @param props * - The properties from which config options are found * @param systemState @@ -148,11 +151,27 @@ */ protected void setupEnvironment(PythonInterpreter interp, Properties props, - PySystemState systemState) { + PySystemState systemState) throws PyException { + checkSitePackages(props); processPythonLib(interp, systemState); } /** + * Check if the user has requested to initialise the jython installation "site-packages". + * + * @param props + * - The properties from which config options are found + */ + protected void checkSitePackages(Properties props) throws PyException { + String loadSitePackagesParam = props.getProperty(LOAD_SITE_PACKAGES_PARAM); + boolean loadSitePackages = true; + if (loadSitePackagesParam != null && loadSitePackagesParam.trim().compareTo("0") == 0) + loadSitePackages = false; + if (loadSitePackages) + imp.load("site"); + } + + /** * Do all processing in relation to the lib-python subdirectory of WEB-INF * * @param interp Modified: trunk/jython/tests/modjy/java/com/xhaus/modjy/ModjyTestBase.java =================================================================== --- trunk/jython/tests/modjy/java/com/xhaus/modjy/ModjyTestBase.java 2009-10-14 06:54:56 UTC (rev 6858) +++ trunk/jython/tests/modjy/java/com/xhaus/modjy/ModjyTestBase.java 2009-10-15 17:40:22 UTC (rev 6859) @@ -238,7 +238,7 @@ suite.addTestSuite(ModjyTestHeaders.class); suite.addTestSuite(ModjyTestContentHeaders.class); suite.addTestSuite(ModjyTestReturnIterable.class); - suite.addTestSuite(ModjyTestServletLifecycle.class); + suite.addTestSuite(ModjyTestInterpreterLifecycle.class); suite.addTestSuite(ModjyTestWebInf.class); suite.addTestSuite(ModjyTestWSGIStreams.class); suite.addTestSuite(PyServletTest.class); Copied: trunk/jython/tests/modjy/java/com/xhaus/modjy/ModjyTestInterpreterLifecycle.java (from rev 6842, trunk/jython/tests/modjy/java/com/xhaus/modjy/ModjyTestServletLifecycle.java) =================================================================== --- trunk/jython/tests/modjy/java/com/xhaus/modjy/ModjyTestInterpreterLifecycle.java (rev 0) +++ trunk/jython/tests/modjy/java/com/xhaus/modjy/ModjyTestInterpreterLifecycle.java 2009-10-15 17:40:22 UTC (rev 6859) @@ -0,0 +1,62 @@ +/*### +# +# Copyright Alan Kennedy. +# +# You may contact the copyright holder at this uri: +# +# http://www.xhaus.com/contact/modjy +# +# The licence under which this code is released is the Apache License v2.0. +# +# The terms and conditions of this license are listed in a file contained +# in the distribution that also contained this file, under the name +# LICENSE.txt. +# +# You may also read a copy of the license at the following web address. +# +# http://modjy.xhaus.com/LICENSE.txt +# +###*/ + +package com.xhaus.modjy; + +import javax.servlet.http.HttpServlet; + +public class ModjyTestInterpreterLifecycle extends ModjyTestBase { + + protected void lifecycleTestSetUp() throws Exception { + baseSetUp(); + setAppFile("lifecycle_tests.py"); + } + + public void testAtExitHandlersCalled() throws Exception { + System.setProperty("modjy", "here"); + lifecycleTestSetUp(); + createServlet(); + doGet(); + HttpServlet modjyServlet = getServlet(); + modjyServlet.destroy(); + assertEquals("gone", System.getProperty("modjy")); + } + + public void testSitePackagesLoaded() throws Exception { + for (int loadSitePackages = -1 ; loadSitePackages < 2 ; loadSitePackages++) { + lifecycleTestSetUp(); + setAppName("load_site_packages_test"); + String parameter, expectedResult; + if (loadSitePackages != -1) { + parameter = Integer.toString(loadSitePackages); + setInitParameter("load_site_packages", parameter); + expectedResult = parameter; + } else { + // Do not set the parameter, so we get default behaviour + expectedResult = "1"; + } + createServlet(); + doGet(); + String result = getOutput(); + assertEquals(expectedResult, result); + } + } + +} Property changes on: trunk/jython/tests/modjy/java/com/xhaus/modjy/ModjyTestInterpreterLifecycle.java ___________________________________________________________________ Added: svn:mergeinfo + /branches/jsr223/tests/modjy/java/com/xhaus/modjy/ModjyTestServletLifecycle.java:6285-6565 /branches/newstyle-java-types/tests/modjy/java/com/xhaus/modjy/ModjyTestServletLifecycle.java:5564-5663,5666-5729 Deleted: trunk/jython/tests/modjy/java/com/xhaus/modjy/ModjyTestServletLifecycle.java =================================================================== --- trunk/jython/tests/modjy/java/com/xhaus/modjy/ModjyTestServletLifecycle.java 2009-10-14 06:54:56 UTC (rev 6858) +++ trunk/jython/tests/modjy/java/com/xhaus/modjy/ModjyTestServletLifecycle.java 2009-10-15 17:40:22 UTC (rev 6859) @@ -1,42 +0,0 @@ -/*### -# -# Copyright Alan Kennedy. -# -# You may contact the copyright holder at this uri: -# -# http://www.xhaus.com/contact/modjy -# -# The licence under which this code is released is the Apache License v2.0. -# -# The terms and conditions of this license are listed in a file contained -# in the distribution that also contained this file, under the name -# LICENSE.txt. -# -# You may also read a copy of the license at the following web address. -# -# http://modjy.xhaus.com/LICENSE.txt -# -###*/ - -package com.xhaus.modjy; - -import javax.servlet.http.HttpServlet; - -public class ModjyTestServletLifecycle extends ModjyTestBase { - - protected void lifecycleTestSetUp() throws Exception { - baseSetUp(); - setAppFile("lifecycle_tests.py"); - } - - public void testAtExitHandlersCalled() throws Exception { - System.setProperty("modjy", "here"); - lifecycleTestSetUp(); - createServlet(); - doGet(); - HttpServlet modjyServlet = getServlet(); - modjyServlet.destroy(); - assertEquals("gone", System.getProperty("modjy")); - } - -} Modified: trunk/jython/tests/modjy/test_apps_dir/lifecycle_tests.py =================================================================== --- trunk/jython/tests/modjy/test_apps_dir/lifecycle_tests.py 2009-10-14 06:54:56 UTC (rev 6858) +++ trunk/jython/tests/modjy/test_apps_dir/lifecycle_tests.py 2009-10-15 17:40:22 UTC (rev 6859) @@ -29,6 +29,11 @@ atexit.register(exit_func) def lifecycle_test(environ, start_response): - writer = start_response("200 OK", []) - writer("Hello World!") - return [] + start_response("200 OK", []) + return ["Hello World!"] + +def load_site_packages_test(environ, start_response): + import sys + start_response("200 OK", []) + response = {True: '1', False: '0'}[sys.modules.has_key('site')] + return [response] This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |