@@ -67,6 +67,50 @@ PySys_SetObject(char *name, PyObject *v)
6767 return PyDict_SetItemString (sd , name , v );
6868}
6969
70+ static PyObject *
71+ sys_displayhook (PyObject * self , PyObject * args )
72+ {
73+ PyObject * o , * stdout ;
74+ PyInterpreterState * interp = PyThreadState_Get ()-> interp ;
75+ PyObject * modules = interp -> modules ;
76+ PyObject * builtins = PyDict_GetItemString (modules , "__builtin__" );
77+
78+ /* parse arguments */
79+ if (!PyArg_ParseTuple (args , "O:displayhook" , & o ))
80+ return NULL ;
81+
82+ /* Print value except if None */
83+ /* After printing, also assign to '_' */
84+ /* Before, set '_' to None to avoid recursion */
85+ if (o == Py_None ) {
86+ Py_INCREF (Py_None );
87+ return Py_None ;
88+ }
89+ if (PyObject_SetAttrString (builtins , "_" , Py_None ) != 0 )
90+ return NULL ;
91+ if (Py_FlushLine () != 0 )
92+ return NULL ;
93+ stdout = PySys_GetObject ("stdout" );
94+ if (stdout == NULL ) {
95+ PyErr_SetString (PyExc_RuntimeError , "lost sys.stdout" );
96+ return NULL ;
97+ }
98+ if (PyFile_WriteObject (o , stdout , 0 ) != 0 )
99+ return NULL ;
100+ PyFile_SoftSpace (stdout , 1 );
101+ if (Py_FlushLine () != 0 )
102+ return NULL ;
103+ if (PyObject_SetAttrString (builtins , "_" , o ) != 0 )
104+ return NULL ;
105+ Py_INCREF (Py_None );
106+ return Py_None ;
107+ }
108+
109+ static char displayhook_doc [] =
110+ "displayhook(o) -> None\n"
111+ "\n"
112+ "Print o to the stdout, and save it in __builtin__._\n" ;
113+
70114static PyObject *
71115sys_exc_info (PyObject * self , PyObject * args )
72116{
@@ -332,6 +376,7 @@ extern PyObject *_Py_GetDXProfile(PyObject *, PyObject *);
332376
333377static PyMethodDef sys_methods [] = {
334378 /* Might as well keep this in alphabetic order */
379+ {"displayhook" , sys_displayhook , 1 , displayhook_doc },
335380 {"exc_info" , sys_exc_info , 1 , exc_info_doc },
336381 {"exit" , sys_exit , 0 , exit_doc },
337382 {"getdefaultencoding" , sys_getdefaultencoding , 1 ,
@@ -475,6 +520,7 @@ __stderr__ -- the original stderr; don't use!\n\
475520\n\
476521Functions:\n\
477522\n\
523+ displayhook() -- print an object to the screen, and save it in __builtin__._\n\
478524exc_info() -- return thread-safe information about the current exception\n\
479525exit() -- exit the interpreter by raising SystemExit\n\
480526getrefcount() -- return the reference count for an object (plus one :-)\n\
0 commit comments