diff Python/sysmodule.c @ 9328:81574e4646b4 legacy-trunk

Patches for mywrite() by Marc Lemburg: save/restore the error state reliably; check return value of vsprintf().
author Guido van Rossum <guido@python.org>
date Mon, 12 Oct 1998 18:22:10 +0000
parents 44baef8ab5c8
children 9342c60eaac0
line wrap: on
line diff
--- a/Python/sysmodule.c	Mon Oct 12 16:26:22 1998 +0000
+++ b/Python/sysmodule.c	Mon Oct 12 18:22:10 1998 +0000
@@ -594,8 +594,7 @@
 
       The first function writes to sys.stdout; the second to sys.stderr.  When
       there is a problem, they write to the real (C level) stdout or stderr;
-      no exceptions are raised (but a pending exception may be cleared when a
-      new exception is caught).
+      no exceptions are raised.
 
       Both take a printf-style format string as their first argument followed
       by a variable length argument list determined by the format string.
@@ -619,18 +618,22 @@
 	va_list va;
 {
 	PyObject *file;
+	PyObject *error_type, *error_value, *error_traceback;
 
+	PyErr_Fetch(&error_type, &error_value, &error_traceback);
 	file = PySys_GetObject(name);
 	if (file == NULL || PyFile_AsFile(file) == fp)
 		vfprintf(fp, format, va);
 	else {
 		char buffer[1001];
-		vsprintf(buffer, format, va);
+		if (vsprintf(buffer, format, va) >= sizeof(buffer))
+		    Py_FatalError("PySys_WriteStdout/err: buffer overrun");
 		if (PyFile_WriteString(buffer, file) != 0) {
 			PyErr_Clear();
 			fputs(buffer, fp);
 		}
 	}
+	PyErr_Restore(error_type, error_value, error_traceback);
 }
 
 void