From 6d6349eea4e2210a69f4617c5a371a3529a8bdea Mon Sep 17 00:00:00 2001 From: tibs Date: Tue, 17 Feb 2009 11:45:14 +0000 Subject: [PATCH] Since none of the printing calls care about a return value, make the new printing mechanism simpler, by having functions with void returns. --HG-- extra : convert_revision : svn%3Aeff31bef-be4a-0410-a8fe-e47997df2690/trunk%40118 --- printing.c | 60 +++++++++++++++---------------------- printing_fns.h | 24 +++++---------- python/tstools/printing.pyx | 44 +++++++++------------------ test_printing.c | 14 ++++----- 4 files changed, 52 insertions(+), 90 deletions(-) diff --git a/printing.c b/printing.c index 0208bba..a6838fa 100644 --- a/printing.c +++ b/printing.c @@ -37,32 +37,32 @@ // Default printing functions // ============================================================ -static int print_message_to_stdout(const char *message) +static void print_message_to_stdout(const char *message) { - return (fputs(message,stdout) == 0 ? 0:1); + (void) fputs(message,stdout); } -static int print_message_to_stderr(const char *message) +static void print_message_to_stderr(const char *message) { - return (fputs(message,stderr) == 0 ? 0:1); + (void) fputs(message,stderr); } -static int fprint_message_to_stdout(const char *format, va_list arg_ptr) +static void fprint_message_to_stdout(const char *format, va_list arg_ptr) { - return (vfprintf(stdout, format, arg_ptr) < 0 ? 1:0); + (void) vfprintf(stdout, format, arg_ptr); } -static int fprint_message_to_stderr(const char *format, va_list arg_ptr) +static void fprint_message_to_stderr(const char *format, va_list arg_ptr) { - return (vfprintf(stderr, format, arg_ptr) < 0 ? 1:0); + (void) vfprintf(stderr, format, arg_ptr); } // ============================================================ // Print redirection // ============================================================ -static int (*print_message_fn) (const char *message) = print_message_to_stdout; -static int (*print_error_fn) (const char *message) = print_message_to_stdout; +static void (*print_message_fn) (const char *message) = print_message_to_stdout; +static void (*print_error_fn) (const char *message) = print_message_to_stdout; -static int (*fprint_message_fn) (const char *format, va_list arg_ptr) = fprint_message_to_stdout; -static int (*fprint_error_fn) (const char *format, va_list arg_ptr) = fprint_message_to_stdout; +static void (*fprint_message_fn) (const char *format, va_list arg_ptr) = fprint_message_to_stdout; +static void (*fprint_error_fn) (const char *format, va_list arg_ptr) = fprint_message_to_stdout; // ============================================================ @@ -70,55 +70,43 @@ static int (*fprint_error_fn) (const char *format, va_list arg_ptr) = fprint_m // ============================================================ /* * Prints the given string, as a normal message. - * - * Returns 0 if all goes well, 1 if something goes wrong. */ -extern int print_msg(const char *text) +extern void print_msg(const char *text) { - return print_message_fn(text); + print_message_fn(text); } /* * Prints the given string, as an error message. - * - * Returns 0 if all goes well, 1 if something goes wrong. */ -extern int print_err(const char *text) +extern void print_err(const char *text) { - return print_error_fn(text); + print_error_fn(text); } /* * Prints the given format text, as a normal message. - * - * Returns 0 if all goes well, 1 if something goes wrong. */ -extern int fprint_msg(const char *format, ...) +extern void fprint_msg(const char *format, ...) { - int retval; va_list va_arg; va_start(va_arg, format); - retval = fprint_message_fn(format, va_arg); + fprint_message_fn(format, va_arg); va_end(va_arg); - return retval; } /* * Prints the given formatted text, as an error message. - * - * Returns 0 if all goes well, 1 if something goes wrong. */ -extern int fprint_err(const char *format, ...) +extern void fprint_err(const char *format, ...) { - int retval; va_list va_arg; va_start(va_arg, format); - retval = fprint_error_fn(format, va_arg); + fprint_error_fn(format, va_arg); va_end(va_arg); - return retval; } // ============================================================ @@ -172,10 +160,10 @@ extern void redirect_output_stdout(void) * * Returns 0 if all goes well, 1 if something goes wrong. */ -extern int redirect_output( int (*new_print_message_fn) (const char *message), - int (*new_print_error_fn) (const char *message), - int (*new_fprint_message_fn) (const char *format, va_list arg_ptr), - int (*new_fprint_error_fn) (const char *format, va_list arg_ptr) +extern int redirect_output( void (*new_print_message_fn) (const char *message), + void (*new_print_error_fn) (const char *message), + void (*new_fprint_message_fn) (const char *format, va_list arg_ptr), + void (*new_fprint_error_fn) (const char *format, va_list arg_ptr) ) { if (new_print_message_fn == NULL || new_print_error_fn == NULL || diff --git a/printing_fns.h b/printing_fns.h index 90fb35c..b0847db 100644 --- a/printing_fns.h +++ b/printing_fns.h @@ -36,28 +36,20 @@ // ============================================================ /* * Prints the given string, as a normal message. - * - * Returns 0 if all goes well, 1 if something goes wrong. */ -extern int print_msg(const char *text); +extern void print_msg(const char *text); /* * Prints the given string, as an error message. - * - * Returns 0 if all goes well, 1 if something goes wrong. */ -extern int print_err(const char *text); +extern void print_err(const char *text); /* * Prints the given format text, as a normal message. - * - * Returns 0 if all goes well, 1 if something goes wrong. */ -extern int fprint_msg(const char *format, ...); +extern void fprint_msg(const char *format, ...); /* * Prints the given formatted text, as an error message. - * - * Returns 0 if all goes well, 1 if something goes wrong. */ -extern int fprint_err(const char *format, ...); +extern void fprint_err(const char *format, ...); // ============================================================ // Choosing what the printing functions do @@ -94,10 +86,10 @@ extern void redirect_output_stdout(void); * * Returns 0 if all goes well, 1 if something goes wrong. */ -extern int redirect_output( int (*new_print_message_fn) (const char *message), - int (*new_print_error_fn) (const char *message), - int (*new_fprint_message_fn) (const char *format, va_list arg_ptr), - int (*new_fprint_error_fn) (const char *format, va_list arg_ptr) +extern int redirect_output( void (*new_print_message_fn) (const char *message), + void (*new_print_error_fn) (const char *message), + void (*new_fprint_message_fn) (const char *format, va_list arg_ptr), + void (*new_fprint_error_fn) (const char *format, va_list arg_ptr) ); #endif // _printing_fns diff --git a/python/tstools/printing.pyx b/python/tstools/printing.pyx index e099a62..a79f916 100644 --- a/python/tstools/printing.pyx +++ b/python/tstools/printing.pyx @@ -37,16 +37,6 @@ I can test most easily! import sys import array -from common cimport FILE, EOF, stdout, fopen, fclose, fileno -from common cimport errno, strerror, free -from common cimport const_void_ptr -from common cimport PyString_FromStringAndSize, PyString_AsStringAndSize, \ - PyObject_AsReadBuffer -from common cimport uint8_t, uint16_t, uint32_t, uint64_t -from common cimport int8_t, int16_t, int32_t, int64_t - -from tstools import TSToolsException - cdef extern from *: ctypedef char* const_char_ptr "const char*" @@ -66,6 +56,8 @@ cdef extern from "Python.h": # written to the real (C level) stdout. void PySys_WriteStdout(const_char_ptr format, ...) + # Unfortunately, there are two common ways of implementing a va_list, + # and we just have to guess which is being used. ctypedef void * va_list #ctypedef struct va_list va_list @@ -77,32 +69,24 @@ cdef extern from "Python.h": # ctypedef void* va_list "va_list" cdef extern from 'printing_fns.h': - int print_msg(const_char_ptr text) - int print_err(const_char_ptr text) - int fprint_msg(const_char_ptr format, ...) - int fprint_err(const_char_ptr format, ...) - int redirect_output( int (*new_print_message_fn) (const_char_ptr message), - int (*new_print_error_fn) (const_char_ptr message), - int (*new_fprint_message_fn) (const_char_ptr format, va_list arg_ptr), - int (*new_fprint_error_fn) (const_char_ptr format, va_list arg_ptr) + void print_msg(const_char_ptr text) + void print_err(const_char_ptr text) + void fprint_msg(const_char_ptr format, ...) + void fprint_err(const_char_ptr format, ...) + int redirect_output( void (*new_print_message_fn) (const_char_ptr message), + void (*new_print_error_fn) (const_char_ptr message), + void (*new_fprint_message_fn) (const_char_ptr format, va_list arg_ptr), + void (*new_fprint_error_fn) (const_char_ptr format, va_list arg_ptr) ) -cdef int our_print_msg(const_char_ptr text): +cdef void our_print_msg(const_char_ptr text): PySys_WriteStdout('%s',text) - return 0 -cdef int our_format_msg(const_char_ptr format, va_list arg_ptr): +cdef void our_format_msg(const_char_ptr format, va_list arg_ptr): cdef int err cdef char buffer[1000] - err = PyOS_vsnprintf(buffer, 1000, format, arg_ptr) - if err < 0: - return 1 - elif err > 999: - PySys_WriteStdout('%s',buffer) - return 1 - else: - PySys_WriteStdout('%s',buffer) - return 0 + PyOS_vsnprintf(buffer, 1000, format, arg_ptr) + PySys_WriteStdout('%s',buffer) def setup_printing(): cdef int err diff --git a/test_printing.c b/test_printing.c index 4118cca..3c54d5e 100644 --- a/test_printing.c +++ b/test_printing.c @@ -33,25 +33,23 @@ // Some example redirection routines -static int print_message_to_stdout(const char *message) +static void print_message_to_stdout(const char *message) { (void) printf("<<>> %s",message); - return 0; } -static int print_message_to_stderr(const char *message) +static void print_message_to_stderr(const char *message) { (void) printf("<<>> %s",message); - return 0; } -static int fprint_message_to_stdout(const char *format, va_list arg_ptr) +static void fprint_message_to_stdout(const char *format, va_list arg_ptr) { printf("<<>> "); - return (vfprintf(stdout, format, arg_ptr) < 0 ? 1:0); + (void) vfprintf(stdout, format, arg_ptr); } -static int fprint_message_to_stderr(const char *format, va_list arg_ptr) +static void fprint_message_to_stderr(const char *format, va_list arg_ptr) { printf("<<>> "); - return (vfprintf(stdout, format, arg_ptr) < 0 ? 1:0); + (void) vfprintf(stdout, format, arg_ptr); } static void print_usage()