Skip to content
Snippets Groups Projects
Commit c4a1c141 authored by felmer's avatar felmer
Browse files

SSDM-3745: Jython27: Execute everything with the right context class loader...

SSDM-3745: Jython27: Execute everything with the right context class loader otherwise python xml module wouldn't work

SVN: 36769
parent 1eadf135
No related branches found
No related tags found
No related merge requests found
...@@ -190,7 +190,7 @@ public final class Evaluator27 implements IJythonEvaluator ...@@ -190,7 +190,7 @@ public final class Evaluator27 implements IJythonEvaluator
{ {
pyArgs[i] = translateToPython(args[i]); pyArgs[i] = translateToPython(args[i]);
} }
PyObject result = func.__call__(pyArgs); PyObject result = JythonUtils.invokeFunction(func, pyArgs);
return translateToJava(result); return translateToJava(result);
} catch (PyException ex) } catch (PyException ex)
{ {
......
...@@ -18,6 +18,7 @@ package ch.systemsx.cisd.common.jython.v27; ...@@ -18,6 +18,7 @@ package ch.systemsx.cisd.common.jython.v27;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.Callable;
import org.python27.core.Py; import org.python27.core.Py;
import org.python27.core.PyDictionary; import org.python27.core.PyDictionary;
...@@ -25,6 +26,8 @@ import org.python27.core.PyFunction; ...@@ -25,6 +26,8 @@ import org.python27.core.PyFunction;
import org.python27.core.PyObject; import org.python27.core.PyObject;
import org.python27.core.PySequenceList; import org.python27.core.PySequenceList;
import ch.systemsx.cisd.base.exceptions.CheckedExceptionTunnel;
/** /**
* Jython utility methods. * Jython utility methods.
* *
...@@ -76,7 +79,39 @@ class JythonUtils ...@@ -76,7 +79,39 @@ class JythonUtils
{ {
pyArgs[i] = Py.java2py(args[i]); pyArgs[i] = Py.java2py(args[i]);
} }
return function.__call__(pyArgs); return invokeFunction(function, pyArgs);
}
static PyObject invokeFunction(final PyFunction function, final PyObject[] pyArgs)
{
return executeWithContextClassLoader(function, new Callable<PyObject>()
{
@Override
public PyObject call() throws Exception
{
return function.__call__(pyArgs);
}
});
}
static final <V> V executeWithContextClassLoader(Object object, Callable<V> action)
{
Thread thread = Thread.currentThread();
ClassLoader originalContextClassLoader = thread.getContextClassLoader();
try
{
thread.setContextClassLoader(object.getClass().getClassLoader());
try
{
return action.call();
} catch (Exception ex)
{
throw CheckedExceptionTunnel.wrapIfNecessary(ex);
}
} finally
{
thread.setContextClassLoader(originalContextClassLoader);
}
} }
} }
...@@ -16,6 +16,8 @@ ...@@ -16,6 +16,8 @@
package ch.systemsx.cisd.common.jython.v27; package ch.systemsx.cisd.common.jython.v27;
import java.util.concurrent.Callable;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.python27.core.CompileMode; import org.python27.core.CompileMode;
import org.python27.core.Py; import org.python27.core.Py;
...@@ -131,7 +133,35 @@ class PythonInterpreter27 extends org.python27.util.PythonInterpreter ...@@ -131,7 +133,35 @@ class PythonInterpreter27 extends org.python27.util.PythonInterpreter
return new PythonInterpreter27(); return new PythonInterpreter27();
} }
public void exec(String data, String filename) @Override
public void exec(final String s)
{
JythonUtils.executeWithContextClassLoader(this, new Callable<Void>()
{
@Override
public Void call() throws Exception
{
PythonInterpreter27.super.exec(s);
return null;
}
});
}
@Override
public void exec(final PyObject code)
{
JythonUtils.executeWithContextClassLoader(this, new Callable<Void>()
{
@Override
public Void call() throws Exception
{
PythonInterpreter27.super.exec(code);
return null;
}
});
}
public void exec(final String data, final String filename)
{ {
String[] pythonPath = ch.systemsx.cisd.common.jython.JythonUtils.getScriptDirectoryPythonPath(filename); String[] pythonPath = ch.systemsx.cisd.common.jython.JythonUtils.getScriptDirectoryPythonPath(filename);
...@@ -143,7 +173,15 @@ class PythonInterpreter27 extends org.python27.util.PythonInterpreter ...@@ -143,7 +173,15 @@ class PythonInterpreter27 extends org.python27.util.PythonInterpreter
setSystemState(); setSystemState();
Py.exec(Py.compile_flags(data, filename, CompileMode.exec, cflags), getLocals(), null); JythonUtils.executeWithContextClassLoader(this, new Callable<Void>()
{
@Override
public Void call() throws Exception
{
Py.exec(Py.compile_flags(data, filename, CompileMode.exec, cflags), getLocals(), null);
return null;
}
});
Py.flushLine(); Py.flushLine();
} finally } finally
{ {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment