On 10/28/06, Rony G. Flatscher <[EMAIL PROTECTED]> wrote:
Hi Sonny,

just looked at your changes for patching the Jython engine. Before doing
that a question for you: is it intentional that your changes only apply
to the "exec(...)" method and not the others (e.g. "apply(...)")? If so,
what would be the reasoning (unless, of course, I oversee the obvious)?

Hi Rony,
I've never used the other methods and I'm not sure how they are used.
The purpose of my patch is to allow code like the following to work:

from com.foo import Bar
b = Bar()

without calling PySystemState.add_package("com.foo")
the above code would fail with something like package not found. Its
ugly to call  PySystemState.add_package(aPackage) manually to import a
package into jython.

Here is another patch that does the same thing but extract the
functionality in a method to make it clearer what the intent of the
code is.

[Please realize that I have no working knowledge about Jython's inner
workings, so maybe you can shed some light to this?]

I'm not an expert in the inner workings of the jython engine either so
if you think importPackage method belongs in the other methods too, go
ahead and add it. It can't hurt.


Regards,

---rony

------------------- here's the present unified diff of your patch in my
local copy ------------------
--- JythonEngine.java.orig    2006-08-14 14:48:02.547874000 +0200
+++ JythonEngine.java    2006-10-28 13:32:22.128012800 +0200
@@ -16,8 +16,11 @@

 package org.apache.bsf.engines.jython;

+import java.beans.PropertyChangeEvent;
 import java.io.ByteArrayInputStream;
 import java.util.Vector;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;

 import org.apache.bsf.BSFDeclaredBean;
 import org.apache.bsf.BSFException;
@@ -28,6 +31,7 @@
 import org.python.core.PyException;
 import org.python.core.PyJavaInstance;
 import org.python.core.PyObject;
+import org.python.core.PySystemState;
 import org.python.util.InteractiveInterpreter;

 /**
@@ -41,7 +45,8 @@

 public class JythonEngine extends BSFEngineImpl {
   BSFPythonInterpreter interp;
-
+  private final static Pattern fromRegExp = Pattern.compile("from
([.^\\S]*)");
+
   /**
    * call the named method of the given object.
    */
@@ -141,7 +146,13 @@
   public void exec (String source, int lineNo, int columnNo,
             Object script) throws BSFException {
     try {
-      interp.exec (byteify(script.toString ()));
+      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);
@@ -241,4 +252,15 @@
           }
       }
   }
+
+
+  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);
+      }
+
+  }
 }
------------------- here's the present unified diff of your patch in my
local copy (The end.) ------------------







---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


--- JythonEngine.java   2006-10-06 23:53:00.000000000 +0800
+++ 
/home/son/workspace/bsf-2.4.0/src/org/apache/bsf/engines/jython/JythonEngine.java
   2006-10-28 20:26:44.000000000 +0800
@@ -16,8 +16,11 @@
 
 package org.apache.bsf.engines.jython;
 
+import java.beans.PropertyChangeEvent;
 import java.io.ByteArrayInputStream;
 import java.util.Vector;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 import org.apache.bsf.BSFDeclaredBean;
 import org.apache.bsf.BSFException;
@@ -28,6 +31,7 @@
 import org.python.core.PyException;
 import org.python.core.PyJavaInstance;
 import org.python.core.PyObject;
+import org.python.core.PySystemState;
 import org.python.util.InteractiveInterpreter;
 
 /**
@@ -41,6 +45,7 @@
 
 public class JythonEngine extends BSFEngineImpl {
   BSFPythonInterpreter interp;
+  private final static Pattern fromRegExp = Pattern.compile("from ([.^\\S]*)");
   
   /**
    * call the named method of the given object.
@@ -141,13 +146,23 @@
   public void exec (String source, int lineNo, int columnNo,
                    Object script) throws BSFException {
        try {
-         interp.exec (byteify(script.toString ()));
+         String scriptString = script.toString();
+         importPackage(scriptString);
+         interp.exec (byteify(scriptString));
        } catch (PyException e) {
          throw new BSFException (BSFException.REASON_EXECUTION_ERROR,
                              "exception from Jython:\n" + e, e);
        }
   }
 
+  private void importPackage(String script) {  
+       Matcher matcher = fromRegExp.matcher(script);
+       while (matcher.find()) {
+               String packageName = matcher.group(1);
+               PySystemState.add_package(packageName);
+       }       
+  }
+
   /**
    * Execute script code, emulating console interaction.
    */
@@ -241,4 +256,15 @@
           }
       }
   }
+
+
+  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);
+      }
+      
+  }
 }
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to