From: <cg...@us...> - 2009-07-12 08:07:52
|
Revision: 6533 http://jython.svn.sourceforge.net/jython/?rev=6533&view=rev Author: cgroves Date: 2009-07-12 08:07:51 +0000 (Sun, 12 Jul 2009) Log Message: ----------- Add javaproxy_dir to sys, and if it's set, compile Java proxies to that directory. Modified Paths: -------------- trunk/jython/Lib/test/test_java_subclasses.py trunk/jython/src/org/python/core/MakeProxies.java trunk/jython/src/org/python/core/Py.java trunk/jython/src/org/python/core/PySystemState.java Added Paths: ----------- trunk/jython/Lib/test/import_as_java_class.py trunk/jython/Lib/test/static_proxy.py Added: trunk/jython/Lib/test/import_as_java_class.py =================================================================== --- trunk/jython/Lib/test/import_as_java_class.py (rev 0) +++ trunk/jython/Lib/test/import_as_java_class.py 2009-07-12 08:07:51 UTC (rev 6533) @@ -0,0 +1,7 @@ +# Part of test_java_subclasses.StaticProxyCompilationTest +from java.lang import Class + +# Grab the proxy class statically compiled by the containing test +cls = Class.forName("test.static_proxy.RunnableImpl") +# Instantiating the proxy class should import the module containing it and create the Python side +assert cls.newInstance().meth() == 78 Added: trunk/jython/Lib/test/static_proxy.py =================================================================== --- trunk/jython/Lib/test/static_proxy.py (rev 0) +++ trunk/jython/Lib/test/static_proxy.py 2009-07-12 08:07:51 UTC (rev 6533) @@ -0,0 +1,11 @@ +# Part of test_java_subclasses.StaticProxyCompilationTest. This needs to be its own module +# so the statically compiled proxy can import it. +from java.lang import Runnable + +class RunnableImpl(Runnable): + __javaname__ = "test.static_proxy.RunnableImpl" + def run(self): + pass + + def meth(self): + return 78 Modified: trunk/jython/Lib/test/test_java_subclasses.py =================================================================== --- trunk/jython/Lib/test/test_java_subclasses.py 2009-07-12 05:41:43 UTC (rev 6532) +++ trunk/jython/Lib/test/test_java_subclasses.py 2009-07-12 08:07:51 UTC (rev 6533) @@ -1,6 +1,8 @@ '''Tests subclassing Java classes in Python''' import os import sys +import tempfile +import subprocess import unittest from test import test_support @@ -345,6 +347,26 @@ except TypeError: pass +class StaticProxyCompilationTest(unittest.TestCase): + def setUp(self): + self.orig_proxy_dir = sys.javaproxy_dir + sys.javaproxy_dir = tempfile.mkdtemp() + + def tearDown(self): + sys.javaproxy_dir = self.orig_proxy_dir + + def test_proxies_without_classloader(self): + # importing with proxy_dir set compiles RunnableImpl there + import static_proxy + + # Use the existing environment with the proxy dir added on the classpath + env = dict(os.environ) + env["CLASSPATH"] = sys.javaproxy_dir + script = test_support.findfile("import_as_java_class.py") + self.assertEquals(subprocess.call([sys.executable, "-J-Dpython.cachedir.skip=true", + script], env=env), + 0) + def test_main(): test_support.run_unittest(InterfaceTest, TableModelTest, @@ -352,4 +374,5 @@ PythonSubclassesTest, AbstractOnSyspathTest, ContextClassloaderTest, - SettingJavaClassNameTest) + SettingJavaClassNameTest, + StaticProxyCompilationTest) Modified: trunk/jython/src/org/python/core/MakeProxies.java =================================================================== --- trunk/jython/src/org/python/core/MakeProxies.java 2009-07-12 05:41:43 UTC (rev 6532) +++ trunk/jython/src/org/python/core/MakeProxies.java 2009-07-12 08:07:51 UTC (rev 6533) @@ -61,6 +61,10 @@ "must be valid Java identifiers: " + "http://java.sun.com/docs/books/jls/second_edition/html/lexical.doc.html#40625"); } + Class<?> proxy = Py.findClass(fullProxyName); + if (proxy != null) { + return proxy; + } } else { fullProxyName = proxyPrefix + proxyName + "$" + proxyNumber++; } @@ -81,7 +85,11 @@ jm.build(); ByteArrayOutputStream bytes = new ByteArrayOutputStream(); jm.classfile.write(bytes); - Py.saveClassFile(fullProxyName, bytes); + if (customProxyName != null) { + Py.saveClassFile(fullProxyName, bytes, Py.getSystemState().javaproxy_dir); + } else { + Py.saveClassFile(fullProxyName, bytes); + } return makeClass(superclass, vinterfaces, jm.myClass, bytes); } catch (Exception exc) { Modified: trunk/jython/src/org/python/core/Py.java =================================================================== --- trunk/jython/src/org/python/core/Py.java 2009-07-12 05:41:43 UTC (rev 6532) +++ trunk/jython/src/org/python/core/Py.java 2009-07-12 08:07:51 UTC (rev 6533) @@ -830,8 +830,9 @@ public static void initProxy(PyProxy proxy, String module, String pyclass, Object[] args) { - if (proxy._getPyInstance() != null) + if (proxy._getPyInstance() != null) { return; + } ThreadState ts = getThreadState(); PyObject instance = ts.getInitializingProxy(); if (instance != null) { @@ -1759,14 +1760,17 @@ } public static void saveClassFile(String name, ByteArrayOutputStream bytestream) { - String dirname = Options.proxyDebugDirectory; + saveClassFile(name, bytestream, Options.proxyDebugDirectory); + } + + public static void saveClassFile(String name, ByteArrayOutputStream baos, String dirname) { if (dirname == null) { return; } - - byte[] bytes = bytestream.toByteArray(); + byte[] bytes = baos.toByteArray(); File dir = new File(dirname); File file = makeFilename(name, dir); + new File(file.getParent()).mkdirs(); try { FileOutputStream o = new FileOutputStream(file); Modified: trunk/jython/src/org/python/core/PySystemState.java =================================================================== --- trunk/jython/src/org/python/core/PySystemState.java 2009-07-12 05:41:43 UTC (rev 6532) +++ trunk/jython/src/org/python/core/PySystemState.java 2009-07-12 08:07:51 UTC (rev 6533) @@ -40,6 +40,7 @@ public static final String PYTHON_CACHEDIR_SKIP = "python.cachedir.skip"; public static final String PYTHON_CONSOLE_ENCODING = "python.console.encoding"; protected static final String CACHEDIR_DEFAULT_NAME = "cachedir"; + public static final String PYTHON_JAVAPROXYDIR = "python.javaproxydir"; public static final String JYTHON_JAR = "jython.jar"; public static final String JYTHON_DEV_JAR = "jython-dev.jar"; @@ -138,6 +139,13 @@ public PyObject last_type = Py.None; public PyObject last_traceback = Py.None; + private static String defaultJavaProxyDir; + + /** + * The directory where named Java proxies are written. + */ + public String javaproxy_dir; + public PyObject __name__ = new PyString("sys"); public PyObject __dict__; @@ -189,6 +197,8 @@ __dict__.invoke("update", getType().fastGetDict()); __dict__.__setitem__("displayhook", __displayhook__); __dict__.__setitem__("excepthook", __excepthook__); + + javaproxy_dir = defaultJavaProxyDir; } void reload() throws PyIgnoreMethodTag { @@ -857,6 +867,8 @@ // other initializations initBuiltins(registry); initStaticFields(); + defaultJavaProxyDir = registry.getProperty(PYTHON_JAVAPROXYDIR); + // Initialize the path (and add system defaults) defaultPath = initPath(registry, standalone, jarFileName); defaultArgv = initArgv(argv); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |