comparison Python/bltinmodule.c @ 37043:cd1818ee7ea9

raw_input() -> input(). old input behavior is history (and test_builtin passes again). It was failing due to future division.
author Neal Norwitz <nnorwitz@gmail.com>
date Fri, 17 Mar 2006 05:59:16 +0000
parents 9eb24f92a784
children ff381b388cff
comparison
equal deleted inserted replaced
37042:7ada16a14968 37043:cd1818ee7ea9
1071 "hex(number) -> string\n\ 1071 "hex(number) -> string\n\
1072 \n\ 1072 \n\
1073 Return the hexadecimal representation of an integer or long integer."); 1073 Return the hexadecimal representation of an integer or long integer.");
1074 1074
1075 1075
1076 static PyObject *builtin_raw_input(PyObject *, PyObject *);
1077
1078 static PyObject * 1076 static PyObject *
1079 builtin_input(PyObject *self, PyObject *args) 1077 builtin_input(PyObject *self, PyObject *args)
1080 { 1078 {
1081 PyObject *line; 1079 PyObject *v = NULL;
1082 char *str; 1080 PyObject *fin = PySys_GetObject("stdin");
1083 PyObject *res; 1081 PyObject *fout = PySys_GetObject("stdout");
1084 PyObject *globals, *locals; 1082
1085 PyCompilerFlags cf; 1083 if (!PyArg_UnpackTuple(args, "input", 0, 1, &v))
1086 1084 return NULL;
1087 line = builtin_raw_input(self, args); 1085
1088 if (line == NULL) 1086 if (fin == NULL) {
1089 return line; 1087 PyErr_SetString(PyExc_RuntimeError, "input: lost sys.stdin");
1090 if (!PyArg_Parse(line, "s;embedded '\\0' in input line", &str)) 1088 return NULL;
1091 return NULL; 1089 }
1092 while (*str == ' ' || *str == '\t') 1090 if (fout == NULL) {
1093 str++; 1091 PyErr_SetString(PyExc_RuntimeError, "input: lost sys.stdout");
1094 globals = PyEval_GetGlobals(); 1092 return NULL;
1095 locals = PyEval_GetLocals(); 1093 }
1096 if (PyDict_GetItemString(globals, "__builtins__") == NULL) { 1094 if (PyFile_SoftSpace(fout, 0)) {
1097 if (PyDict_SetItemString(globals, "__builtins__", 1095 if (PyFile_WriteString(" ", fout) != 0)
1098 PyEval_GetBuiltins()) != 0)
1099 return NULL; 1096 return NULL;
1100 } 1097 }
1101 cf.cf_flags = 0; 1098 if (PyFile_Check(fin) && PyFile_Check(fout)
1102 PyEval_MergeCompilerFlags(&cf); 1099 && isatty(fileno(PyFile_AsFile(fin)))
1103 res = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf); 1100 && isatty(fileno(PyFile_AsFile(fout)))) {
1104 Py_DECREF(line); 1101 PyObject *po;
1105 return res; 1102 char *prompt;
1103 char *s;
1104 PyObject *result;
1105 if (v != NULL) {
1106 po = PyObject_Str(v);
1107 if (po == NULL)
1108 return NULL;
1109 prompt = PyString_AsString(po);
1110 if (prompt == NULL)
1111 return NULL;
1112 }
1113 else {
1114 po = NULL;
1115 prompt = "";
1116 }
1117 s = PyOS_Readline(PyFile_AsFile(fin), PyFile_AsFile(fout),
1118 prompt);
1119 Py_XDECREF(po);
1120 if (s == NULL) {
1121 if (!PyErr_Occurred())
1122 PyErr_SetNone(PyExc_KeyboardInterrupt);
1123 return NULL;
1124 }
1125 if (*s == '\0') {
1126 PyErr_SetNone(PyExc_EOFError);
1127 result = NULL;
1128 }
1129 else { /* strip trailing '\n' */
1130 size_t len = strlen(s);
1131 if (len > INT_MAX) {
1132 PyErr_SetString(PyExc_OverflowError,
1133 "[raw_]input: input too long");
1134 result = NULL;
1135 }
1136 else {
1137 result = PyString_FromStringAndSize(s,
1138 (int)(len-1));
1139 }
1140 }
1141 PyMem_FREE(s);
1142 return result;
1143 }
1144 if (v != NULL) {
1145 if (PyFile_WriteObject(v, fout, Py_PRINT_RAW) != 0)
1146 return NULL;
1147 }
1148 return PyFile_GetLine(fin, -1);
1106 } 1149 }
1107 1150
1108 PyDoc_STRVAR(input_doc, 1151 PyDoc_STRVAR(input_doc,
1109 "input([prompt]) -> value\n\ 1152 "input([prompt]) -> string\n\
1110 \n\ 1153 \n\
1111 Equivalent to eval(raw_input(prompt))."); 1154 Read a string from standard input. The trailing newline is stripped.\n\
1155 If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.\n\
1156 On Unix, GNU readline is used if enabled. The prompt string, if given,\n\
1157 is printed without a trailing newline before reading.");
1158
1112 1159
1113 1160
1114 static PyObject * 1161 static PyObject *
1115 builtin_intern(PyObject *self, PyObject *args) 1162 builtin_intern(PyObject *self, PyObject *args)
1116 { 1163 {
1682 Return a list containing an arithmetic progression of integers.\n\ 1729 Return a list containing an arithmetic progression of integers.\n\
1683 range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.\n\ 1730 range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.\n\
1684 When step is given, it specifies the increment (or decrement).\n\ 1731 When step is given, it specifies the increment (or decrement).\n\
1685 For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!\n\ 1732 For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!\n\
1686 These are exactly the valid indices for a list of 4 elements."); 1733 These are exactly the valid indices for a list of 4 elements.");
1687
1688
1689 static PyObject *
1690 builtin_raw_input(PyObject *self, PyObject *args)
1691 {
1692 PyObject *v = NULL;
1693 PyObject *fin = PySys_GetObject("stdin");
1694 PyObject *fout = PySys_GetObject("stdout");
1695
1696 if (!PyArg_UnpackTuple(args, "[raw_]input", 0, 1, &v))
1697 return NULL;
1698
1699 if (fin == NULL) {
1700 PyErr_SetString(PyExc_RuntimeError, "[raw_]input: lost sys.stdin");
1701 return NULL;
1702 }
1703 if (fout == NULL) {
1704 PyErr_SetString(PyExc_RuntimeError, "[raw_]input: lost sys.stdout");
1705 return NULL;
1706 }
1707 if (PyFile_SoftSpace(fout, 0)) {
1708 if (PyFile_WriteString(" ", fout) != 0)
1709 return NULL;
1710 }
1711 if (PyFile_Check(fin) && PyFile_Check(fout)
1712 && isatty(fileno(PyFile_AsFile(fin)))
1713 && isatty(fileno(PyFile_AsFile(fout)))) {
1714 PyObject *po;
1715 char *prompt;
1716 char *s;
1717 PyObject *result;
1718 if (v != NULL) {
1719 po = PyObject_Str(v);
1720 if (po == NULL)
1721 return NULL;
1722 prompt = PyString_AsString(po);
1723 if (prompt == NULL)
1724 return NULL;
1725 }
1726 else {
1727 po = NULL;
1728 prompt = "";
1729 }
1730 s = PyOS_Readline(PyFile_AsFile(fin), PyFile_AsFile(fout),
1731 prompt);
1732 Py_XDECREF(po);
1733 if (s == NULL) {
1734 if (!PyErr_Occurred())
1735 PyErr_SetNone(PyExc_KeyboardInterrupt);
1736 return NULL;
1737 }
1738 if (*s == '\0') {
1739 PyErr_SetNone(PyExc_EOFError);
1740 result = NULL;
1741 }
1742 else { /* strip trailing '\n' */
1743 size_t len = strlen(s);
1744 if (len > INT_MAX) {
1745 PyErr_SetString(PyExc_OverflowError,
1746 "[raw_]input: input too long");
1747 result = NULL;
1748 }
1749 else {
1750 result = PyString_FromStringAndSize(s,
1751 (int)(len-1));
1752 }
1753 }
1754 PyMem_FREE(s);
1755 return result;
1756 }
1757 if (v != NULL) {
1758 if (PyFile_WriteObject(v, fout, Py_PRINT_RAW) != 0)
1759 return NULL;
1760 }
1761 return PyFile_GetLine(fin, -1);
1762 }
1763
1764 PyDoc_STRVAR(raw_input_doc,
1765 "raw_input([prompt]) -> string\n\
1766 \n\
1767 Read a string from standard input. The trailing newline is stripped.\n\
1768 If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.\n\
1769 On Unix, GNU readline is used if enabled. The prompt string, if given,\n\
1770 is printed without a trailing newline before reading.");
1771 1734
1772 1735
1773 static PyObject * 1736 static PyObject *
1774 builtin_reduce(PyObject *self, PyObject *args) 1737 builtin_reduce(PyObject *self, PyObject *args)
1775 { 1738 {
2242 {"min", (PyCFunction)builtin_min, METH_VARARGS | METH_KEYWORDS, min_doc}, 2205 {"min", (PyCFunction)builtin_min, METH_VARARGS | METH_KEYWORDS, min_doc},
2243 {"oct", builtin_oct, METH_O, oct_doc}, 2206 {"oct", builtin_oct, METH_O, oct_doc},
2244 {"ord", builtin_ord, METH_O, ord_doc}, 2207 {"ord", builtin_ord, METH_O, ord_doc},
2245 {"pow", builtin_pow, METH_VARARGS, pow_doc}, 2208 {"pow", builtin_pow, METH_VARARGS, pow_doc},
2246 {"range", builtin_range, METH_VARARGS, range_doc}, 2209 {"range", builtin_range, METH_VARARGS, range_doc},
2247 {"raw_input", builtin_raw_input, METH_VARARGS, raw_input_doc},
2248 {"reduce", builtin_reduce, METH_VARARGS, reduce_doc}, 2210 {"reduce", builtin_reduce, METH_VARARGS, reduce_doc},
2249 {"reload", builtin_reload, METH_O, reload_doc}, 2211 {"reload", builtin_reload, METH_O, reload_doc},
2250 {"repr", builtin_repr, METH_O, repr_doc}, 2212 {"repr", builtin_repr, METH_O, repr_doc},
2251 {"round", builtin_round, METH_VARARGS, round_doc}, 2213 {"round", builtin_round, METH_VARARGS, round_doc},
2252 {"setattr", builtin_setattr, METH_VARARGS, setattr_doc}, 2214 {"setattr", builtin_setattr, METH_VARARGS, setattr_doc},