Issue2404

classification
Title: Import as 1st exec statement doesn't work
Type: behaviour Severity: major
Components: Core Versions: Jython 2.7
Milestone:
process
Status: closed Resolution: duplicate
Dependencies: Superseder:
Assigned To: zyasoft Nosy List: oxmane, zyasoft
Priority: Keywords:

Created on 2015-09-24.14:26:14 by oxmane, last changed 2016-01-06.16:00:10 by zyasoft.

Messages
msg10287 (view) Author: Eli Oxman (oxmane) Date: 2015-09-24.14:26:13
The following small test reproduces a behavior which seems like a bug to us:

@Test
public void testImport() throws Exception {
    Options.importSite = false;
    PySystemState pySystemState = new PySystemState();
    PythonInterpreter interpreter = new PythonInterpreter(null, pySystemState);
    interpreter.exec("import os");
    assertTrue(interpreter.getSystemState().modules.__contains__(new PyString("os")));
}

Notice that the following 4 alterations make the test pass (3 of them seem rather unrelated):
1. Removing "Options.importSite = false;"
2. Adding "System.gc()" after the initialization of the PythonInterpreter
3. Adding some dummy statement like "interpreter.exec("pass")" after the initialization of the PythonInterpreter
4. Adding "Py.defaultSystemState = pySystemState;" between the PySystemState initialization and the PythonInterpreter initialization

It seems to me that the issue is somehow related to the cachedThreadState in the ThreadStateMapping.
In general it seems that this cache might get purged (due to GC) between the setSystemState() and the Py.exec(...) in the exec method of PythonInterpreter in which case the defaultSystemState will get used (which doesn't seem like the intended behavior).
msg10548 (view) Author: Jim Baker (zyasoft) Date: 2015-12-22.03:35:07
Most likely the root cause here is #2321
msg10552 (view) Author: Jim Baker (zyasoft) Date: 2015-12-24.17:23:23
Underlying #2321 is now fixed as of https://hg.python.org/jython/rev/dcff83c1c4ac

I did add a modified version of the test proposed by Eli, thanks!
History
Date User Action Args
2016-01-06 16:00:10zyasoftsetstatus: pending -> closed
2015-12-24 17:23:24zyasoftsetstatus: open -> pending
assignee: zyasoft
resolution: duplicate
messages: + msg10552
2015-12-22 03:35:08zyasoftsetmessages: + msg10548
2015-09-24 14:33:37zyasoftsetnosy: + zyasoft
2015-09-24 14:26:14oxmanecreate