Index: Python/sysmodule.c |
diff --git a/Python/sysmodule.c b/Python/sysmodule.c |
index 61c9b1ef33354e10529dedef684857d5613b485a..8fe5fa7fecd21dac833b0d2fa08ae4320a7da1ad 100644 |
--- a/Python/sysmodule.c |
+++ b/Python/sysmodule.c |
@@ -1834,18 +1834,14 @@ PySys_SetArgv(int argc, wchar_t **argv) |
PyErr_CheckSignals(): avoid the call to PyObject_Str(). */ |
static int |
-sys_pyfile_write(const char *text, PyObject *file) |
+sys_pyfile_write_unicode(PyObject *unicode, PyObject *file) |
{ |
- PyObject *unicode = NULL, *writer = NULL, *args = NULL, *result = NULL; |
+ PyObject *writer = NULL, *args = NULL, *result = NULL; |
int err; |
if (file == NULL) |
return -1; |
- unicode = PyUnicode_FromString(text); |
- if (unicode == NULL) |
- goto error; |
- |
writer = PyObject_GetAttrString(file, "write"); |
if (writer == NULL) |
goto error; |
@@ -1865,13 +1861,29 @@ sys_pyfile_write(const char *text, PyObject *file) |
error: |
err = -1; |
finally: |
- Py_XDECREF(unicode); |
Py_XDECREF(writer); |
Py_XDECREF(args); |
Py_XDECREF(result); |
return err; |
} |
+static int |
+sys_pyfile_write(const char *text, PyObject *file) |
+{ |
+ PyObject *unicode = NULL; |
+ int err; |
+ |
+ if (file == NULL) |
+ return -1; |
+ |
+ unicode = PyUnicode_FromString(text); |
+ if (unicode == NULL) |
+ return -1; |
+ |
+ err = sys_pyfile_write_unicode(unicode, file); |
+ Py_DECREF(unicode); |
+ return err; |
+} |
/* APIs to write to sys.stdout or sys.stderr using a printf-like interface. |
Adapted from code submitted by Just van Rossum. |
@@ -1918,10 +1930,28 @@ mywrite(char *name, FILE *fp, const char *format, va_list va) |
} |
if (written < 0 || (size_t)written >= sizeof(buffer)) { |
const char *truncated = "... truncated"; |
- if (sys_pyfile_write(truncated, file) != 0) { |
- PyErr_Clear(); |
+ if (sys_pyfile_write(truncated, file) != 0) |
fputs(truncated, fp); |
- } |
+ } |
+ PyErr_Restore(error_type, error_value, error_traceback); |
+} |
+ |
+static void |
+myformat(char *name, FILE *fp, const char *format, va_list va) |
Ezio Melotti
2010/07/30 00:38:52
Is this function public or should it be _myformat?
haypo
2010/07/30 03:45:40
"static" means private. I think that the name is c
|
+{ |
+ PyObject *file; |
+ PyObject *error_type, *error_value, *error_traceback; |
+ PyObject *unicode; |
+ char *str; |
+ |
+ PyErr_Fetch(&error_type, &error_value, &error_traceback); |
+ file = PySys_GetObject(name); |
+ unicode = PyUnicode_FromFormatV(format, va); |
+ if (sys_pyfile_write_unicode(unicode, file) != 0) { |
+ PyErr_Clear(); |
+ str = _PyUnicode_AsString(unicode); |
+ if (str != NULL) |
+ fputs(str, fp); |
} |
PyErr_Restore(error_type, error_value, error_traceback); |
} |
@@ -1945,3 +1975,13 @@ PySys_WriteStderr(const char *format, ...) |
mywrite("stderr", stderr, format, va); |
va_end(va); |
} |
+ |
+void |
+PySys_FormatStderr(const char *format, ...) |
Ezio Melotti
2010/07/30 00:38:52
Shouldn't this be documented?
|
+{ |
+ va_list va; |
+ |
+ va_start(va, format); |
+ myformat("stderr", stderr, format, va); |
+ va_end(va); |
+} |