Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f68f2fe

Browse files
author
Moshe Zadka
committed
Implementation of PEP-0217.
This closes the PEP, and patch 103170
1 parent 5ac9795 commit f68f2fe

3 files changed

Lines changed: 72 additions & 27 deletions

File tree

Doc/lib/libsys.tex

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,15 @@ \section{\module{sys} ---
4444
Availability: Windows.
4545
\end{datadesc}
4646

47+
\begin{funcdesc}{displayhook}{\var{value}}
48+
If \var{value} is not \code{None}, this function prints it to
49+
\code{sys.stdout}, and saves it in \code{__builtin__._}.
50+
51+
This function is called when an expression is entered at the prompt
52+
of an interactive Python session. It exists mainly so it can be
53+
overridden.
54+
\end{funcdesc}
55+
4756
\begin{funcdesc}{exc_info}{}
4857
This function returns a tuple of three values that give information
4958
about the exception that is currently being handled. The information

Python/ceval.c

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1245,36 +1245,26 @@ eval_code2(PyCodeObject *co, PyObject *globals, PyObject *locals,
12451245

12461246
case PRINT_EXPR:
12471247
v = POP();
1248-
/* Print value except if None */
1249-
/* After printing, also assign to '_' */
1250-
/* Before, set '_' to None to avoid recursion */
1251-
if (v != Py_None &&
1252-
(err = PyDict_SetItemString(
1253-
f->f_builtins, "_", Py_None)) == 0) {
1254-
err = Py_FlushLine();
1255-
if (err == 0) {
1256-
x = PySys_GetObject("stdout");
1257-
if (x == NULL) {
1258-
PyErr_SetString(
1259-
PyExc_RuntimeError,
1260-
"lost sys.stdout");
1261-
err = -1;
1262-
}
1263-
}
1264-
if (err == 0)
1265-
err = PyFile_WriteObject(v, x, 0);
1266-
if (err == 0) {
1267-
PyFile_SoftSpace(x, 1);
1268-
err = Py_FlushLine();
1269-
}
1270-
if (err == 0) {
1271-
err = PyDict_SetItemString(
1272-
f->f_builtins, "_", v);
1273-
}
1248+
w = PySys_GetObject("displayhook");
1249+
if (w == NULL) {
1250+
PyErr_SetString(PyExc_RuntimeError,
1251+
"lost sys.displayhook");
1252+
err = -1;
1253+
}
1254+
if (err == 0) {
1255+
x = Py_BuildValue("(O)", v);
1256+
if (x == NULL)
1257+
err = -1;
1258+
}
1259+
if (err == 0) {
1260+
w = PyEval_CallObject(w, x);
1261+
if (w == NULL)
1262+
err = -1;
12741263
}
12751264
Py_DECREF(v);
1265+
Py_XDECREF(x);
12761266
break;
1277-
1267+
12781268
case PRINT_ITEM_TO:
12791269
w = stream = POP();
12801270
/* fall through to PRINT_ITEM */

Python/sysmodule.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
70114
static PyObject *
71115
sys_exc_info(PyObject *self, PyObject *args)
72116
{
@@ -332,6 +376,7 @@ extern PyObject *_Py_GetDXProfile(PyObject *, PyObject *);
332376

333377
static 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\
476521
Functions:\n\
477522
\n\
523+
displayhook() -- print an object to the screen, and save it in __builtin__._\n\
478524
exc_info() -- return thread-safe information about the current exception\n\
479525
exit() -- exit the interpreter by raising SystemExit\n\
480526
getrefcount() -- return the reference count for an object (plus one :-)\n\

0 commit comments

Comments
 (0)