comparison Python/bltinmodule.c @ 37044:ff381b388cff

Whoops, input *and* raw_input are slated for removal, and now both are gone.
author Neal Norwitz <nnorwitz@gmail.com>
date Fri, 17 Mar 2006 06:04:34 +0000
parents cd1818ee7ea9
children c048a09d06ff
comparison
equal deleted inserted replaced
37043:cd1818ee7ea9 37044:ff381b388cff
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 * 1076 static PyObject *
1077 builtin_input(PyObject *self, PyObject *args)
1078 {
1079 PyObject *v = NULL;
1080 PyObject *fin = PySys_GetObject("stdin");
1081 PyObject *fout = PySys_GetObject("stdout");
1082
1083 if (!PyArg_UnpackTuple(args, "input", 0, 1, &v))
1084 return NULL;
1085
1086 if (fin == NULL) {
1087 PyErr_SetString(PyExc_RuntimeError, "input: lost sys.stdin");
1088 return NULL;
1089 }
1090 if (fout == NULL) {
1091 PyErr_SetString(PyExc_RuntimeError, "input: lost sys.stdout");
1092 return NULL;
1093 }
1094 if (PyFile_SoftSpace(fout, 0)) {
1095 if (PyFile_WriteString(" ", fout) != 0)
1096 return NULL;
1097 }
1098 if (PyFile_Check(fin) && PyFile_Check(fout)
1099 && isatty(fileno(PyFile_AsFile(fin)))
1100 && isatty(fileno(PyFile_AsFile(fout)))) {
1101 PyObject *po;
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);
1149 }
1150
1151 PyDoc_STRVAR(input_doc,
1152 "input([prompt]) -> string\n\
1153 \n\
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
1159
1160
1161 static PyObject *
1162 builtin_intern(PyObject *self, PyObject *args) 1077 builtin_intern(PyObject *self, PyObject *args)
1163 { 1078 {
1164 PyObject *s; 1079 PyObject *s;
1165 if (!PyArg_ParseTuple(args, "S:intern", &s)) 1080 if (!PyArg_ParseTuple(args, "S:intern", &s))
1166 return NULL; 1081 return NULL;
2191 {"globals", (PyCFunction)builtin_globals, METH_NOARGS, globals_doc}, 2106 {"globals", (PyCFunction)builtin_globals, METH_NOARGS, globals_doc},
2192 {"hasattr", builtin_hasattr, METH_VARARGS, hasattr_doc}, 2107 {"hasattr", builtin_hasattr, METH_VARARGS, hasattr_doc},
2193 {"hash", builtin_hash, METH_O, hash_doc}, 2108 {"hash", builtin_hash, METH_O, hash_doc},
2194 {"hex", builtin_hex, METH_O, hex_doc}, 2109 {"hex", builtin_hex, METH_O, hex_doc},
2195 {"id", builtin_id, METH_O, id_doc}, 2110 {"id", builtin_id, METH_O, id_doc},
2196 {"input", builtin_input, METH_VARARGS, input_doc},
2197 {"intern", builtin_intern, METH_VARARGS, intern_doc}, 2111 {"intern", builtin_intern, METH_VARARGS, intern_doc},
2198 {"isinstance", builtin_isinstance, METH_VARARGS, isinstance_doc}, 2112 {"isinstance", builtin_isinstance, METH_VARARGS, isinstance_doc},
2199 {"issubclass", builtin_issubclass, METH_VARARGS, issubclass_doc}, 2113 {"issubclass", builtin_issubclass, METH_VARARGS, issubclass_doc},
2200 {"iter", builtin_iter, METH_VARARGS, iter_doc}, 2114 {"iter", builtin_iter, METH_VARARGS, iter_doc},
2201 {"len", builtin_len, METH_O, len_doc}, 2115 {"len", builtin_len, METH_O, len_doc},