From: Finn B. <bc...@us...> - 2001-06-22 18:56:01
|
Update of /cvsroot/jython/jython/org/python/core In directory usw-pr-cvs1:/tmp/cvs-serv16726 Modified Files: Py.java PySystemState.java Log Message: Support for the displayhook feature (PEP#217). Index: Py.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/core/Py.java,v retrieving revision 2.44 retrieving revision 2.45 diff -C2 -r2.44 -r2.45 *** Py.java 2001/05/27 18:49:15 2.44 --- Py.java 2001/06/22 18:55:56 2.45 *************** *** 1559,1567 **** public static void printResult(PyObject ret) { ! if (ret != Py.None) { ! Py.getSystemState().builtins.__setitem__("_", Py.None); ! Py.println(ret.__repr__()); ! Py.getSystemState().builtins.__setitem__("_", ret); ! } } --- 1559,1564 ---- public static void printResult(PyObject ret) { ! Py.getThreadState().systemState.invoke("displayhook", ret); ! //Py.getThreadState().systemState.__dict__.__finditem__("displayhook").__call__(ret); } Index: PySystemState.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/core/PySystemState.java,v retrieving revision 2.55 retrieving revision 2.56 diff -C2 -r2.55 -r2.56 *** PySystemState.java 2001/04/13 18:32:55 2.55 --- PySystemState.java 2001/06/22 18:55:56 2.56 *************** *** 176,179 **** --- 176,180 ---- __dict__ = new PyStringMap(); } + System.out.println("__setattr__#1 " + name + " " + value); __dict__.__setitem__(name, value); //throw Py.AttributeError(name); *************** *** 217,220 **** --- 218,223 ---- __dict__ = new PyStringMap(); __dict__.invoke("update", __class__.__getattr__("__dict__")); + __dict__.__setitem__("displayhook", + new PySystemStateFunctions("displayhook", 10, 1, 1)); } } *************** *** 607,610 **** --- 610,630 ---- codecs.setDefaultEncoding(encoding); } + + + // Not public by design. We can't rebind the displayhook if + // a reflected function is inserted in the class dict. + + 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) + return; + + PySystemState sys = Py.getThreadState().systemState; + sys.builtins.__setitem__("_", Py.None); + Py.stdout.println(o.__repr__()); + sys.builtins.__setitem__("_", o); + } } *************** *** 636,639 **** --- 656,678 ---- waitForBytes(); return super.read(b, off, len); + } + } + + + class PySystemStateFunctions extends PyBuiltinFunctionSet + { + PySystemStateFunctions(String name, int index, int minargs, int maxargs) { + super(name, index, minargs, maxargs, false, null); + } + + public PyObject __call__(PyObject arg) { + PySystemState sys = Py.getThreadState().systemState; + switch (index) { + case 10: + sys.displayhook(arg); + return Py.None; + default: + throw argCountError(1); + } } } |