Yes I did track the problem. Attached is the diff for JythonEngine.java summar of change:
1) I've added propertyChange method to set the ClassLoader for jython 2) modify execute method to call PySystemState.add_package(packageName) when there's a from packageName import className in python script When can this get incoporated? I dont want to maintain my own private subversion repository to keep track of the these minor bug fixes which is what im currently doing. On 10/16/06, Rony G. Flatscher <[EMAIL PROTECTED]> wrote:
Hi Sonny, the developers are dispersed at the moment (I think no one is at home, but travelling from/to conferences). Quoting Sonny To <[EMAIL PROTECTED]>: > I asked this question on the bsf-user mailing list but it doesnt seem > to be very active. The bsf-user archieve at > http://mail-archives.eu.apache.org/mod_mbox/jakarta-bsf-usr/ is also > not available. I'm working on an opensource project > http://code.google.com/p/wulumuqi/ that integrates BSF. Any help > getting BSF to use URLClassLoaders will be much appreciated. It seems from the stack traces that the problem is in the BSF engines for the respective languages. So you would be best served by tracking down the engine-creators and inform them about the problem. Alternatively, if you can trace it down and fix it (if it is a true error, nothing that is linked to security issues, or not being able to retrieve the class from the net, and the like), it would be great, if you could make a unified diff available (which we could apply at least to the Jython engine, as its source-code is in the BSF distro itself). Good luck! ---rony --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
18a19 > import java.beans.PropertyChangeEvent; 20a22,23 > import java.util.regex.Matcher; > import java.util.regex.Pattern; 30a34 > import org.python.core.PySystemState; 34,39c38,43 < * This is the interface to Jython (http://www.jython.org/) from BSF. < * It's derived from the JPython 1.x engine < * < * @author Sanjiva Weerawarana < * @author Finn Bock <[EMAIL PROTECTED]> < * @author Chuck Murcko --- > * This is the interface to Jython (http://www.jython.org/) from BSF. It's > * derived from the JPython 1.x engine > * > * @author Sanjiva Weerawarana > * @author Finn Bock <[EMAIL PROTECTED]> > * @author Chuck Murcko 43,243c47,253 < BSFPythonInterpreter interp; < < /** < * call the named method of the given object. < */ < public Object call (Object object, String method, Object[] args) < throws BSFException { < try { < PyObject[] pyargs = Py.EmptyObjects; < < if (args != null) { < pyargs = new PyObject[args.length]; < for (int i = 0; i < pyargs.length; i++) < pyargs[i] = Py.java2py(args[i]); < } < < if (object != null) { < PyObject o = Py.java2py(object); < return unwrap(o.invoke(method, pyargs)); < } < < PyObject m = interp.get(method); < < if (m == null) < m = interp.eval(method); < if (m != null) { < return unwrap(m.__call__(pyargs)); < } < < return null; < } catch (PyException e) { < throw new BSFException (BSFException.REASON_EXECUTION_ERROR, < "exception from Jython:\n" + e, e); < } < } < < /** < * Declare a bean < */ < public void declareBean (BSFDeclaredBean bean) throws BSFException { < interp.set (bean.name, bean.bean); < } < < /** < * Evaluate an anonymous function (differs from eval() in that apply() < * handles multiple lines). < */ < public Object apply (String source, int lineNo, int columnNo, < Object funcBody, Vector paramNames, < Vector arguments) throws BSFException { < try { < /* We wrapper the original script in a function definition, and < * evaluate the function. A hack, no question, but it allows < * apply() to pretend to work on Jython. < */ < StringBuffer script = new StringBuffer(byteify(funcBody.toString())); < int index = 0; < script.insert(0, "def bsf_temp_fn():\n"); < < while (index < script.length()) { < if (script.charAt(index) == '\n') { < script.insert(index+1, '\t'); < } < index++; < } < < interp.exec (script.toString ()); < < Object result = interp.eval ("bsf_temp_fn()"); < < if (result != null && result instanceof PyJavaInstance) < result = ((PyJavaInstance)result).__tojava__(Object.class); < return result; < } catch (PyException e) { < throw new BSFException (BSFException.REASON_EXECUTION_ERROR, < "exception from Jython:\n" + e, e); < } < } < < /** < * Evaluate an expression. < */ < public Object eval (String source, int lineNo, int columnNo, < Object script) throws BSFException { < try { < Object result = interp.eval (byteify(script.toString ())); < if (result != null && result instanceof PyJavaInstance) < result = ((PyJavaInstance)result).__tojava__(Object.class); < return result; < } catch (PyException e) { < throw new BSFException (BSFException.REASON_EXECUTION_ERROR, < "exception from Jython:\n" + e, e); < } < } < < /** < * Execute a script. < */ < public void exec (String source, int lineNo, int columnNo, < Object script) throws BSFException { < try { < interp.exec (byteify(script.toString ())); < } catch (PyException e) { < throw new BSFException (BSFException.REASON_EXECUTION_ERROR, < "exception from Jython:\n" + e, e); < } < } < < /** < * Execute script code, emulating console interaction. < */ < public void iexec (String source, int lineNo, int columnNo, < Object script) throws BSFException { < String scriptStr = byteify(script.toString()); < int newline = scriptStr.indexOf("\n"); < < if (newline > -1) < scriptStr = scriptStr.substring(0, newline); < < try { < if (interp.buffer.length() > 0) < interp.buffer.append("\n"); < interp.buffer.append(scriptStr); < if (!(interp.runsource(interp.buffer.toString()))) < interp.resetbuffer(); < } catch (PyException e) { < interp.resetbuffer(); < throw new BSFException(BSFException.REASON_EXECUTION_ERROR, < "exception from Jython:\n" + e, e); < } < } < < /** < * Initialize the engine. < */ < public void initialize (BSFManager mgr, String lang, < Vector declaredBeans) throws BSFException { < super.initialize (mgr, lang, declaredBeans); < < // create an interpreter < interp = new BSFPythonInterpreter (); < < // ensure that output and error streams are re-directed correctly < interp.setOut(System.out); < interp.setErr(System.err); < < // register the mgr with object name "bsf" < interp.set ("bsf", new BSFFunctions (mgr, this)); < < // Declare all declared beans to the interpreter < int size = declaredBeans.size (); < for (int i = 0; i < size; i++) { < declareBean ((BSFDeclaredBean) declaredBeans.elementAt (i)); < } < } < < /** < * Undeclare a previously declared bean. < */ < public void undeclareBean (BSFDeclaredBean bean) throws BSFException { < interp.set (bean.name, null); < } < < public Object unwrap(PyObject result) { < if (result != null) { < Object ret = result.__tojava__(Object.class); < if (ret != Py.NoConversion) < return ret; < } < return result; < } < < private String byteify (String orig) { < // Ugh. Jython likes to be fed bytes, rather than the input string. < ByteArrayInputStream bais = < new ByteArrayInputStream(orig.getBytes()); < StringBuffer s = new StringBuffer(); < int c; < < while ((c = bais.read()) >= 0) { < s.append((char)c); < } < < return s.toString(); < } < < private class BSFPythonInterpreter extends InteractiveInterpreter { < < public BSFPythonInterpreter() { < super(); < } < < // Override runcode so as not to print the stack dump < public void runcode(PyObject code) { < try { < this.exec(code); < } catch (PyException exc) { < throw exc; < } < } < } --- > BSFPythonInterpreter interp; > private final static Pattern fromRegExp = Pattern.compile("from > ([.^\\S]*)"); > /** > * call the named method of the given object. > */ > public Object call(Object object, String method, Object[] args) throws > BSFException { > try { > PyObject[] pyargs = Py.EmptyObjects; > > if (args != null) { > pyargs = new PyObject[args.length]; > for (int i = 0; i < pyargs.length; i++) > pyargs[i] = Py.java2py(args[i]); > } > > if (object != null) { > PyObject o = Py.java2py(object); > return unwrap(o.invoke(method, pyargs)); > } > > PyObject m = interp.get(method); > > if (m == null) > m = interp.eval(method); > if (m != null) { > return unwrap(m.__call__(pyargs)); > } > > return null; > } catch (PyException e) { > throw new > BSFException(BSFException.REASON_EXECUTION_ERROR, "exception from Jython:\n" > + e, e); > } > } > > /** > * Declare a bean > */ > public void declareBean(BSFDeclaredBean bean) throws BSFException { > interp.set(bean.name, bean.bean); > } > > /** > * Evaluate an anonymous function (differs from eval() in that apply() > * handles multiple lines). > */ > public Object apply(String source, int lineNo, int columnNo, Object > funcBody, Vector paramNames, Vector arguments) > throws BSFException { > try { > /* > * We wrapper the original script in a function > definition, and > * evaluate the function. A hack, no question, but it > allows apply() > * to pretend to work on Jython. > */ > StringBuffer script = new > StringBuffer(byteify(funcBody.toString())); > int index = 0; > script.insert(0, "def bsf_temp_fn():\n"); > > while (index < script.length()) { > if (script.charAt(index) == '\n') { > script.insert(index + 1, '\t'); > } > index++; > } > > interp.exec(script.toString()); > > Object result = interp.eval("bsf_temp_fn()"); > > if (result != null && result instanceof PyJavaInstance) > result = ((PyJavaInstance) > result).__tojava__(Object.class); > return result; > } catch (PyException e) { > throw new > BSFException(BSFException.REASON_EXECUTION_ERROR, "exception from Jython:\n" > + e, e); > } > } > > /** > * Evaluate an expression. > */ > public Object eval(String source, int lineNo, int columnNo, Object > script) throws BSFException { > try { > Object result = interp.eval(byteify(script.toString())); > if (result != null && result instanceof PyJavaInstance) > result = ((PyJavaInstance) > result).__tojava__(Object.class); > return result; > } catch (PyException e) { > throw new > BSFException(BSFException.REASON_EXECUTION_ERROR, "exception from Jython:\n" > + e, e); > } > } > > /** > * Execute a script. > */ > public void exec(String source, int lineNo, int columnNo, Object > script) throws BSFException { > try { > String scriptString = script.toString(); > Matcher matcher = fromRegExp.matcher(scriptString); > while (matcher.find()) { > String packageName = matcher.group(1); > PySystemState.add_package(packageName); > } > > interp.exec(byteify(scriptString)); > } catch (PyException e) { > throw new > BSFException(BSFException.REASON_EXECUTION_ERROR, "exception from Jython:\n" > + e, e); > } > } > > /** > * Execute script code, emulating console interaction. > */ > public void iexec(String source, int lineNo, int columnNo, Object > script) throws BSFException { > String scriptStr = byteify(script.toString()); > int newline = scriptStr.indexOf("\n"); > > if (newline > -1) > scriptStr = scriptStr.substring(0, newline); > > try { > if (interp.buffer.length() > 0) > interp.buffer.append("\n"); > interp.buffer.append(scriptStr); > if (!(interp.runsource(interp.buffer.toString()))) > interp.resetbuffer(); > } catch (PyException e) { > interp.resetbuffer(); > throw new > BSFException(BSFException.REASON_EXECUTION_ERROR, "exception from Jython:\n" > + e, e); > } > } > > /** > * Initialize the engine. > */ > public void initialize(BSFManager mgr, String lang, Vector > declaredBeans) throws BSFException { > super.initialize(mgr, lang, declaredBeans); > > // create an interpreter > interp = new BSFPythonInterpreter(); > > // ensure that output and error streams are re-directed > correctly > interp.setOut(System.out); > interp.setErr(System.err); > > // register the mgr with object name "bsf" > interp.set("bsf", new BSFFunctions(mgr, this)); > > // Declare all declared beans to the interpreter > int size = declaredBeans.size(); > for (int i = 0; i < size; i++) { > declareBean((BSFDeclaredBean) > declaredBeans.elementAt(i)); > } > } > > /** > * Undeclare a previously declared bean. > */ > public void undeclareBean(BSFDeclaredBean bean) throws BSFException { > interp.set(bean.name, null); > } > > public Object unwrap(PyObject result) { > if (result != null) { > Object ret = result.__tojava__(Object.class); > if (ret != Py.NoConversion) > return ret; > } > return result; > } > > public void propertyChange(PropertyChangeEvent e) { > super.propertyChange(e); > > String name = e.getPropertyName(); > Object value = e.getNewValue(); > if (name.equals("classLoader")) { > Py.getSystemState().setClassLoader((ClassLoader) value); > } > } > > private String byteify(String orig) { > // Ugh. Jython likes to be fed bytes, rather than the input > string. > ByteArrayInputStream bais = new > ByteArrayInputStream(orig.getBytes()); > StringBuffer s = new StringBuffer(); > int c; > > while ((c = bais.read()) >= 0) { > s.append((char) c); > } > > return s.toString(); > } > > private class BSFPythonInterpreter extends InteractiveInterpreter { > > public BSFPythonInterpreter() { > super(); > } > > // Override runcode so as not to print the stack dump > public void runcode(PyObject code) { > try { > this.exec(code); > } catch (PyException exc) { > throw exc; > } > } > }
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
