forked from jython/jython
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
If META-INF/services/org.python.core.JythonInitializer is on the classpath, the class named in that file will be instantiated and used in Jython's initialization. This is useful when Jython is initialized by a library outside of your control, but some customization still needs to be done to Jython's environment. I promise not to add a JythonFactory or a JythonFactoryFactory.
- Loading branch information
Showing
8 changed files
with
206 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import sys | ||
assert "/from_SyspathAppendingInitializer_with_love" in sys.path |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import unittest | ||
import subprocess | ||
import sys | ||
from test import test_support | ||
|
||
class TestUsingInitializer(unittest.TestCase): | ||
def test_syspath_initializer(self): | ||
fn = test_support.findfile("check_for_initializer_in_syspath.py") | ||
ret = subprocess.Popen([sys.executable, fn], | ||
env={"CLASSPATH":"tests/data/initializer"}).wait() | ||
self.assertEquals(0, ret) | ||
|
||
def test_main(): | ||
test_support.run_unittest(TestUsingInitializer) | ||
|
||
if __name__ == "__main__": | ||
test_main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package org.python.core; | ||
|
||
import java.util.Properties; | ||
|
||
import org.python.core.adapter.ExtensiblePyObjectAdapter; | ||
|
||
/** | ||
* A service for initializing Jython without explicitly calling {@link PySystemState#initialize}. If | ||
* a file META-INF/services/org.python.core.JythonInitializer is on the classpath, Jython will | ||
* instantiate the class named in that file and use it in Jython's initialization. The given class | ||
* must be an implementation of this interface with a no-arg constructor. | ||
* | ||
* @see <a href="http://java.sun.com/j2se/1.5.0/docs/guide/jar/jar.html#Service%20Provider">Java | ||
Service Providers</a> | ||
*/ | ||
public interface JythonInitializer { | ||
|
||
/** | ||
* Called from {@link PySystemState#initialize} with the full set of initialization arguments. | ||
* Implementations may modify or replace the given arguments, and must call | ||
* {@link PySystemState#doInitialize}. | ||
* | ||
* @param argv | ||
* - The command line arguments the jython interpreter was started with, or an empty | ||
* array if jython wasn't started directly from the command line. | ||
* @param classLoader | ||
* - The classloader to be used by sys, or null if no sys-specific classloader was | ||
* specified | ||
*/ | ||
void initialize(Properties preProperties, | ||
Properties postProperties, | ||
String[] argv, | ||
ClassLoader classLoader, | ||
ExtensiblePyObjectAdapter adapter); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
tests/data/initializer/META-INF/services/org.python.core.JythonInitializer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
SyspathAppendingInitializer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import java.util.Properties; | ||
import org.python.core.JythonInitializer; | ||
import org.python.core.Py; | ||
import org.python.core.PySystemState; | ||
import org.python.core.adapter.ExtensiblePyObjectAdapter; | ||
|
||
public class SyspathAppendingInitializer implements JythonInitializer { | ||
public void initialize(Properties preProperties, | ||
Properties postProperties, | ||
String[] argv, | ||
ClassLoader classLoader, | ||
ExtensiblePyObjectAdapter adapter) { | ||
postProperties.put(PySystemState.PYTHON_CACHEDIR_SKIP, "true"); | ||
PySystemState defaultState = | ||
PySystemState.doInitialize(preProperties, postProperties, argv, classLoader, adapter); | ||
defaultState.path.append(Py.newString("/from_SyspathAppendingInitializer_with_love")); | ||
} | ||
} |