comparison Python/sysmodule.c @ 64064:5d9bdf5aa07b

Issue #9599: Create PySys_FormatStdout() and PySys_FormatStderr() Write a message formatted by PyUnicode_FromFormatV() to sys.stdout and sys.stderr.
author Victor Stinner <victor.stinner@haypocalc.com>
date Mon, 16 Aug 2010 17:36:42 +0000
parents 1f37d082a9aa
children 212a16f906a6
comparison
equal deleted inserted replaced
64062:91d4c7b1bc25 64064:5d9bdf5aa07b
1832 1832
1833 /* Reimplementation of PyFile_WriteString() no calling indirectly 1833 /* Reimplementation of PyFile_WriteString() no calling indirectly
1834 PyErr_CheckSignals(): avoid the call to PyObject_Str(). */ 1834 PyErr_CheckSignals(): avoid the call to PyObject_Str(). */
1835 1835
1836 static int 1836 static int
1837 sys_pyfile_write(const char *text, PyObject *file) 1837 sys_pyfile_write_unicode(PyObject *unicode, PyObject *file)
1838 { 1838 {
1839 PyObject *unicode = NULL, *writer = NULL, *args = NULL, *result = NULL; 1839 PyObject *writer = NULL, *args = NULL, *result = NULL;
1840 int err; 1840 int err;
1841 1841
1842 if (file == NULL) 1842 if (file == NULL)
1843 return -1; 1843 return -1;
1844
1845 unicode = PyUnicode_FromString(text);
1846 if (unicode == NULL)
1847 goto error;
1848 1844
1849 writer = PyObject_GetAttrString(file, "write"); 1845 writer = PyObject_GetAttrString(file, "write");
1850 if (writer == NULL) 1846 if (writer == NULL)
1851 goto error; 1847 goto error;
1852 1848
1863 } 1859 }
1864 1860
1865 error: 1861 error:
1866 err = -1; 1862 err = -1;
1867 finally: 1863 finally:
1868 Py_XDECREF(unicode);
1869 Py_XDECREF(writer); 1864 Py_XDECREF(writer);
1870 Py_XDECREF(args); 1865 Py_XDECREF(args);
1871 Py_XDECREF(result); 1866 Py_XDECREF(result);
1872 return err; 1867 return err;
1873 } 1868 }
1874 1869
1870 static int
1871 sys_pyfile_write(const char *text, PyObject *file)
1872 {
1873 PyObject *unicode = NULL;
1874 int err;
1875
1876 if (file == NULL)
1877 return -1;
1878
1879 unicode = PyUnicode_FromString(text);
1880 if (unicode == NULL)
1881 return -1;
1882
1883 err = sys_pyfile_write_unicode(unicode, file);
1884 Py_DECREF(unicode);
1885 return err;
1886 }
1875 1887
1876 /* APIs to write to sys.stdout or sys.stderr using a printf-like interface. 1888 /* APIs to write to sys.stdout or sys.stderr using a printf-like interface.
1877 Adapted from code submitted by Just van Rossum. 1889 Adapted from code submitted by Just van Rossum.
1878 1890
1879 PySys_WriteStdout(format, ...) 1891 PySys_WriteStdout(format, ...)
1882 The first function writes to sys.stdout; the second to sys.stderr. When 1894 The first function writes to sys.stdout; the second to sys.stderr. When
1883 there is a problem, they write to the real (C level) stdout or stderr; 1895 there is a problem, they write to the real (C level) stdout or stderr;
1884 no exceptions are raised. 1896 no exceptions are raised.
1885 1897
1886 PyErr_CheckSignals() is not called to avoid the execution of the Python 1898 PyErr_CheckSignals() is not called to avoid the execution of the Python
1887 signal handlers: they may raise a new exception whereas mywrite() ignores 1899 signal handlers: they may raise a new exception whereas sys_write()
1888 all exceptions. 1900 ignores all exceptions.
1889 1901
1890 Both take a printf-style format string as their first argument followed 1902 Both take a printf-style format string as their first argument followed
1891 by a variable length argument list determined by the format string. 1903 by a variable length argument list determined by the format string.
1892 1904
1893 *** WARNING *** 1905 *** WARNING ***
1900 which can print hundreds of digits for very large numbers. 1912 which can print hundreds of digits for very large numbers.
1901 1913
1902 */ 1914 */
1903 1915
1904 static void 1916 static void
1905 mywrite(char *name, FILE *fp, const char *format, va_list va) 1917 sys_write(char *name, FILE *fp, const char *format, va_list va)
1906 { 1918 {
1907 PyObject *file; 1919 PyObject *file;
1908 PyObject *error_type, *error_value, *error_traceback; 1920 PyObject *error_type, *error_value, *error_traceback;
1909 char buffer[1001]; 1921 char buffer[1001];
1910 int written; 1922 int written;
1916 PyErr_Clear(); 1928 PyErr_Clear();
1917 fputs(buffer, fp); 1929 fputs(buffer, fp);
1918 } 1930 }
1919 if (written < 0 || (size_t)written >= sizeof(buffer)) { 1931 if (written < 0 || (size_t)written >= sizeof(buffer)) {
1920 const char *truncated = "... truncated"; 1932 const char *truncated = "... truncated";
1921 if (sys_pyfile_write(truncated, file) != 0) { 1933 if (sys_pyfile_write(truncated, file) != 0)
1922 PyErr_Clear();
1923 fputs(truncated, fp); 1934 fputs(truncated, fp);
1924 }
1925 } 1935 }
1926 PyErr_Restore(error_type, error_value, error_traceback); 1936 PyErr_Restore(error_type, error_value, error_traceback);
1927 } 1937 }
1928 1938
1929 void 1939 void
1930 PySys_WriteStdout(const char *format, ...) 1940 PySys_WriteStdout(const char *format, ...)
1931 { 1941 {
1932 va_list va; 1942 va_list va;
1933 1943
1934 va_start(va, format); 1944 va_start(va, format);
1935 mywrite("stdout", stdout, format, va); 1945 sys_write("stdout", stdout, format, va);
1936 va_end(va); 1946 va_end(va);
1937 } 1947 }
1938 1948
1939 void 1949 void
1940 PySys_WriteStderr(const char *format, ...) 1950 PySys_WriteStderr(const char *format, ...)
1941 { 1951 {
1942 va_list va; 1952 va_list va;
1943 1953
1944 va_start(va, format); 1954 va_start(va, format);
1945 mywrite("stderr", stderr, format, va); 1955 sys_write("stderr", stderr, format, va);
1946 va_end(va); 1956 va_end(va);
1947 } 1957 }
1958
1959 static void
1960 sys_format(char *name, FILE *fp, const char *format, va_list va)
1961 {
1962 PyObject *file, *message;
1963 PyObject *error_type, *error_value, *error_traceback;
1964 char *utf8;
1965
1966 PyErr_Fetch(&error_type, &error_value, &error_traceback);
1967 file = PySys_GetObject(name);
1968 message = PyUnicode_FromFormatV(format, va);
1969 if (message != NULL) {
1970 if (sys_pyfile_write_unicode(message, file) != 0) {
1971 PyErr_Clear();
1972 utf8 = _PyUnicode_AsString(message);
1973 if (utf8 != NULL)
1974 fputs(utf8, fp);
1975 }
1976 Py_DECREF(message);
1977 }
1978 PyErr_Restore(error_type, error_value, error_traceback);
1979 }
1980
1981 void
1982 PySys_FormatStdout(const char *format, ...)
1983 {
1984 va_list va;
1985
1986 va_start(va, format);
1987 sys_format("stdout", stdout, format, va);
1988 va_end(va);
1989 }
1990
1991 void
1992 PySys_FormatStderr(const char *format, ...)
1993 {
1994 va_list va;
1995
1996 va_start(va, format);
1997 sys_format("stderr", stderr, format, va);
1998 va_end(va);
1999 }