Title: REGRESSION: TypeError: displayhook(): expected 1 args; got 0 (2.5.2rc1)
Type: crash Severity: major
Components: Core Versions: 2.5.2b1
Status: closed Resolution: works for me
Dependencies: Superseder:
Assigned To: zyasoft Nosy List: spider, zyasoft
Priority: high Keywords:

Created on 2010-10-04.08:32:46 by spider, last changed 2010-10-15.14:01:15 by zyasoft.

msg6132 (view) Author: Matthew Webber (spider) Date: 2010-10-04.08:32:43
Our code uses a displayhook (see It works in Jython 2.5.1, but fails in 2.5.1rc1 and 2.5.2rc2 with the following traceback:

2010-10-01 10:36:26,318 WARN  [gda.jython.GDAInteractiveConsole] - InteractiveConsole exception: Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: displayhook(): expected 1 args; got 0

	at org.python.core.PyException.fillInStackTrace( [jython.jar:na]
	at java.lang.Throwable.<init>( [na:1.6.0_16]
	at java.lang.Exception.<init>( [na:1.6.0_16]
	at java.lang.RuntimeException.<init>( [na:1.6.0_16]
	at org.python.core.PyException.<init>( [jython.jar:na]
	at org.python.core.PyException.<init>( [jython.jar:na]
	at org.python.core.PyException.<init>( [jython.jar:na]
	at org.python.core.Py.TypeError( [jython.jar:na]
	at org.python.core.PyReflectedFunction.throwError( [jython.jar:na]
	at org.python.core.PyReflectedFunction.throwArgCountError( [jython.jar:na]
	at org.python.core.PyReflectedFunction.throwError( [jython.jar:na]
	at org.python.core.PyReflectedFunction.__call__( [jython.jar:na]
	at org.python.core.PyReflectedFunction.__call__( [jython.jar:na]
	at org.python.core.PyObject.__call__( [jython.jar:na]
	at org.python.core.PyObject.invoke( [jython.jar:na]
	at org.python.core.Py.printResult( [jython.jar:na]
	at org.python.pycode._pyx69.f$0(<input>:1) [na:na]
	at org.python.pycode._pyx69.call_function(<input>) [na:na]
	at [jython.jar:na]
	at [jython.jar:na]
	at org.python.core.Py.runCode( [jython.jar:na]
	at org.python.core.Py.exec( [jython.jar:na]
	at org.python.util.PythonInterpreter.exec( [jython.jar:na]
	at org.python.util.InteractiveInterpreter.runcode( [jython.jar:na]
	at org.python.util.InteractiveInterpreter.runsource( [jython.jar:na]
	at org.python.util.InteractiveInterpreter.runsource( [jython.jar:na]
	at gda.jython.GDAJythonInterpreter.populateNamespace( [main/:na]
	at gda.jython.GDAJythonInterpreter.initialise( [main/:na]
	at gda.jython.JythonServer.configure( [main/:na]
	at gda.factory.FactoryBase.configure( [main/:na]
	at gda.util.ObjectCreatorsObjectServer.startServer( [main/:na]
	at gda.util.ObjectServer.configure( [main/:na]
	at gda.util.ObjectServer.createServerImpl( [main/:na]
	at gda.util.ObjectServer.main( [main/:na]
2010-10-01 10:36:26,424 WARN  [gda.jython.GDAInteractiveConsole] - InteractiveConsole exception: Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: displayhook(): expected 1 args; got 0

displayhook itself is fairly simple:
    public static void displayhook(PyObject o) {
        /* Print value except if None */
        /* After printing, also assign to '_' */
        /* Before, set '_' to None to avoid recursion */
        if (o == Py.None)

        PyObject currentBuiltins = Py.getSystemState().getBuiltins();
        currentBuiltins.__setitem__("_", Py.None);
        if (o instanceof PyUnicode) {
        	PyUnicode u = (PyUnicode)o;
		} else {
        currentBuiltins.__setitem__("_", o);

Unfortunately this is part of a fairly large code base, and at this point I haven't pulled out a simple test case for you.

A quick glance at the change log doesn't throw up any obvious candidates.
msg6133 (view) Author: Matthew Webber (spider) Date: 2010-10-04.08:48:16
Correction to above:
OLD: fails in 2.5.1rc1 and 2.5.2rc2
NEW: fails in 2.5.2rc1 and 2.5.2rc2
msg6134 (view) Author: Matthew Webber (spider) Date: 2010-10-04.10:51:50
OK, I think we've found where the problem was introduced:

Revision 6837
hack around PyReflectedFunction's assumption of how instancemethod calls into
it so instancemethod's ThreadState pass through optimization can be enabled

The problem is in, in this method: 
public PyObject __call__(PyObject[] args, String[] keywords) {
(it's line 194 in revision 7066).

I don't have enough understanding of how all this fits together to suggest a fix (the comment in line 48
    // NOTE: this calledStatically business is pretty hacky
suggests only the brave should touch this code).

However, I hope that's enough to get you started.
msg6168 (view) Author: Jim Baker (zyasoft) Date: 2010-10-15.14:01:13
I can't reproduce this problem on Jython trunk. Here's what I tried:

>>> import dh    
>>> import sys
>>> h = dh()
>>> h.hook("hi")
>>> sys.displayhook = h.hook
>>> "foo"                   
>>> sys.displayhook = dh.static_hook
>>> "foo"                           

This is the dh class:

import org.python.core.PyObject;

public class dh {

    public dh() {}

    public static void static_hook(PyObject o) {
        System.out.println("Hooked<<<" + o + ">>>");

    public void hook(PyObject o) {
        System.out.println("Hooked<<<" + o + ">>>");

So that all looks good to me.

Please submit a test case (in either JUnit or PyUnit, see our test cases for examples) and reopen if you're still experiencing a problem. I did take a look at the possibility of downloading  and running GDA, but that's just too big of a project.
Date User Action Args
2010-10-15 14:01:15zyasoftsetstatus: open -> closed
resolution: works for me
messages: + msg6168
2010-10-06 04:22:31zyasoftsetpriority: high
assignee: zyasoft
nosy: + zyasoft
2010-10-04 10:51:51spidersetmessages: + msg6134
2010-10-04 08:48:16spidersetmessages: + msg6133
2010-10-04 08:32:46spidercreate