Issue2612

classification
Title: NPE while trying to load class
Type: crash Severity: normal
Components: Core Versions: Jython 2.7
Milestone: Jython 2.7.2
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: jeff.allen Nosy List: jamesmudd, jeff.allen
Priority: Keywords:

Created on 2017-07-26.20:54:59 by jamesmudd, last changed 2017-08-27.13:49:50 by jeff.allen.

Messages
msg11502 (view) Author: James Mudd (jamesmudd) Date: 2017-07-26.20:54:57
If you run Jython in a directory containing a subdirectory called test containing a compiled class Test and try to import it you will receive a NPE. It is caused in org.python.core.PyString.encode_UnicodeEscape as null is passed in by the import logic. e.g.

james@james-desktop ~/Desktop/JyTest
 % tree
.
├── jython-standalone-2.7.1.jar
└── test
    ├── Test.class
    └── Test.java

1 directory, 3 files
james@james-desktop ~/Desktop/JyTest

 % java  -jar jython-standalone-2.7.1.jar
Jython 2.7.1 (default:0df7adb1b397, Jun 30 2017, 19:02:43) 
[OpenJDK 64-Bit Server VM (Oracle Corporation)] on java1.8.0_131
Type "help", "copyright", "credits" or "license" for more information.
>>> from test import Test
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
java.lang.NullPointerException
	at org.python.core.PyString.encode_UnicodeEscape(PyString.java:339)
	at org.python.core.imp.loadFromSource(imp.java:627)
	at org.python.core.imp.find_module(imp.java:551)
	at org.python.core.imp.import_next(imp.java:838)
	at org.python.core.imp.import_module_level(imp.java:957)
	at org.python.core.imp.importName(imp.java:1057)
	at org.python.core.ImportFunction.__call__(__builtin__.java:1280)
	at org.python.core.PyObject.__call__(PyObject.java:450)
	at org.python.core.__builtin__.__import__(__builtin__.java:1232)
	at org.python.core.imp.importFromAs(imp.java:1149)
	at org.python.core.imp.importFrom(imp.java:1124)
	at org.python.pycode._pyx1.f$0(<stdin>:1)
	at org.python.pycode._pyx1.call_function(<stdin>)
	at org.python.core.PyTableCode.call(PyTableCode.java:171)
	at org.python.core.PyCode.call(PyCode.java:18)
	at org.python.core.Py.runCode(Py.java:1614)
	at org.python.core.Py.exec(Py.java:1658)
	at org.python.util.PythonInterpreter.exec(PythonInterpreter.java:276)
	at org.python.util.InteractiveInterpreter.runcode(InteractiveInterpreter.java:131)
	at org.python.util.InteractiveInterpreter.runsource(InteractiveInterpreter.java:116)
	at org.python.util.InteractiveInterpreter.runsource(InteractiveInterpreter.java:62)
	at org.python.util.InteractiveConsole.push(InteractiveConsole.java:187)
	at org.python.util.InteractiveConsole._interact(InteractiveConsole.java:168)
	at org.python.util.InteractiveConsole.interact(InteractiveConsole.java:126)
	at org.python.util.jython.run(jython.java:419)
	at org.python.util.jython.main(jython.java:142)
java.lang.NullPointerException: java.lang.NullPointerException
>>> 

The simple fix is to check for null at the start of encode_UnicodeEscape then return null and that make this work but i'm not sure if that's the best fix.
msg11503 (view) Author: James Mudd (jamesmudd) Date: 2017-07-26.20:59:38
Pull request implementing the suggested fix https://github.com/jythontools/jython/pull/86

Not sure if this method is tested anywhere couldn't find anything obvious?
msg11507 (view) Author: Jeff Allen (jeff.allen) Date: 2017-07-28.06:47:07
I think it probably should be an error to call encode_UnicodeEscape with a null string.

And maybe the inner-workings of repr should not have been public.

My mistake was not guarding against null here: https://hg.python.org/jython/rev/cb01e444e8e2#l16.7 when I chose to use the displayDirName. I suppose this is a  combination that ought to be tested in test_import_jy. It only generates a warning, though.
msg11549 (view) Author: Jeff Allen (jeff.allen) Date: 2017-08-27.11:32:46
Disconcertingly, all you need to do (on my system anyway) is:
>>> import ast

What it wanted to say before it died was: "Not importing directory 'C:\\Users\\Jeff\\Documents\\Eclipse\\jython-trunk\\ast': missing __init__.py", which is of course correct, since *that* ast directory on sys.path is not the one that "import ast" is looking for.

It kills test_ast too, although it passes during regression testing, perhaps because of the way regrtest discovers tests. I'll fix it in imp.java where I caused it.
msg11550 (view) Author: Jeff Allen (jeff.allen) Date: 2017-08-27.13:49:50
Declare success https://hg.python.org/jython/rev/39b0bd0dcd01
History
Date User Action Args
2017-08-27 13:49:50jeff.allensetstatus: open -> closed
resolution: accepted -> fixed
messages: + msg11550
2017-08-27 11:32:46jeff.allensettitle: 2.7.1 NPE while trying to load class -> NPE while trying to load class
resolution: accepted
messages: + msg11549
assignee: jeff.allen
milestone: Jython 2.7.2
type: behaviour -> crash
2017-07-28 06:47:07jeff.allensetnosy: + jeff.allen
messages: + msg11507
2017-07-26 20:59:38jamesmuddsetmessages: + msg11503
2017-07-26 20:54:59jamesmuddcreate