You are viewing a plain text version of this content. The canonical link for it is here.
Posted to bsf-user@jakarta.apache.org by Neil Benn <ne...@ziath.com> on 2006/06/06 21:48:22 UTC

Usage of Eval

Hello,
 
            I am working with BSF and Jython (I'll move onto other
languages later - it's just the Jython/Python is the scriptng language I
am most used to).  I'm trying to implement a simple script to run a
method and get the result such as:
 
---
def test():
            return 1
test()
 
---
 
I was hoping that when I eval that using:
 
---
 
public void testRawJythonScript(){
            String strScript = "def test():\n";
            strScript += "\treturn 1\n";
            strScript += "test()\n";
            BSFManager objBM = new BSFManager();
            try{
                        Object objResult = objBM.eval("Jython", null, 0,
0, strScript);
                        System.out.println(objResult);
            }catch (BSFException eBSF){
                        eBSF.printStackTrace();
                        fail(eBSF.getMessage());
            }
}
 
---
 
  I would get the number 1 back (well in a PyObject but still 1).
However I get an exception:
 
---
 
junit.framework.AssertionFailedError: exception from Jython: Traceback
(innermost last):
  (no code object) at line 0
  File "<string>", line 1
            def test():
            ^
SyntaxError: invalid syntax
 
            at junit.framework.Assert.fail(Assert.java:47)
            at
uk.co.ziath.datapaq.server.scripting.test.BSFScriptProcessorTest.testRaw
JythonScript(BSFScriptProcessorTest.java:67)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native
Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown
Source)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown
Source)
            at java.lang.reflect.Method.invoke(Unknown Source)
            at junit.framework.TestCase.runTest(TestCase.java:154)
            at junit.framework.TestCase.runBare(TestCase.java:127)
            at junit.framework.TestResult$1.protect(TestResult.java:106)
            at
junit.framework.TestResult.runProtected(TestResult.java:124)
            at junit.framework.TestResult.run(TestResult.java:109)
            at junit.framework.TestCase.run(TestCase.java:118)
            at junit.framework.TestSuite.runTest(TestSuite.java:208)
            at junit.framework.TestSuite.run(TestSuite.java:203)
            at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTe
stRunner.java:478)
            at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRun
ner.java:344)
            at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRu
nner.java:196)
 
---
 
            In addition my System.out repeats the top message a few
times, I've tried playing around with a few things but I can't
understand why BSF/Jython thinks this is bad code.  I would be eternally
grateful if someone could point me in the right direction! If this is
better asked on the Jython Users board then please let me know - ta.
 
            Thanks in advance.        
 
Cheers,
 
Neil

Re: Usage of Eval

Posted by "Rony G. Flatscher" <Ro...@wu-wien.ac.at>.
Hi Mario,

> If you want to try...
> http://www.xmoon.org
>
> <task>
>   <code language="javascript | ruby | judoscript | beanshell | *jython*">
>        Script code here
>    </code>
> </task>

would eval() work? Ie. invoking a jython script and have it return a 
(function) value with which you could go on working? (Executing a jython 
script works.)

BTW, BSF was created *exactly* for the purpose you mention on your page, 
so why not join forces?

In the meantime BSF supports at least the following languages:

javascript = org.apache.bsf.engines.javascript.JavaScriptEngine, js
jacl = org.apache.bsf.engines.jacl.JaclEngine, jacl
netrexx = org.apache.bsf.engines.netrexx.NetRexxEngine, nrx
java = org.apache.bsf.engines.java.JavaEngine, java
javaclass = org.apache.bsf.engines.javaclass.JavaClassEngine, class
bml = org.apache.bml.ext.BMLEngine, bml
vbscript = org.apache.bsf.engines.activescript.ActiveScriptEngine, vbs
jscript = org.apache.bsf.engines.activescript.ActiveScriptEngine, jss
perlscript = org.apache.bsf.engines.activescript.ActiveScriptEngine, pls
perl = org.apache.bsf.engines.perl.PerlEngine, pl
jpython = org.apache.bsf.engines.jpython.JPythonEngine, py
jython = org.apache.bsf.engines.jython.JythonEngine, py
lotusscript = org.apache.bsf.engines.lotusscript.LsEngine, lss
xslt = org.apache.bsf.engines.xslt.XSLTEngine, xslt
pnuts = pnuts.ext.PnutsBSFEngine, pnut
beanbasic = org.apache.bsf.engines.beanbasic.BeanBasicEngine, bb
beanshell = bsh.util.BeanShellBSFEngine, bsh
ruby = org.jruby.javasupport.bsf.JRubyEngine, rb
judoscript = com.judoscript.BSFJudoEngine, judo|jud
groovy = org.codehaus.groovy.bsf.GroovyEngine, groovy|gy
objectscript = oscript.bsf.ObjectScriptEngine, os
prolog = ubc.cs.JLog.Extras.BSF.JLogBSFEngine, plog|prolog
rexx = org.rexxla.bsf.engines.rexx.RexxEngine, rex | rexx | cls | rxj | rxs

