Issue1407

classification
Title: ClassCastException in plain Python coroutine
Type: behaviour Severity: normal
Components: Core Versions: 2.5.0
Milestone:
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: thobes Nosy List: fwierzbicki, scoder, thobes
Priority: urgent Keywords:

Created on 2009-07-20.11:40:55 by scoder, last changed 2009-07-21.21:11:04 by thobes.

Messages
msg4929 (view) Author: Stefan Behnel (scoder) Date: 2009-07-20.11:40:55
The following code leads to a ClassCastException:

----------------------------
def add(target):
    while True:
        target.send( (yield) + 1 )

def sink():
    while True:
        print( (yield) )

p = add(sink()) ; p.next()

p.send(5)   #  <== fails!
p.close()
----------------------------

When running it, I get:

Traceback (most recent call last):
  File "pype/jytest.py", line 15, in <module>
    p.send(5)
  File "pype/jytest.py", line 4, in add
    target.send( (yield) + 1 )
java.lang.ClassCastException: org.python.core.PyGenerator$send_exposer
cannot be cast to org.python.core.ThreadState
        at org.python.pycode._pyx0.add$1(pype/jytest.py:3)
        at org.python.pycode._pyx0.call_function(pype/jytest.py)
        at org.python.core.PyTableCode.call(PyTableCode.java:165)
        at org.python.core.PyGenerator.__iternext__(PyGenerator.java:129)
        at org.python.core.PyGenerator.__iternext__(PyGenerator.java:112)
        at org.python.core.PyIterator.next(PyIterator.java:42)
        at org.python.core.PyGenerator.next(PyGenerator.java:68)
        at org.python.core.PyGenerator.send(PyGenerator.java:39)
        at org.python.core.PyGenerator$send_exposer.__call__(Unknown Source)
        at org.python.core.PyObject.__call__(PyObject.java:401)
        at org.python.pycode._pyx0.f$0(pype/jytest.py:16)
        at org.python.pycode._pyx0.call_function(pype/jytest.py)
        at org.python.core.PyTableCode.call(PyTableCode.java:165)
        at org.python.core.PyCode.call(PyCode.java:18)
        at org.python.core.Py.runCode(Py.java:1198)
        at
org.python.util.PythonInterpreter.execfile(PythonInterpreter.java:166)
        at org.python.util.jython.run(jython.java:229)
        at org.python.util.jython.main(jython.java:117)

java.lang.ClassCastException: java.lang.ClassCastException:
org.python.core.PyGenerator$send_exposer cannot be cast to
org.python.core.ThreadState
msg4930 (view) Author: Tobias Ivarsson (thobes) Date: 2009-07-20.14:15:12
It looks like the stack gets scrambled when yield is in the argument
list, shouldn't there be cases for this in the standard test suite?

It could (for some strange reason) be specific to send, I'll investigate
and fix this ASAP.
msg4934 (view) Author: Tobias Ivarsson (thobes) Date: 2009-07-21.21:11:03
Fixed in revision 6555:
http://jython.svn.sourceforge.net/viewvc/jython?view=rev&revision=6555

This error was much worse than expected. The stack was not restored
properly after yield, any invocation where yield was in the argument
list was faulty. I am amazed that there were no test cases in the
standard test suite for catching this. Here is a simpler failing example:

  def one():
    two((yield))
  def two(x):
    print x
  o = one(); o.next()
  o.send(5)

This is all fixed now though.
History
Date User Action Args
2009-07-21 21:11:04thobessetstatus: open -> closed
resolution: fixed
messages: + msg4934
2009-07-20 14:51:53fwierzbickisetnosy: + fwierzbicki
2009-07-20 14:15:26thobessetpriority: urgent
2009-07-20 14:15:12thobessetassignee: thobes
messages: + msg4930
nosy: + thobes
2009-07-20 11:40:55scodercreate