Issue1536

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

Created on 2010-01-07.14:09:12 by tiago182, last changed 2012-03-20.01:52:10 by fwierzbicki.

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?
msg6848 (view) Author: Alan Kennedy (amak) Date: 2012-03-19.19:58:34
Is this still happening? Or has it been resolved?
msg6890 (view) Author: Frank Wierzbicki (fwierzbicki) Date: 2012-03-19.21:42:29
I'll have a look at this.
msg6891 (view) Author: Nicholas Riley (nriley) Date: 2012-03-19.21:46:59
Thanks, you have a better chance of actually having time to work on it :-)
msg6897 (view) Author: Frank Wierzbicki (fwierzbicki) Date: 2012-03-19.22:42:52
Nicholas, Tiago: I dropped this test into 2.5 and it did not error - can I mark this as fixed (though it doesn't look like the parser code changed, maybe jsr223 in JDK 6 no longer has the problem? I'll check in the test code.
msg6900 (view) Author: Frank Wierzbicki (fwierzbicki) Date: 2012-03-19.22:54:50
Scratch that - it looks like I can reproduce it if I run it with our "singlejavatest" target. Weird that it didn't error with the more general batch testing.
msg6902 (view) Author: Alan Kennedy (amak) Date: 2012-03-19.23:03:25
Possibly related issue #1721 ?
msg6903 (view) Author: Frank Wierzbicki (fwierzbicki) Date: 2012-03-19.23:15:35
Alan: huh probably not the same as #1721 - but I'll look at that one too while I'm poking with jsr223 :)
msg6907 (view) Author: Frank Wierzbicki (fwierzbicki) Date: 2012-03-20.01:49:14
Fixed in r6420 - will be fixed in 2.5.3
msg6908 (view) Author: Frank Wierzbicki (fwierzbicki) Date: 2012-03-20.01:52:10
Looks like the rXXXX is still bound to svn - see http://hg.python.org/jython/rev/18e962064e6e for the changes.
History
Date User Action Args
2012-03-20 01:52:10fwierzbickisetmessages: + msg6908
2012-03-20 01:49:14fwierzbickisetstatus: open -> closed
resolution: accepted -> fixed
messages: + msg6907
2012-03-19 23:32:08fwierzbickisetpriority: high
type: crash
resolution: accepted
2012-03-19 23:15:35fwierzbickisetmessages: + msg6903
2012-03-19 23:03:25amaksetmessages: + msg6902
2012-03-19 22:54:50fwierzbickisetmessages: + msg6900
2012-03-19 22:42:52fwierzbickisetmessages: + msg6897
2012-03-19 21:46:59nrileysetmessages: + msg6891
2012-03-19 21:42:29fwierzbickisetassignee: nriley -> fwierzbicki
messages: + msg6890
2012-03-19 19:58:34amaksetnosy: + amak
messages: + msg6848
2010-08-15 17:19:51zyasoftsetnosy: + zyasoft
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