From: <cg...@us...> - 2008-11-09 19:46:27
|
Revision: 5558 http://jython.svn.sourceforge.net/jython/?rev=5558&view=rev Author: cgroves Date: 2008-11-09 19:46:24 +0000 (Sun, 09 Nov 2008) Log Message: ----------- Set the name of every PythonInterpreter module to __main__, not just those created through org.python.util.jython. Fixes proxy class lookup for proxies created in the interpreter's module. Modified Paths: -------------- trunk/jython/src/org/python/util/PythonInterpreter.java trunk/jython/src/org/python/util/jython.java Modified: trunk/jython/src/org/python/util/PythonInterpreter.java =================================================================== --- trunk/jython/src/org/python/util/PythonInterpreter.java 2008-11-08 00:08:20 UTC (rev 5557) +++ trunk/jython/src/org/python/util/PythonInterpreter.java 2008-11-09 19:46:24 UTC (rev 5558) @@ -1,41 +1,47 @@ -// Copyright (c) Corporation for National Research Initiatives package org.python.util; -import org.python.core.*; -import java.util.*; +import java.util.Properties; + +import org.python.core.CompilerFlags; +import org.python.core.Py; +import org.python.core.PyCode; +import org.python.core.PyException; +import org.python.core.PyFile; +import org.python.core.PyModule; +import org.python.core.PyObject; +import org.python.core.PyString; +import org.python.core.PyStringMap; +import org.python.core.PySystemState; +import org.python.core.__builtin__; + /** - * The PythonInterpreter class is a standard wrapper for a Jython - * interpreter for use embedding in a Java application. - * - * @author Jim Hugunin - * @version 1.0, 02/23/97 + * The PythonInterpreter class is a standard wrapper for a Jython interpreter for use embedding in a + * Java application. */ - public class PythonInterpreter { + PyModule module; + protected PySystemState systemState; + PyObject locals; protected CompilerFlags cflags = new CompilerFlags(); /** - * Initializes the jython runtime. This should only be called once, and - * should be called before any other python objects are created (including a - * PythonInterpreter). + * Initializes the jython runtime. This should only be called once, and should be called before + * any other python objects are created (including a PythonInterpreter). * * @param preProperties * A set of properties. Typically System.getProperties() is used. * @param postProperties - * An other set of properties. Values like python.home, - * python.path and all other values from the registry files can - * be added to this property set. PostProperties will override - * system properties and registry properties. + * An other set of properties. Values like python.home, python.path and all other + * values from the registry files can be added to this property set. PostProperties + * will override system properties and registry properties. * @param argv * Command line argument. These values will assigned to sys.argv. */ - public static void initialize(Properties preProperties, - Properties postProperties, - String[] argv) { + public static void initialize(Properties preProperties, Properties postProperties, String[] argv) { PySystemState.initialize(preProperties, postProperties, argv); } @@ -47,29 +53,27 @@ } /** - * Create a new interpreter with the given dictionary to use as its - * namespace - * - * @param dict the dictionary to use + * Create a new interpreter with the given dictionary to use as its namespace */ - - // Optional dictionary will be used for locals namespace public PythonInterpreter(PyObject dict) { this(dict, null); } public PythonInterpreter(PyObject dict, PySystemState systemState) { - if (dict == null) + if (dict == null) { dict = new PyStringMap(); + } if (systemState == null) { systemState = Py.getSystemState(); - if (systemState == null) + if (systemState == null) { systemState = new PySystemState(); + } } - module = new PyModule("__main__", dict); this.systemState = systemState; - locals = module.__dict__; setState(); + module = new PyModule("__main__", dict); + systemState.modules.__setitem__("__main__", module); + locals = dict; } protected void setState() { @@ -79,7 +83,8 @@ /** * Set the Python object to use for the standard output stream * - * @param outStream Python file-like object to use as output stream + * @param outStream + * Python file-like object to use as output stream */ public void setOut(PyObject outStream) { systemState.stdout = outStream; @@ -88,7 +93,8 @@ /** * Set a java.io.OutputStream to use for the standard output stream * - * @param outStream OutputStream to use as output stream + * @param outStream + * OutputStream to use as output stream */ public void setOut(java.io.OutputStream outStream) { setOut(new PyFile(outStream)); @@ -104,8 +110,6 @@ /** * Evaluate a string as Python source and return the result - * - * @param s the string to evaluate */ public PyObject eval(String s) { setState(); @@ -114,20 +118,15 @@ /** * Execute a string of Python source in the local namespace - * - * @param s the string to execute */ public void exec(String s) { setState(); - Py.exec(Py.compile_flags(s, "<string>", "exec",cflags), - locals, locals); + Py.exec(Py.compile_flags(s, "<string>", "exec", cflags), locals, locals); Py.flushLine(); } /** * Execute a Python code object in the local namespace - * - * @param code the code object to execute */ public void exec(PyObject code) { setState(); @@ -137,12 +136,10 @@ /** * Execute a file of Python source in the local namespace - * - * @param s the name of the file to execute */ - public void execfile(String s) { + public void execfile(String filename) { setState(); - __builtin__.execfile_flags(s, locals, locals, cflags); + __builtin__.execfile_flags(filename, locals, locals, cflags); Py.flushLine(); } @@ -152,11 +149,10 @@ public void execfile(java.io.InputStream s, String name) { setState(); - Py.runCode((PyCode)Py.compile_flags(s, name, "exec",cflags), locals, locals); + Py.runCode((PyCode)Py.compile_flags(s, name, "exec", cflags), locals, locals); Py.flushLine(); } - // Getting and setting the locals dictionary public PyObject getLocals() { return locals; } @@ -168,10 +164,12 @@ /** * Set a variable in the local namespace * - * @param name the name of the variable - * @param value the value to set the variable to. - Will be automatically converted to an appropriate Python object. - */ + * @param name + * the name of the variable + * @param value + * the value to set the variable to. Will be automatically converted to an + * appropriate Python object. + */ public void set(String name, Object value) { locals.__setitem__(name.intern(), Py.java2py(value)); } @@ -179,8 +177,10 @@ /** * Set a variable in the local namespace * - * @param name the name of the variable - * @param value the value to set the variable to + * @param name + * the name of the variable + * @param value + * the value to set the variable to */ public void set(String name, PyObject value) { locals.__setitem__(name.intern(), value); @@ -198,21 +198,19 @@ } /** - * Get the value of a variable in the local namespace Value will be returned - * as an instance of the given Java class. - * <code>interp.get("foo", Object.class)</code> will return the most + * Get the value of a variable in the local namespace Value will be returned as an instance of + * the given Java class. <code>interp.get("foo", Object.class)</code> will return the most * appropriate generic Java object. * * @param name * the name of the variable * @param javaclass * the class of object to return - * @return the value of the variable as the given class, or null if that - * name isn't assigned + * @return the value of the variable as the given class, or null if that name isn't assigned */ public <T> T get(String name, Class<T> javaclass) { PyObject val = locals.__finditem__(name.intern()); - if(val == null) { + if (val == null) { return null; } return Py.tojava(val, javaclass); Modified: trunk/jython/src/org/python/util/jython.java =================================================================== --- trunk/jython/src/org/python/util/jython.java 2008-11-08 00:08:20 UTC (rev 5557) +++ trunk/jython/src/org/python/util/jython.java 2008-11-09 19:46:24 UTC (rev 5558) @@ -16,7 +16,6 @@ import org.python.core.PyException; import org.python.core.PyFile; import org.python.core.PyList; -import org.python.core.PyModule; import org.python.core.PyString; import org.python.core.PyStringMap; import org.python.core.PySystemState; @@ -302,26 +301,17 @@ } /** - * @return a new python interpreter, instantiating the right - * InteractiveConsole subclass for the configured <tt>python.console</tt> + * Returns a new python interpreter using the InteractiveConsole subclass from the + * <tt>python.console</tt> registry key. */ private static InteractiveConsole newInterpreter() { - InteractiveConsole interp = null; try { - String interpClass = PySystemState.registry.getProperty( - "python.console", - "org.python.util.InteractiveConsole"); - interp = (InteractiveConsole) - Class.forName(interpClass).newInstance(); + String interpClass = PySystemState.registry.getProperty("python.console", + "org.python.util.InteractiveConsole"); + return (InteractiveConsole)Class.forName(interpClass).newInstance(); } catch (Exception e) { - interp = new InteractiveConsole(); -} - - //System.err.println("interp"); - PyModule mod = imp.addModule("__main__"); - interp.setLocals(mod.__dict__); - //System.err.println("imp"); - return interp; + return new InteractiveConsole(); + } } /** This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <cg...@us...> - 2009-06-23 06:19:30
|
Revision: 6496 http://jython.svn.sourceforge.net/jython/?rev=6496&view=rev Author: cgroves Date: 2009-06-23 06:17:57 +0000 (Tue, 23 Jun 2009) Log Message: ----------- Break an initializer clas out of PyServlet to keep PyServlet from being a ServletContextListener. Modified Paths: -------------- trunk/jython/src/org/python/util/PyFilter.java trunk/jython/src/org/python/util/PyServlet.java Added Paths: ----------- trunk/jython/src/org/python/util/PyServletInitializer.java Modified: trunk/jython/src/org/python/util/PyFilter.java =================================================================== --- trunk/jython/src/org/python/util/PyFilter.java 2009-06-23 02:20:07 UTC (rev 6495) +++ trunk/jython/src/org/python/util/PyFilter.java 2009-06-23 06:17:57 UTC (rev 6496) @@ -43,17 +43,16 @@ * </p> * * <pre> - * <!-- PyFilter depends on PyServlet --> - * <servlet> - * <servlet-name>PyServlet</servlet-name> - * <servlet-class>org.python.util.PyServlet</servlet-class> - * <load-on-startup>1</load-on-startup> - * </servlet> + * <!-- Initialize the Jython runtime --> + * <listener> + * <listener-class>org.python.util.PyServletInitializer</listener-class> + * <load-on-startup>1</load-on-startup> + * </listener> * * <!-- Declare a uniquely-named PyFilter --> * <filter> * <filter-name>HeaderFilter</filter-name> - * <filter-class>org.jython.util.PyFilter</filter-class> + * <filter-class>org.python.util.PyFilter</filter-class> * * <!-- The special param __filter__ gives the context-relative path to the Jython source file --> * <init-param> @@ -72,20 +71,6 @@ * <url-pattern>/*</url-pattern> * </filter-mapping> * </pre> - * - * <p> - * PyFilter depends on initialization code from PyServlet being run. If PyServlet is used to serve - * pages, this code will be executed and PyFilter will work properly. However, if you aren't using - * PyServlet, the initialization code can be invoked as a ServletContextListener instead of as an - * HttpServlet. Use the following in web.xml instead of a servlet tag: - * - * <pre> - * <listener> - * <listener-class>org.python.util.PyServlet</listener-class> - * <load-on-startup>1</load-on-startup> - * </listener> - * </pre> - * */ public class PyFilter implements Filter { public static final String FILTER_PATH_PARAM = "__filter__"; Modified: trunk/jython/src/org/python/util/PyServlet.java =================================================================== --- trunk/jython/src/org/python/util/PyServlet.java 2009-06-23 02:20:07 UTC (rev 6495) +++ trunk/jython/src/org/python/util/PyServlet.java 2009-06-23 06:17:57 UTC (rev 6496) @@ -9,8 +9,6 @@ import java.util.regex.Pattern; import javax.servlet.ServletContext; -import javax.servlet.ServletContextEvent; -import javax.servlet.ServletContextListener; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; @@ -62,18 +60,12 @@ * * </pre> */ -public class PyServlet extends HttpServlet implements ServletContextListener { +public class PyServlet extends HttpServlet { public static final String SKIP_INIT_NAME = "skip_jython_initialization"; protected static final String INIT_ATTR = "__jython_initialized__"; - public void contextInitialized(ServletContextEvent event) { - init(new Properties(), event.getServletContext(), "a ServletContextListener", true); - } - - public void contextDestroyed(ServletContextEvent event) {} - @Override public void init() { Properties props = new Properties(); @@ -83,8 +75,18 @@ String name = (String)e.nextElement(); props.put(name, getInitParameter(name)); } - init(props, getServletContext(), "the servlet " + getServletConfig().getServletName(), - getServletConfig().getInitParameter(SKIP_INIT_NAME) != null); + boolean initialize = getServletConfig().getInitParameter(SKIP_INIT_NAME) != null; + if (getServletContext().getAttribute(INIT_ATTR) != null) { + if (initialize) { + System.err.println("Jython has already been initialized in this context, not " + + "initializing for " + getServletName() + ". Add " + SKIP_INIT_NAME + + " to as an init param to this servlet's configuration to indicate this " + + "is expected."); + } + } else if (initialize) { + init(props, getServletContext()); + } + reset(); } /** @@ -92,46 +94,37 @@ * servlet, and this is the shared init code. If both initializations are used in a single * context, the system state initialization code only runs once. */ - private void init(Properties props, ServletContext context, String initializerName, - boolean initialize) { - rootPath = context.getRealPath("/"); + protected static void init(Properties props, ServletContext context) { + String rootPath = getRootPath(context); + context.setAttribute(INIT_ATTR, true); + Properties baseProps = PySystemState.getBaseProperties(); + // Context parameters + Enumeration<?> e = context.getInitParameterNames(); + while (e.hasMoreElements()) { + String name = (String)e.nextElement(); + props.put(name, context.getInitParameter(name)); + } + if (props.getProperty("python.home") == null + && baseProps.getProperty("python.home") == null) { + props.put("python.home", rootPath + "WEB-INF" + File.separator + "lib"); + } + PySystemState.initialize(baseProps, props, new String[0]); + PySystemState.add_package("javax.servlet"); + PySystemState.add_package("javax.servlet.http"); + PySystemState.add_package("javax.servlet.jsp"); + PySystemState.add_package("javax.servlet.jsp.tagext"); + PySystemState.add_classdir(rootPath + "WEB-INF" + File.separator + "classes"); + PySystemState.add_extdir(rootPath + "WEB-INF" + File.separator + "lib", true); + } + + protected static String getRootPath(ServletContext context) { + String rootPath = context.getRealPath("/"); if (!rootPath.endsWith(File.separator)) { rootPath += File.separator; } - if (context.getAttribute(INIT_ATTR) != null) { - if (initialize) { - System.err.println("Jython has already been initialized by " - + context.getAttribute(INIT_ATTR) - + " in this context, not initializing for " + initializerName + ". Add " - + SKIP_INIT_NAME - + " to as an init param to this servlet's configuration to indicate this " - + "is expected."); - } - } else if (initialize) { - context.setAttribute(INIT_ATTR, initializerName); - Properties baseProps = PySystemState.getBaseProperties(); - // Context parameters - Enumeration<?> e = context.getInitParameterNames(); - while (e.hasMoreElements()) { - String name = (String)e.nextElement(); - props.put(name, context.getInitParameter(name)); - } - if (props.getProperty("python.home") == null - && baseProps.getProperty("python.home") == null) { - props.put("python.home", rootPath + "WEB-INF" + File.separator + "lib"); - } - PySystemState.initialize(baseProps, props, new String[0]); - PySystemState.add_package("javax.servlet"); - PySystemState.add_package("javax.servlet.http"); - PySystemState.add_package("javax.servlet.jsp"); - PySystemState.add_package("javax.servlet.jsp.tagext"); - PySystemState.add_classdir(rootPath + "WEB-INF" + File.separator + "classes"); - PySystemState.add_extdir(rootPath + "WEB-INF" + File.separator + "lib", true); - } - reset(); + return rootPath; } - @Override public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException @@ -161,6 +154,7 @@ * requests. */ public void reset() { + String rootPath = getRootPath(getServletContext()); destroyCache(); interp = new PythonInterpreter(null, new PySystemState()); @@ -246,6 +240,5 @@ private static final Pattern FIND_NAME = Pattern.compile("([^/]+)\\.py$"); private PythonInterpreter interp; - private String rootPath; private Map<String, CacheEntry> cache = Generic.map(); } \ No newline at end of file Added: trunk/jython/src/org/python/util/PyServletInitializer.java =================================================================== --- trunk/jython/src/org/python/util/PyServletInitializer.java (rev 0) +++ trunk/jython/src/org/python/util/PyServletInitializer.java 2009-06-23 06:17:57 UTC (rev 6496) @@ -0,0 +1,26 @@ +package org.python.util; + +import java.util.Properties; + +import javax.servlet.ServletContextEvent; +import javax.servlet.ServletContextListener; + +/** + * Initializes the jython runtime inside a servlet engine. Should be used with {@link PyFilter} to + * initialize the system before the filter starts. Add the following to web.xml to run the + * initializer: + * + * <pre> + * <listener> + * <listener-class>org.python.util.PyServletInitializer</listener-class> + * <load-on-startup>1</load-on-startup> + * </listener> + *</pre> + */ +public class PyServletInitializer implements ServletContextListener { + public void contextInitialized(ServletContextEvent evt) { + PyServlet.init(new Properties(), evt.getServletContext()); + } + + public void contextDestroyed(ServletContextEvent evt) {} +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <cg...@us...> - 2009-06-29 06:13:31
|
Revision: 6502 http://jython.svn.sourceforge.net/jython/?rev=6502&view=rev Author: cgroves Date: 2009-06-29 06:13:29 +0000 (Mon, 29 Jun 2009) Log Message: ----------- Add the root dir and WEB-INF/jython to sys.path for PyFilters as well as PyServlets Modified Paths: -------------- trunk/jython/src/org/python/util/PyFilter.java trunk/jython/src/org/python/util/PyServlet.java Modified: trunk/jython/src/org/python/util/PyFilter.java =================================================================== --- trunk/jython/src/org/python/util/PyFilter.java 2009-06-29 05:42:24 UTC (rev 6501) +++ trunk/jython/src/org/python/util/PyFilter.java 2009-06-29 06:13:29 UTC (rev 6502) @@ -11,7 +11,6 @@ import javax.servlet.ServletResponse; import org.python.core.PyException; -import org.python.core.PySystemState; import org.python.util.PythonInterpreter; /** @@ -96,7 +95,7 @@ if (!source.exists()) { throw new ServletException(source.getAbsolutePath() + " does not exist."); } - interp = new PythonInterpreter(null, new PySystemState()); + interp = PyServlet.createInterpreter(config.getServletContext()); } private String getRealPath(ServletContext context, String appPath) { Modified: trunk/jython/src/org/python/util/PyServlet.java =================================================================== --- trunk/jython/src/org/python/util/PyServlet.java 2009-06-29 05:42:24 UTC (rev 6501) +++ trunk/jython/src/org/python/util/PyServlet.java 2009-06-29 06:13:29 UTC (rev 6502) @@ -19,6 +19,7 @@ import org.python.core.PyException; import org.python.core.PyObject; import org.python.core.PyString; +import org.python.core.PyStringMap; import org.python.core.PySystemState; /** @@ -117,6 +118,17 @@ PySystemState.add_extdir(rootPath + "WEB-INF" + File.separator + "lib", true); } + protected static PythonInterpreter createInterpreter(ServletContext servletContext) { + String rootPath = getRootPath(servletContext); + PySystemState sys = new PySystemState(); + PythonInterpreter interp = new PythonInterpreter(new PyStringMap(), sys); + sys.path.append(new PyString(rootPath)); + + String modulesDir = rootPath + "WEB-INF" + File.separator + "jython"; + sys.path.append(new PyString(modulesDir)); + return interp; + } + protected static String getRootPath(ServletContext context) { String rootPath = context.getRealPath("/"); if (!rootPath.endsWith(File.separator)) { @@ -154,15 +166,8 @@ * requests. */ public void reset() { - String rootPath = getRootPath(getServletContext()); destroyCache(); - interp = new PythonInterpreter(null, new PySystemState()); - - PySystemState sys = Py.getSystemState(); - sys.path.append(new PyString(rootPath)); - - String modulesDir = rootPath + "WEB-INF" + File.separator + "jython"; - sys.path.append(new PyString(modulesDir)); + interp = createInterpreter(getServletContext()); } private synchronized HttpServlet getServlet(String path) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <cg...@us...> - 2009-06-29 06:25:29
|
Revision: 6503 http://jython.svn.sourceforge.net/jython/?rev=6503&view=rev Author: cgroves Date: 2009-06-29 06:25:28 +0000 (Mon, 29 Jun 2009) Log Message: ----------- Explain how to set python.home for the context initializer Modified Paths: -------------- trunk/jython/src/org/python/util/PyServlet.java trunk/jython/src/org/python/util/PyServletInitializer.java Modified: trunk/jython/src/org/python/util/PyServlet.java =================================================================== --- trunk/jython/src/org/python/util/PyServlet.java 2009-06-29 06:13:29 UTC (rev 6502) +++ trunk/jython/src/org/python/util/PyServlet.java 2009-06-29 06:25:28 UTC (rev 6503) @@ -50,7 +50,7 @@ * <servlet-class>org.python.util.PyServlet</servlet-class> * <init-param> * <param-name>python.home</param-name> - * <param-value>/usr/home/jython-2.1</param-value> + * <param-value>/usr/home/jython-2.5</param-value> * </init-param> * </servlet> * <servlet-mapping> Modified: trunk/jython/src/org/python/util/PyServletInitializer.java =================================================================== --- trunk/jython/src/org/python/util/PyServletInitializer.java 2009-06-29 06:13:29 UTC (rev 6502) +++ trunk/jython/src/org/python/util/PyServletInitializer.java 2009-06-29 06:25:28 UTC (rev 6503) @@ -16,6 +16,17 @@ * <load-on-startup>1</load-on-startup> * </listener> *</pre> + * + * To use modules from Python's standard library in servlets and filters initialized by this + * listener, either add the standard library to the lib directory in WEB-INF, or add python.home as + * a context-param. The latter can be done by adding the following to web.xml: + * + * <pre> + * <context-param> + * <param-name>python.home</param-name> + * <param-value>/usr/local/jython-2.5</param-value> + * </context-param> + * </pre> */ public class PyServletInitializer implements ServletContextListener { public void contextInitialized(ServletContextEvent evt) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |