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. |