In addition work has been done for a JSR-223 version of BSF, which will 
go in beta once this version of BSF goes Golden.

Regards,

---rony




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


Re: Usage of Eval

Posted by Mario Neè <ma...@xmoon.org>.
If you want to try...
http://www.xmoon.org

<task>
   <code language="javascript | ruby | judoscript | beanshell | *jython*">
        Script code here
    </code>
</task>


Neil Benn ha scritto:
> Hello,
>  
>             I am working with BSF and Jython (I'll move onto other
> languages later - it's just the Jython/Python is the scriptng language I
> am most used to).  I'm trying to implement a simple script to run a
> method and get the result such as:
>  
> ---
> def test():
>             return 1
> test()
>  
> ---
>  
> I was hoping that when I eval that using:
>  
> ---
>  
> public void testRawJythonScript(){
>             String strScript = "def test():\n";
>             strScript += "\treturn 1\n";
>             strScript += "test()\n";
>             BSFManager objBM = new BSFManager();
>             try{
>                         Object objResult = objBM.eval("Jython", null, 0,
> 0, strScript);
>                         System.out.println(objResult);
>             }catch (BSFException eBSF){
>                         eBSF.printStackTrace();
>                         fail(eBSF.getMessage());
>             }
> }
>  
> ---
>  
>   I would get the number 1 back (well in a PyObject but still 1).
> However I get an exception:
>  
> ---
>  
> junit.framework.AssertionFailedError: exception from Jython: Traceback
> (innermost last):
>   (no code object) at line 0
>   File "<string>", line 1
>             def test():
>             ^
> SyntaxError: invalid syntax
>  
>             at junit.framework.Assert.fail(Assert.java:47)
>             at
> uk.co.ziath.datapaq.server.scripting.test.BSFScriptProcessorTest.testRaw
> JythonScript(BSFScriptProcessorTest.java:67)
>             at sun.reflect.NativeMethodAccessorImpl.invoke0(Native
> Method)
>             at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown
> Source)
>             at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown
> Source)
>             at java.lang.reflect.Method.invoke(Unknown Source)
>             at junit.framework.TestCase.runTest(TestCase.java:154)
>             at junit.framework.TestCase.runBare(TestCase.java:127)
>             at junit.framework.TestResult$1.protect(TestResult.java:106)
>             at
> junit.framework.TestResult.runProtected(TestResult.java:124)
>             at junit.framework.TestResult.run(TestResult.java:109)
>             at junit.framework.TestCase.run(TestCase.java:118)
>             at junit.framework.TestSuite.runTest(TestSuite.java:208)
>             at junit.framework.TestSuite.run(TestSuite.java:203)
>             at
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTe
> stRunner.java:478)
>             at
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRun
> ner.java:344)
>             at
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRu
> nner.java:196)
>  
> ---
>  
>             In addition my System.out repeats the top message a few
> times, I've tried playing around with a few things but I can't
> understand why BSF/Jython thinks this is bad code.  I would be eternally
> grateful if someone could point me in the right direction! If this is
> better asked on the Jython Users board then please let me know - ta.
>  
>             Thanks in advance.        
>  
> Cheers,
>  
> Neil
>
>   


-- 
Mario Nee'
Web Developer

------------------------------
Neex
Via Vanzeghetto, 47
25031 Capriolo (BS) Italia
www.neex.it
------------------------------

CONFIDENZIALE: Questo messaggio e gli eventuali allegati sono confidenziali e riservati. Se vi e' stato recapitato per errore e non siete fra i destinatari elencati, siete pregati di darne immediatamente avviso al mittente. Le informazioni contenute non devono essere mostrate ad altri, ne utilizzate, memorizzate o copiate in qualsiasi forma.
CONFIDENTIALITY : This  e-mail  and  any attachments are confidential and may be privileged. If  you are not a named recipient, please notify the sender immediately and do not disclose the contents to another person, use it for any purpose or store or copy the information in any medium.


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


RE: Usage of Eval

Posted by Neil Benn <ne...@ziath.com>.

-----Original Message-----
From: Rony G. Flatscher [mailto:Rony.Flatscher@wu-wien.ac.at] 
Sent: 06 June 2006 23:09
To: Bean Scripting Framework users
Subject: Re: Usage of Eval

Hi Neil,

just noticed that using exec() and compile() works, so it is the eval() 
(and probably apply()) which poses the problem.

<snip>
Hello,

	Sorry for my extended silence - the same error occurs with raw
