Skip to content

Commit 5fb5ae2

Browse files
author
bwarsaw
committed
PEP 214, Extended print Statement, has been accepted by the BDFL.
eval_code2(): Implement new bytecodes PRINT_ITEM_TO and PRINT_NEWLINE_TO, as per accepted SF patch #100970. Also update graminit.c based on related Grammar/Grammar changes. git-svn-id: http://svn.python.org/projects/python/trunk@16916 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent 3601cb1 commit 5fb5ae2

2 files changed

Lines changed: 184 additions & 138 deletions

File tree

Python/ceval.c

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@ eval_code2(PyCodeObject *co, PyObject *globals, PyObject *locals,
326326
register PyObject *w;
327327
register PyObject *u;
328328
register PyObject *t;
329+
register PyObject *stream = NULL; /* for PRINT opcodes */
329330
register PyFrameObject *f; /* Current frame */
330331
register PyObject **fastlocals;
331332
PyObject *retval = NULL; /* Return value */
@@ -635,7 +636,6 @@ eval_code2(PyCodeObject *co, PyObject *globals, PyObject *locals,
635636
}
636637
}
637638
#endif
638-
639639
/* Main switch on opcode */
640640

641641
switch (opcode) {
@@ -975,7 +975,7 @@ eval_code2(PyCodeObject *co, PyObject *globals, PyObject *locals,
975975
Py_DECREF(w);
976976
if (err == 0) continue;
977977
break;
978-
978+
979979
case PRINT_EXPR:
980980
v = POP();
981981
/* Print value except if None */
@@ -1008,15 +1008,21 @@ eval_code2(PyCodeObject *co, PyObject *globals, PyObject *locals,
10081008
Py_DECREF(v);
10091009
break;
10101010

1011+
case PRINT_ITEM_TO:
1012+
w = stream = POP();
1013+
/* fall through to PRINT_ITEM */
1014+
10111015
case PRINT_ITEM:
10121016
v = POP();
1013-
w = PySys_GetObject("stdout");
1014-
if (w == NULL) {
1015-
PyErr_SetString(PyExc_RuntimeError,
1016-
"lost sys.stdout");
1017-
err = -1;
1017+
if (stream == NULL) {
1018+
w = PySys_GetObject("stdout");
1019+
if (w == NULL) {
1020+
PyErr_SetString(PyExc_RuntimeError,
1021+
"lost sys.stdout");
1022+
err = -1;
1023+
}
10181024
}
1019-
else if (PyFile_SoftSpace(w, 1))
1025+
if (w != NULL && PyFile_SoftSpace(w, 1))
10201026
err = PyFile_WriteString(" ", w);
10211027
if (err == 0)
10221028
err = PyFile_WriteObject(v, w, Py_PRINT_RAW);
@@ -1030,19 +1036,30 @@ eval_code2(PyCodeObject *co, PyObject *globals, PyObject *locals,
10301036
PyFile_SoftSpace(w, 0);
10311037
}
10321038
Py_DECREF(v);
1033-
if (err == 0) continue;
1039+
Py_XDECREF(stream);
1040+
stream = NULL;
1041+
if (err == 0)
1042+
continue;
10341043
break;
10351044

1045+
case PRINT_NEWLINE_TO:
1046+
w = stream = POP();
1047+
/* fall through to PRINT_NEWLINE */
1048+
10361049
case PRINT_NEWLINE:
1037-
x = PySys_GetObject("stdout");
1038-
if (x == NULL)
1039-
PyErr_SetString(PyExc_RuntimeError,
1040-
"lost sys.stdout");
1041-
else {
1042-
err = PyFile_WriteString("\n", x);
1050+
if (stream == NULL) {
1051+
w = PySys_GetObject("stdout");
1052+
if (w == NULL)
1053+
PyErr_SetString(PyExc_RuntimeError,
1054+
"lost sys.stdout");
1055+
}
1056+
if (w != NULL) {
1057+
err = PyFile_WriteString("\n", w);
10431058
if (err == 0)
1044-
PyFile_SoftSpace(x, 0);
1059+
PyFile_SoftSpace(w, 0);
10451060
}
1061+
Py_XDECREF(stream);
1062+
stream = NULL;
10461063
break;
10471064

10481065
case BREAK_LOOP:

0 commit comments

Comments
 (0)