You are viewing a plain text version of this content. The canonical link for it is here.
Posted to bsf-dev@jakarta.apache.org by or...@apache.org on 2003/11/04 08:55:44 UTC

cvs commit: jakarta-bsf/src/org/apache/bsf/engines/jython JythonEngine.java

orlikowski    2003/11/03 23:55:44

  Modified:    src/org/apache/bsf/engines/jython JythonEngine.java
  Log:
  Ran into an interesting problem w/ Jython and double byte character sets.
  Seems that Jython prefers to be fed strings as a set of bytes, if it
  is to handle these character sets correctly.
  
  Note well, we do need the loop involving the ByteArrayInputStream;
  getBytes() won't cut the mustard, in this instance.
  
  Revision  Changes    Path
  1.4       +25 -4     jakarta-bsf/src/org/apache/bsf/engines/jython/JythonEngine.java
  
  Index: JythonEngine.java
  ===================================================================
  RCS file: /home/cvs/jakarta-bsf/src/org/apache/bsf/engines/jython/JythonEngine.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- JythonEngine.java	10 Jul 2003 05:33:37 -0000	1.3
  +++ JythonEngine.java	4 Nov 2003 07:55:44 -0000	1.4
  @@ -55,6 +55,7 @@
   
   package org.apache.bsf.engines.jython;
   
  +import java.io.ByteArrayInputStream;
   import java.util.Vector;
   
   import org.python.util.*;
  @@ -121,7 +122,7 @@
              * evaluate the function. A hack, no question, but it allows
              * apply() to pretend to work on Jython.
              */
  -          StringBuffer script = new StringBuffer(funcBody.toString());
  +          StringBuffer script = new StringBuffer(byteify(funcBody.toString()));
             int index = 0;
             script.insert(0, "def bsf_temp_fn():\n");
            
  @@ -151,7 +152,7 @@
     public Object eval (String source, int lineNo, int columnNo, 
   		      Object script) throws BSFException {
   	try {
  -	  Object result = interp.eval (script.toString ());
  +	  Object result = interp.eval (byteify(script.toString ()));
   	  if (result != null && result instanceof PyJavaInstance)
   		result = ((PyJavaInstance)result).__tojava__(Object.class);
   	  return result;
  @@ -167,7 +168,7 @@
     public void exec (String source, int lineNo, int columnNo,
   		    Object script) throws BSFException {
   	try {
  -	  interp.exec (script.toString ());
  +	  interp.exec (byteify(script.toString ()));
   	} catch (PyException e) {
   	  throw new BSFException (BSFException.REASON_EXECUTION_ERROR,
   			      "exception from Jython: " + e, e);
  @@ -179,10 +180,16 @@
      */
     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(script);
  +          interp.buffer.append(scriptStr);
             if (!(interp.runsource(interp.buffer.toString())))
                 interp.resetbuffer();
         } catch (PyException e) {
  @@ -226,6 +233,20 @@
   		  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 {
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: bsf-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: bsf-dev-help@jakarta.apache.org