Jython so it seems that it is a Jython problem rather than a BSF
problem.

Cheers,

Neil


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


Re: Usage of Eval

Posted by "Rony G. Flatscher" <Ro...@wu-wien.ac.at>.
Hi Neil,

just noticed that using exec() and compile() works, so it is the eval() 
(and probably apply()) which poses the problem.

The following works:

    java org.apache.bsf.Main -in test.py -lang jython -mode exec
    java org.apache.bsf.Main -in test.py -lang jython -mode compile

Not sure about eval(). Could it be that it expects a compiled jython 
program instead (ie. doing a compile and using its result for eval)?

Regards,

---rony



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


Re: Usage of Eval

Posted by "Rony G. Flatscher" <Ro...@wu-wien.ac.at>.
Hi Neil,

as you know there are two Python engines available, "jython" and 
"jpython" (not that it makes a difference for your problem, but it might 
be interesting to know).

Your test program runs nicely standalone, when invoked via the command 
line using BSF it bombs as you reported. It seems that the error comes 
from jython.

Here's the content of a file named "test.py" containing almost what you 
wrote:

------------ cut here ----------
def test ():
    return 1

print test()
------------ cut here ----------

It runs flawlessly with Python.

Here are the results of running it via BSF under different versions of Java from the command line (using org.apache.bsf.Main):

using the "jython" engine with Java 3:
------------ cut here ----------
E:\rony\dev\bsf\src\samples\bsf_samples\bsh>java org.apache.bsf.Main -in test.py -lang jython
[ERROR] BSFManager - Exception:  <java.security.PrivilegedActionException <<org.apache.bsf.BSFException: exception from Jython:
Traceback (innermost last):
  (no code object) at line 0
SyntaxError: ('invalid syntax', ('<string>', 1, 1, 'def test ():'))
>>>java.security.PrivilegedActionException: org.apache.bsf.BSFException: exception from Jython:
Traceback (innermost last):
  (no code object) at line 0
SyntaxError: ('invalid syntax', ('<string>', 1, 1, 'def test ():'))

        at org.apache.bsf.engines.jython.JythonEngine.eval(JythonEngine.java:168)
        at org.apache.bsf.BSFManager$5.run(BSFManager.java:470)
        at java.security.AccessController.doPrivileged(Native Method)
        at org.apache.bsf.BSFManager.eval(BSFManager.java:467)
        at org.apache.bsf.Main.main(Main.java:154)

exception from Jython:
Traceback (innermost last):
  (no code object) at line 0
SyntaxError: ('invalid syntax', ('<string>', 1, 1, 'def test ():'))
: Traceback (innermost last):
  (no code object) at line 0
  File "<string>", line 1
        def test ():
        ^
SyntaxError: invalid syntax
------------ cut here ----------


using the "jpython" engine with Java 3:

------------ cut here ----------
E:\rony\dev\bsf\src\samples\bsf_samples\bsh>java org.apache.bsf.Main -in test.py -lang jpython
Traceback (innermost last):
  (no code object) at line 0
  File "<string>", line 1
        def test ():
        ^
SyntaxError: invalid syntax
[ERROR] BSFManager - Exception:  <java.security.PrivilegedActionException <<org.apache.bsf.BSFException: exception from JPython: Traceback (innermost last):
  (no code object) at line 0
  File "<string>", line 1
        def test ():
        ^
SyntaxError: invalid syntax
>>>java.security.PrivilegedActionException: org.apache.bsf.BSFException: exception from JPython: Traceback (innermost last):
  (no code object) at line 0
  File "<string>", line 1
        def test ():
        ^
SyntaxError: invalid syntax

        at org.apache.bsf.engines.jpython.JPythonEngine.eval(JPythonEngine.java:120)
        at org.apache.bsf.BSFManager$5.run(BSFManager.java:470)
        at java.security.AccessController.doPrivileged(Native Method)
        at org.apache.bsf.BSFManager.eval(BSFManager.java:467)
        at org.apache.bsf.Main.main(Main.java:154)

exception from JPython: Traceback (innermost last):
  (no code object) at line 0
  File "<string>", line 1
        def test ():
        ^
SyntaxError: invalid syntax
: Traceback (innermost last):
  (no code object) at line 0
  File "<string>", line 1
        def test ():
        ^
SyntaxError: invalid syntax
------------ cut here ----------



using the "jython" engine with Java 4:

------------ cut here ----------
E:\temp\bsf>java org.apache.bsf.Main -in test.py -lang jython
exception from Jython:
Traceback (innermost last):
  (no code object) at line 0
SyntaxError: ('invalid syntax', ('<string>', 1, 1, 'def test():'))
: Traceback (innermost last):
  (no code object) at line 0
  File "<string>", line 1
        def test():
        ^
