comparison Python/sysmodule.c @ 41846:308e632702bf

Enable new I/O. Disable creation of old files. Lots of stuff fails now, including -S and -m command line flags.
author Guido van Rossum <guido@python.org>
date Thu, 24 May 2007 14:31:33 +0000
parents 15b810e21fe9
children b30c75d22f34
comparison
equal deleted inserted replaced
41845:9ce8fa0a0899 41846:308e632702bf
958 settrace() -- set the global debug tracing function\n\ 958 settrace() -- set the global debug tracing function\n\
959 " 959 "
960 ) 960 )
961 /* end of sys_doc */ ; 961 /* end of sys_doc */ ;
962 962
963 static int
964 _check_and_flush (FILE *stream)
965 {
966 int prev_fail = ferror (stream);
967 return fflush (stream) || prev_fail ? EOF : 0;
968 }
969
970 /* Subversion branch and revision management */ 963 /* Subversion branch and revision management */
971 static const char _patchlevel_revision[] = PY_PATCHLEVEL_REVISION; 964 static const char _patchlevel_revision[] = PY_PATCHLEVEL_REVISION;
972 static const char headurl[] = "$HeadURL$"; 965 static const char headurl[] = "$HeadURL$";
973 static int svn_initialized; 966 static int svn_initialized;
974 static char patchlevel_revision[50]; /* Just the number */ 967 static char patchlevel_revision[50]; /* Just the number */
1056 1049
1057 PyObject * 1050 PyObject *
1058 _PySys_Init(void) 1051 _PySys_Init(void)
1059 { 1052 {
1060 PyObject *m, *v, *sysdict; 1053 PyObject *m, *v, *sysdict;
1061 PyObject *sysin, *sysout, *syserr;
1062 char *s; 1054 char *s;
1063 #ifdef MS_WINDOWS
1064 char buf[128];
1065 #endif
1066 1055
1067 m = Py_InitModule3("sys", sys_methods, sys_doc); 1056 m = Py_InitModule3("sys", sys_methods, sys_doc);
1068 if (m == NULL) 1057 if (m == NULL)
1069 return NULL; 1058 return NULL;
1070 sysdict = PyModule_GetDict(m); 1059 sysdict = PyModule_GetDict(m);
1079 PySys_WriteStderr("Python error: <stdin> is a directory, cannot continue\n"); 1068 PySys_WriteStderr("Python error: <stdin> is a directory, cannot continue\n");
1080 exit(EXIT_FAILURE); 1069 exit(EXIT_FAILURE);
1081 } 1070 }
1082 } 1071 }
1083 1072
1084 /* Closing the standard FILE* if sys.std* goes aways causes problems 1073 /* stdin/stdout/stderr are now set by site.py. */
1085 * for embedded Python usages. Closing them when somebody explicitly 1074
1086 * invokes .close() might be possible, but the FAQ promises they get
1087 * never closed. However, we still need to get write errors when
1088 * writing fails (e.g. because stdout is redirected), so we flush the
1089 * streams and check for errors before the file objects are deleted.
1090 * On OS X, fflush()ing stdin causes an error, so we exempt stdin
1091 * from that procedure.
1092 */
1093 sysin = PyFile_FromFile(stdin, "<stdin>", "r", NULL);
1094 sysout = PyFile_FromFile(stdout, "<stdout>", "w", _check_and_flush);
1095 syserr = PyFile_FromFile(stderr, "<stderr>", "w", _check_and_flush);
1096 if (PyErr_Occurred())
1097 return NULL;
1098 #ifdef MS_WINDOWS
1099 if(isatty(_fileno(stdin)) && PyFile_Check(sysin)) {
1100 sprintf(buf, "cp%d", GetConsoleCP());
1101 if (!PyFile_SetEncoding(sysin, buf))
1102 return NULL;
1103 }
1104 if(isatty(_fileno(stdout)) && PyFile_Check(sysout)) {
1105 sprintf(buf, "cp%d", GetConsoleOutputCP());
1106 if (!PyFile_SetEncoding(sysout, buf))
1107 return NULL;
1108 }
1109 if(isatty(_fileno(stderr)) && PyFile_Check(syserr)) {
1110 sprintf(buf, "cp%d", GetConsoleOutputCP());
1111 if (!PyFile_SetEncoding(syserr, buf))
1112 return NULL;
1113 }
1114 #endif
1115
1116 PyDict_SetItemString(sysdict, "stdin", sysin);
1117 PyDict_SetItemString(sysdict, "stdout", sysout);
1118 PyDict_SetItemString(sysdict, "stderr", syserr);
1119 /* Make backup copies for cleanup */
1120 PyDict_SetItemString(sysdict, "__stdin__", sysin);
1121 PyDict_SetItemString(sysdict, "__stdout__", sysout);
1122 PyDict_SetItemString(sysdict, "__stderr__", syserr);
1123 PyDict_SetItemString(sysdict, "__displayhook__", 1075 PyDict_SetItemString(sysdict, "__displayhook__",
1124 PyDict_GetItemString(sysdict, "displayhook")); 1076 PyDict_GetItemString(sysdict, "displayhook"));
1125 PyDict_SetItemString(sysdict, "__excepthook__", 1077 PyDict_SetItemString(sysdict, "__excepthook__",
1126 PyDict_GetItemString(sysdict, "excepthook")); 1078 PyDict_GetItemString(sysdict, "excepthook"));
1127 Py_XDECREF(sysin);
1128 Py_XDECREF(sysout);
1129 Py_XDECREF(syserr);
1130 PyDict_SetItemString(sysdict, "version", 1079 PyDict_SetItemString(sysdict, "version",
1131 v = PyString_FromString(Py_GetVersion())); 1080 v = PyString_FromString(Py_GetVersion()));
1132 Py_XDECREF(v); 1081 Py_XDECREF(v);
1133 PyDict_SetItemString(sysdict, "hexversion", 1082 PyDict_SetItemString(sysdict, "hexversion",
1134 v = PyInt_FromLong(PY_VERSION_HEX)); 1083 v = PyInt_FromLong(PY_VERSION_HEX));