[Python-Dev] At the interactive port
Moshe Zadka
Moshe Zadka <mzadka@geocities.com>
Sat, 29 Apr 2000 21:09:40 +0300 (IDT)
Continuing the recent debate about what is appropriate to the interactive
prompt printing, and the wide agreement that whatever we decide, users
might think otherwise, I've written up a patch to have the user control
via a function in __builtin__ the way things are printed at the prompt.
This is not patches@python level stuff for two reasons:
1. I'm not sure what to call this function. Currently, I call it
__print_expr__, but I'm not sure it's a good name
2. I haven't yet supplied a default in __builtin__, so the user *must*
override this. This is unacceptable, of course.
I'd just like people to tell me if they think this is worth while, and if
there is anything I missed.
*** ../python/dist/src/Python/ceval.c Fri Mar 31 04:42:47 2000
--- Python/ceval.c Sat Apr 29 03:55:36 2000
***************
*** 1014,1047 ****
case PRINT_EXPR:
v = POP();
! /* Print value except if None */
! /* After printing, also assign to '_' */
! /* Before, set '_' to None to avoid recursion */
! if (v != Py_None &&
! (err = PyDict_SetItemString(
! f->f_builtins, "_", Py_None)) == 0) {
! err = Py_FlushLine();
! if (err == 0) {
! x = PySys_GetObject("stdout");
! if (x == NULL) {
! PyErr_SetString(
! PyExc_RuntimeError,
! "lost sys.stdout");
! err = -1;
! }
! }
! if (err == 0)
! err = PyFile_WriteObject(v, x, 0);
! if (err == 0) {
! PyFile_SoftSpace(x, 1);
! err = Py_FlushLine();
! }
! if (err == 0) {
! err = PyDict_SetItemString(
! f->f_builtins, "_", v);
! }
}
! Py_DECREF(v);
break;
case PRINT_ITEM:
--- 1014,1035 ----
case PRINT_EXPR:
v = POP();
! x = PyDict_GetItemString(f->f_builtins,
! "__print_expr__");
! if (x == NULL) {
! PyErr_SetString(PyExc_SystemError,
! "__print_expr__ not found");
! Py_DECREF(v);
! break;
! }
! t = PyTuple_New(1);
! if (t != NULL) {
! PyTuple_SET_ITEM(t, 0, v);
! w = PyEval_CallObject(x, t);
! Py_XDECREF(w);
}
! /*Py_DECREF(x);*/
! Py_XDECREF(t);
break;
case PRINT_ITEM:
--
Moshe Zadka <mzadka@geocities.com>.
http://www.oreilly.com/news/prescod_0300.html
http://www.linux.org.il -- we put the penguin in .com