SyntaxError: invalid syntax
------------ cut here ----------


using the "jpython" engine with Java 4:

------------ cut here ----------
E:\temp\bsf>java org.apache.bsf.Main -in test.py -lang jpython
Traceback (innermost last):
  (no code object) at line 0
  File "<string>", line 1
        def test():
        ^
SyntaxError: invalid syntax
exception from JPython: Traceback (innermost last):
  (no code object) at line 0
  File "<string>", line 1
        def test():
        ^
SyntaxError: invalid syntax
: Traceback (innermost last):
  (no code object) at line 0
  File "<string>", line 1
        def test():
        ^
SyntaxError: invalid syntax
------------ cut here ----------


using the "jython" engine with Java 5:

------------ cut here ----------
E:\rony\dev\bsf\src\samples\bsf_samples\bsh>java org.apache.bsf.Main -in test.py -lang jython
Jun 6, 2006 10:27:32 PM org.apache.bsf.BSFManager eval
SEVERE: Exception:
java.security.PrivilegedActionException: org.apache.bsf.BSFException: exception from Jython:
Traceback (innermost last):
  (no code object) at line 0
SyntaxError: ('invalid syntax', ('<string>', 1, 1, 'def test ():'))

        at java.security.AccessController.doPrivileged(Native Method)
        at org.apache.bsf.BSFManager.eval(BSFManager.java:467)
        at org.apache.bsf.Main.main(Main.java:154)
Caused by: org.apache.bsf.BSFException: exception from Jython:
Traceback (innermost last):
  (no code object) at line 0
SyntaxError: ('invalid syntax', ('<string>', 1, 1, 'def test ():'))

        at org.apache.bsf.engines.jython.JythonEngine.eval(JythonEngine.java:168)
        at org.apache.bsf.BSFManager$5.run(BSFManager.java:470)
        ... 3 more
exception from Jython:
Traceback (innermost last):
  (no code object) at line 0
SyntaxError: ('invalid syntax', ('<string>', 1, 1, 'def test ():'))
: Traceback (innermost last):
  (no code object) at line 0
  File "<string>", line 1
        def test ():
        ^
SyntaxError: invalid syntax
------------ cut here ----------


using the "jpython" engine with Java 5:
------------ cut here ----------
E:\rony\dev\bsf\src\samples\bsf_samples\bsh>java org.apache.bsf.Main -in test.py -lang jpython
Traceback (innermost last):
  (no code object) at line 0
  File "<string>", line 1
        def test ():
        ^
SyntaxError: invalid syntax
Jun 6, 2006 10:29:23 PM org.apache.bsf.BSFManager eval
SEVERE: Exception:
java.security.PrivilegedActionException: org.apache.bsf.BSFException: exception from JPython: Traceback (innermost last):
  (no code object) at line 0
  File "<string>", line 1
        def test ():
        ^
SyntaxError: invalid syntax

        at java.security.AccessController.doPrivileged(Native Method)
        at org.apache.bsf.BSFManager.eval(BSFManager.java:467)
        at org.apache.bsf.Main.main(Main.java:154)
Caused by: org.apache.bsf.BSFException: exception from JPython: Traceback (innermost last):
  (no code object) at line 0
  File "<string>", line 1
        def test ():
        ^
SyntaxError: invalid syntax

        at org.apache.bsf.engines.jpython.JPythonEngine.eval(JPythonEngine.java:120)
        at org.apache.bsf.BSFManager$5.run(BSFManager.java:470)
        ... 3 more
exception from JPython: Traceback (innermost last):
  (no code object) at line 0
  File "<string>", line 1
        def test ():
        ^
SyntaxError: invalid syntax
: Traceback (innermost last):
  (no code object) at line 0
  File "<string>", line 1
        def test ():
        ^
SyntaxError: invalid syntax
------------ cut here ----------

Not sure where the error comes from...

---rony

P.S.: Also tried it with Jython 2.2alpha on Java 4 with the same results...




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


Re: Usage of Eval

Posted by "Rony G. Flatscher" <Ro...@wu-wien.ac.at>.
Hi Neil,

maybe you want to look at the source code of the jython and jpython BSF 
engines, located at "org.apache.bsf.engines.jython.JythonEngine.java" or 
"org.apache.bsf.engines.jpython.JPythonEngine.java". With a little luck 
something hits your eye?

Unless the original jython BSF engine implementors look at this list, 
you might want to address the www.jython.org developers?

Regards,

---rony






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


RE: Usage of Eval

Posted by Neil Benn <ne...@ziath.com>.
Hello,

	Sorry the capital J in Jython is a typo from my email client!
<snip>
Object objResult = objBM.eval("Jython", null, 0, 0, strScript);
<snip>

  It should be (and is) - 'jython'. 
Cheers,

Neil


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