ruby-changes:26506
From: nobu <ko1@a...>
Date: Sat, 22 Dec 2012 23:59:31 +0900 (JST)
Subject: [ruby-changes:26506] nobu:r38557 (trunk): error.c: PRIsVALUE
nobu 2012-12-22 23:59:21 +0900 (Sat, 22 Dec 2012) New Revision: 38557 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=38557 Log: error.c: PRIsVALUE * error.c (rb_compile_error, rb_compile_warn, rb_compile_warning), (rb_warn, rb_warning): support PRIsVALUE. Modified files: trunk/ChangeLog trunk/error.c Index: ChangeLog =================================================================== --- ChangeLog (revision 38556) +++ ChangeLog (revision 38557) @@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Sat Dec 22 23:59:18 2012 Nobuyoshi Nakada <nobu@r...> + + * error.c (rb_compile_error, rb_compile_warn, rb_compile_warning), + (rb_warn, rb_warning): support PRIsVALUE. + Sat Dec 22 22:04:58 2012 CHIKANAGA Tomoyuki <nagachika@r...> * cont.c (rb_fiber_start): unify conditions. Index: error.c =================================================================== --- error.c (revision 38556) +++ error.c (revision 38557) @@ -73,36 +73,23 @@ err_position_0(char *buf, long len, cons https://github.com/ruby/ruby/blob/trunk/error.c#L73 } } -static int -err_position(char *buf, long len) +static VALUE +compile_snprintf(rb_encoding *enc, const char *pre, const char *file, int line, const char *fmt, va_list args) { - return err_position_0(buf, len, rb_sourcefile(), rb_sourceline()); -} - -static void -err_snprintf(char *buf, long len, const char *fmt, va_list args) -{ - long n; - - n = err_position(buf, len); - if (len > n) { - vsnprintf((char*)buf+n, len-n, fmt, args); - } -} - -static void -compile_snprintf(char *buf, long len, const char *file, int line, const char *fmt, va_list args) -{ - long n; + VALUE str = rb_enc_str_new(0, 0, enc); - n = err_position_0(buf, len, file, line); - if (len > n) { - vsnprintf((char*)buf+n, len-n, fmt, args); + if (file) { + rb_str_cat2(str, file); + if (line) rb_str_catf(str, ":%d", line); + rb_str_cat2(str, ": "); } + if (pre) rb_str_cat2(str, pre); + rb_str_vcatf(str, fmt, args); + return str; } static void -compile_err_append(const char *s, rb_encoding *enc) +compile_err_append(VALUE mesg) { rb_thread_t *th = GET_THREAD(); VALUE err = th->errinfo; @@ -112,26 +99,23 @@ compile_err_append(const char *s, rb_enc https://github.com/ruby/ruby/blob/trunk/error.c#L99 /* after this line, any Ruby code *can* run */ if (th->mild_compile_error) { - if (!RTEST(err)) { - err = rb_exc_new3(rb_eSyntaxError, - rb_enc_str_new(s, strlen(s), enc)); - th->errinfo = err; - } - else { + if (RTEST(err)) { VALUE str = rb_obj_as_string(err); rb_str_cat2(str, "\n"); - rb_str_cat2(str, s); - th->errinfo = rb_exc_new3(rb_eSyntaxError, str); + rb_str_append(str, mesg); + mesg = str; } + err = rb_exc_new3(rb_eSyntaxError, mesg); + th->errinfo = err; } else { if (!RTEST(err)) { err = rb_exc_new2(rb_eSyntaxError, "compile error"); th->errinfo = err; } - rb_write_error(s); - rb_write_error("\n"); + rb_str_cat2(mesg, "\n"); + rb_io_write(rb_stderr, mesg); } /* returned to the parser world */ @@ -142,62 +126,57 @@ void https://github.com/ruby/ruby/blob/trunk/error.c#L126 rb_compile_error_with_enc(const char *file, int line, void *enc, const char *fmt, ...) { va_list args; - char buf[BUFSIZ]; + VALUE str; va_start(args, fmt); - compile_snprintf(buf, BUFSIZ, file, line, fmt, args); + str = compile_snprintf(enc, NULL, file, line, fmt, args); va_end(args); - compile_err_append(buf, (rb_encoding *)enc); + compile_err_append(str); } void rb_compile_error(const char *file, int line, const char *fmt, ...) { va_list args; - char buf[BUFSIZ]; + VALUE str; va_start(args, fmt); - compile_snprintf(buf, BUFSIZ, file, line, fmt, args); + str = compile_snprintf(NULL, NULL, file, line, fmt, args); va_end(args); - compile_err_append(buf, NULL); + compile_err_append(str); } void rb_compile_error_append(const char *fmt, ...) { va_list args; - char buf[BUFSIZ]; + VALUE str; va_start(args, fmt); - vsnprintf(buf, BUFSIZ, fmt, args); + str = rb_sprintf(fmt, args); va_end(args); - compile_err_append(buf, NULL); + compile_err_append(str); } static void compile_warn_print(const char *file, int line, const char *fmt, va_list args) { - char buf[BUFSIZ]; - int len; + VALUE str; - compile_snprintf(buf, BUFSIZ, file, line, fmt, args); - len = (int)strlen(buf); - buf[len++] = '\n'; - rb_write_error2(buf, len); + str = compile_snprintf(NULL, "warning: ", file, line, fmt, args); + rb_str_cat2(str, "\n"); + rb_io_write(rb_stderr, str); } void rb_compile_warn(const char *file, int line, const char *fmt, ...) { - char buf[BUFSIZ]; va_list args; if (NIL_P(ruby_verbose)) return; - snprintf(buf, BUFSIZ, "warning: %s", fmt); - va_start(args, fmt); - compile_warn_print(file, line, buf, args); + compile_warn_print(file, line, fmt, args); va_end(args); } @@ -205,42 +184,43 @@ rb_compile_warn(const char *file, int li https://github.com/ruby/ruby/blob/trunk/error.c#L184 void rb_compile_warning(const char *file, int line, const char *fmt, ...) { - char buf[BUFSIZ]; va_list args; if (!RTEST(ruby_verbose)) return; - snprintf(buf, BUFSIZ, "warning: %s", fmt); - va_start(args, fmt); - compile_warn_print(file, line, buf, args); + compile_warn_print(file, line, fmt, args); va_end(args); } static void warn_print(const char *fmt, va_list args) { - char buf[BUFSIZ]; - int len; + VALUE str = rb_str_new(0, 0); + VALUE file = rb_sourcefilename(); - err_snprintf(buf, BUFSIZ, fmt, args); - len = (int)strlen(buf); - buf[len++] = '\n'; - rb_write_error2(buf, len); + if (!NIL_P(file)) { + int line = rb_sourceline(); + str = rb_str_append(str, file); + if (line) rb_str_catf(str, ":%d", line); + rb_str_cat2(str, ": "); + } + + rb_str_cat2(str, "warning: "); + rb_str_vcatf(str, fmt, args); + rb_str_cat2(str, "\n"); + rb_io_write(rb_stderr, str); } void rb_warn(const char *fmt, ...) { - char buf[BUFSIZ]; va_list args; if (NIL_P(ruby_verbose)) return; - snprintf(buf, BUFSIZ, "warning: %s", fmt); - va_start(args, fmt); - warn_print(buf, args); + warn_print(fmt, args); va_end(args); } @@ -248,15 +228,12 @@ rb_warn(const char *fmt, ...) https://github.com/ruby/ruby/blob/trunk/error.c#L228 void rb_warning(const char *fmt, ...) { - char buf[BUFSIZ]; va_list args; if (!RTEST(ruby_verbose)) return; - snprintf(buf, BUFSIZ, "warning: %s", fmt); - va_start(args, fmt); - warn_print(buf, args); + warn_print(fmt, args); va_end(args); } -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/