Issue1536

classification
Title: NPE in org.python.jsr223.PyScriptEngine:187
Type: Severity: major
Components: Core Versions: 2.5.1
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: nriley Nosy List: fwierzbicki, nriley, tiago182
Priority: Keywords:

Created on 2010-01-07.14:09:12 by tiago182, last changed 2010-01-24.01:33:14 by nriley.

Messages
msg5417 (view) Author: Tiago Fernandez (tiago182) Date: 2010-01-07.14:09:11
Here the test:

import org.junit.Test;

import javax.script.Bindings;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.SimpleBindings;
import java.io.FileReader;
import java.io.Reader;

public class TestJSR223EvalWithReader {

  @Test public void should_reuse_reader_in_eval() throws Exception {
    final ScriptEngineManager manager = new ScriptEngineManager();

    final String engineType = "jython";
    final ScriptEngine engine = manager.getEngineByName(engineType);

    final Bindings bindings = new SimpleBindings();
    bindings.put("firstLevelNodes", 10);
    bindings.put("secondLevelNodes", 5);

    engine.setBindings(bindings, ScriptContext.ENGINE_SCOPE);

    final Reader dfsScript = new FileReader(getDepthFirstSearchPath(engineType));

    for (int i = 1; i <= 10; i++)
      engine.eval(dfsScript);
  }

  private String getDepthFirstSearchPath(final String engineType) {
    if ("jython".equalsIgnoreCase(engineType))
      return "src/test/scripts/py/DepthFirstSearch.py";

    else if ("jruby".equalsIgnoreCase(engineType))
      return "src/test/scripts/rb/DepthFirstSearch.rb";

    else if ("rhino".equalsIgnoreCase(engineType))
      return "src/test/scripts/js/DepthFirstSearch.js";

    else if ("groovy".equalsIgnoreCase(engineType))
      return "src/test/scripts/groovy/DepthFirstSearch.groovy";

    throw new IllegalArgumentException("Invalid engine: " + engineType);
  }
}

= = =

Here the script:

class DFS:

  def __init__(self):
    self.visited_node_counter = 0

  def visitor(self):
    self.visited_node_counter += 1

  def visit(self, node):
    node.accept_visitor(self.visitor)
    for child in node.children: self.visit(child)

class Node:

  def __init__(self):
    self.children = []

  def add_child(self, node):
    self.children.append(node)

  def accept_visitor(self, visitor):
    visitor()

root = Node()

for i in xrange(0, firstLevelNodes):
  root.add_child(Node())

for child in root.children:
  for i in xrange(0, secondLevelNodes): child.add_child(Node())

dfs = DFS()
dfs.visit(root)

result = dfs.visited_node_counter

= = =

Just for the records, the same evaluation works well with other scripting engines, e.g. JRuby, Groovy, Rhino.
msg5418 (view) Author: Tiago Fernandez (tiago182) Date: 2010-01-07.14:14:50
javax.script.ScriptException: java.lang.NullPointerException: java.lang.NullPointerException
	at org.python.jsr223.PyScriptEngine.scriptException(PyScriptEngine.java:187)
	at org.python.jsr223.PyScriptEngine.compileScript(PyScriptEngine.java:89)
	at org.python.jsr223.PyScriptEngine.eval(PyScriptEngine.java:48)
	at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:232)
	at TestJSR223EvalWithReader.should_reuse_reader_in_eval(TestJSR223EvalWithReader.java:28)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:94)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:165)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:60)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at com.intellij.rt.execution.application.AppMain.main(AppMain.java:110)
Caused by: java.lang.NullPointerException

	at org.python.core.ParserFacade.parseExpressionOrModule(ParserFacade.java:129)

	at org.python.util.PythonInterpreter.compile(PythonInterpreter.java:221)

	at org.python.util.PythonInterpreter.compile(PythonInterpreter.java:215)

	at org.python.jsr223.PyScriptEngine.compileScript(PyScriptEngine.java:84)

	at org.python.jsr223.PyScriptEngine.eval(PyScriptEngine.java:48)

	at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:232)

	at TestJSR223EvalWithReader.should_reuse_reader_in_eval(TestJSR223EvalWithReader.java:28)

	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)


	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)

	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)

	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)

	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)

	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)

	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)

	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)

	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)

	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)

	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)

	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)

	at org.junit.runners.ParentRunner.run(ParentRunner.java:236)

	at org.junit.runner.JUnitCore.run(JUnitCore.java:157)

	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:94)

	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:165)

	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:60)

	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

	at com.intellij.rt.execution.application.AppMain.main(AppMain.java:110)


java.lang.NullPointerException: java.lang.NullPointerException

	at org.python.core.PyException.fillInStackTrace(PyException.java:70)
	at java.lang.Throwable.<init>(Throwable.java:181)
	at java.lang.Exception.<init>(Exception.java:29)
	at java.lang.RuntimeException.<init>(RuntimeException.java:32)
	at org.python.core.PyException.<init>(PyException.java:46)
	at org.python.core.PyException.<init>(PyException.java:43)
	at org.python.core.Py.JavaError(Py.java:455)
	at org.python.core.ParserFacade.fixParseError(ParserFacade.java:108)
	at org.python.core.ParserFacade.parseExpressionOrModule(ParserFacade.java:132)
	at org.python.util.PythonInterpreter.compile(PythonInterpreter.java:221)
	at org.python.util.PythonInterpreter.compile(PythonInterpreter.java:215)
	at org.python.jsr223.PyScriptEngine.compileScript(PyScriptEngine.java:84)
	... 28 more
Caused by: java.lang.NullPointerException
	at org.python.core.ParserFacade.parseExpressionOrModule(ParserFacade.java:129)
	... 31 more
msg5452 (view) Author: Nicholas Riley (nriley) Date: 2010-01-24.01:33:13
So there are two problems here: first, we're not dealing with an exception that occurs during parsing correctly; and second, we're closing the Reader after we finish compiling.

The first problem is trivially fixable, and I have done so (r6963).

I don't see anything in JSR 223 that mandates the Reader not be closed, but then again, I'm not sure why we're closing it.  The original code came from Frank's parser work in r5005 (http://fisheye3.atlassian.com/changelog/jython/?cs=5005).  Frank - do you remember why you did this?
History
Date User Action Args
2010-01-24 01:33:14nrileysetassignee: nriley
messages: + msg5452
nosy: + nriley
2010-01-08 18:39:56fwierzbickisetnosy: + fwierzbicki
2010-01-07 14:14:51tiago182setmessages: + msg5418
2010-01-07 14:09:12tiago182create