Issue1288

classification
Title: NameError in complex loop
Type: crash Severity: major
Components: Core Versions: Jython 2.1
Milestone:
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: fwierzbicki, jsaiz, zyasoft
Priority: Keywords:

Created on 2009-03-25.13:57:30 by jsaiz, last changed 2009-04-02.02:29:57 by zyasoft.

Messages
msg4350 (view) Author: Jaime (jsaiz) Date: 2009-03-25.13:57:27
Running this script:

### BEGIN SCRIPT
rmax = 300
for i in range(10):
	for j in range(1):
		r=0 
		while(r<rmax):
			for k in range(r):
				continue
			r+=1
		print i,j
### END SCRIPT

produces the following output:

### BEGIN OUTPUT
0 0
1 0
2 0
3 0
4 0
5 0
5Traceback (innermost last):
  File "<console>", line 8, in ?
NameError: j
### END OUTPUT

Sometimes it happens when i = 4, other times when i = 5, i = 6 ...

I reproduced it with OS X 10.5.6 on a MacBook Pro
I could not reproduce it with Red Hat Enterprise Linux 4
I know people that have reproduced it with SuSE Linux 10.3

In all cases, JRE version is 1.6.0_07

It seems that the problem does not happen with JRE 1.6.0_11

A solution would be welcome, since we have to live with that Java
version and that Jython version (2.1) for some weeks/months.

Thanks in advance.
msg4351 (view) Author: Jaime (jsaiz) Date: 2009-03-25.15:05:37
Debugging the code, it happens that
PyStringMap.__setitem__(String key, PyObject newValue)
on the "locals" object of PythonInterpreter is called with the arguments

key = "j"
newValue = null

As far as I know, setting "j" to null shouldn't happen; a null object in
Jython is PyNone, which is not a Java null.

Checking whether an object exists in the namespace is done by asking for
its key and seeing that the returned value is not null. Therefore,
setting "j" to null provokes the NameError later on.
msg4352 (view) Author: Jaime (jsaiz) Date: 2009-03-26.14:26:21
The stack trace is:

    PyStringMap.__setitem__(String, PyObject) line: 195   
    PyFrame.setlocal(String, PyObject) line: 216   
    _pyx154.f$0(PyFrame) line: 3   
    _pyx154.call_function(int, PyFrame) line: not available   
    PyTableCode.call(PyFrame, PyObject) line: 208   
    PyTableCode(PyCode).call(PyFrame) line: 14   
    Py.runCode(PyCode, PyObject, PyObject) line: 1135   
    Py.exec(PyObject, PyObject, PyObject) line: 1157   
    PythonInterpreter.exec(String) line: 137

The erroneous call with null value seems to be done from

    _pyx154.f$0(PyFrame) line: 3   

which is compiled code, so I'm not able to see it.
msg4408 (view) Author: Jim Baker (zyasoft) Date: 2009-04-02.02:29:53
This NameError also occurs on java1.6.0_07 configuration with trunk on
OS 10.5

Jython 2.5b3+ (trunk:6136M, Apr 1 2009, 20:07:38) 
[Java HotSpot(TM) 64-Bit Server VM (Apple Inc.)] on java1.6.0_07

But not so when running on java 5 or soylatte on OS X 10.5

I think the likely reason is that the loops are using Java locals (what
we call temporary variables in the Jython compilation) that are
optimized away by a buggy version of the JVM, since fixed after
1.6.0_07. Let's remove the use of temp vars by trying this version,
where we explicitly assign the ranges to a name:

rmax = 300
X = range(10)
for i in X:
    Y = range(1)
    for j in Y:
        r=0 
        while(r<rmax):
            Z = range(r)
            for k in Z:
                continue
            r+=1
        print i,j

And that works on all versions.

Given that, I'm closing this as being a JVM bug until we have evidence
to the contrary.
History
Date User Action Args
2009-04-02 02:29:57zyasoftsetstatus: open -> closed
nosy: + zyasoft, fwierzbicki
resolution: wont fix
messages: + msg4408
2009-03-26 14:26:22jsaizsetmessages: + msg4352
2009-03-25 15:05:38jsaizsetmessages: + msg4351
2009-03-25 13:57:30jsaizcreate