Message12083

Author jeff.allen
Recipients jeff.allen, spaceman_spiff, stefan.richthofer
Date 2018-08-11.17:48:46
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1534009727.59.0.56676864532.issue2700@psf.upfronthosting.co.za>
In-reply-to
Content
I think I can explain this now. I repeated the last sequence with 2.7.0 and the results are different.

PS 271-sa> java -Xmx512m -Xss2560k -cp "C:\Jython\2.7.0-sa\jython-standalone-2.7.0.jar" org.python.util.jython
Jython 2.7.0 (default:9987c746f838, Apr 29 2015, 02:25:11)
[Java HotSpot(TM) 64-Bit Server VM (Oracle Corporation)] on java1.7.0_60
Type "help", "copyright", "credits" or "license" for more information.
>>> from javax.script import ScriptEngineManager as SEM
>>> sem = SEM()
>>> se = sem.getEngineByName('jython')
>>> se.eval('import sys; print sys.path')
['', 'C:\\Jython\\2.7.0-sa\\Lib', 'C:\\Jython\\2.7.0-sa\\jython-standalone-2.7.0.jar\\Lib', '__classpath__', '__pyclasspath__/']
>>>

The first difference to notice here is that I already have '' on the sys.path. Now try:
>>> se.eval("import ModuleB")
TESTPROP: 0
TESTPROP: 1
>>> se.eval("import ModuleB")
>>> se2 = sem.getEngineByName('jython')
>>> se2.eval("import ModuleB")

What's more, even in the main interpreter:
>>> import ModuleB
produces no output. This is because in 2.7.0 all the interpreter instances, while providing you distinct namespaces, share a lot of state including sys.modules. We decided this was incorrect: you are owed a clean interpreter (a clean sys module) with no baggage from other interpreters.

It's still only a theory for you to check, but I'd say openhab creates two interpreters and both of them run your code. Maybe this is something you can prevent. Or maybe it is harmless.

A way to ask "which interpreter am I" is to insert a use of System.identityHashCode in your test_file.py:

==> TestModule/test_file.py <==
import TestModule

from java.lang import System
print 'TestModule = ', System.identityHashCode(TestModule)

print( 'TESTPROP: {}'.format(hasattr(TestModule, 'TESTPROP')))
TestModule.TESTPROP = 'TEST'
print( 'TESTPROP: {}'.format(hasattr(TestModule, 'TESTPROP')))

Then I get (in a 2.7.1 Jython console, after preparing two interpreters):

>>> se.eval("import sys; sys.path[:0]=['']")
>>> se.eval("import ModuleB")
TestModule =  324099565
TESTPROP: 0
TESTPROP: 1
>>> se2.eval("import sys; sys.path[:0]=['']")
>>> se2.eval("import ModuleB")
TestModule =  192838628
TESTPROP: 0
TESTPROP: 1

Maybe you could try the same test_file.py under openhab.
History
Date User Action Args
2018-08-11 17:48:47jeff.allensetmessageid: <1534009727.59.0.56676864532.issue2700@psf.upfronthosting.co.za>
2018-08-11 17:48:47jeff.allensetrecipients: + jeff.allen, stefan.richthofer, spaceman_spiff
2018-08-11 17:48:47jeff.allenlinkissue2700 messages
2018-08-11 17:48:46jeff.allencreate