From a7a7324ea645b881658fb78d2e7e049f48970afd Mon Sep 17 00:00:00 2001 From: matz Date: Fri, 18 Jan 2002 14:24:01 +0000 Subject: [PATCH] * io.c (rb_io_s_new): block check moved from initialize to this method. * io.c (rb_io_s_open): open should call initialize too. IO#for_fd also calls initialize. [new] * error.c (rb_sys_fail): replace INT2FIX() by INT2NUM() since errno value may not fit in Fixnum size on Hurd. * error.c (set_syserr): ditto. * dir.c (dir_s_glob): returns nil if block given. * io.c (rb_io_each_byte): should return self. * io.c (rb_io_close_m): close check added. * dir.c (dir_seek): should return pos. * parse.y (fixpos): orig may be (NODE*)1, which should not be dereferenced. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@2004 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 30 ++++++++++++++++++++++++ dir.c | 11 +++++---- error.c | 4 ++-- eval.c | 28 +++++++++++------------ ext/socket/socket.c | 56 --------------------------------------------- io.c | 56 +++++++++++++++++++-------------------------- misc/ruby-mode.el | 39 +++++++++++++++++++++++++++++++ parse.y | 1 + 8 files changed, 116 insertions(+), 109 deletions(-) diff --git a/ChangeLog b/ChangeLog index d4a1068902..1a9ea8c0a0 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,7 +1,37 @@ +Fri Jan 18 17:32:09 2002 Yukihiro Matsumoto + + * io.c (rb_io_s_new): block check moved from initialize to this + method. + + * io.c (rb_io_s_open): open should call initialize too. IO#for_fd + also calls initialize. [new] + +Fri Jan 18 10:26:33 2002 Yukihiro Matsumoto + + * error.c (rb_sys_fail): replace INT2FIX() by INT2NUM() since + errno value may not fit in Fixnum size on Hurd. + + * error.c (set_syserr): ditto. + Fri Jan 18 10:12:00 2002 Usaku Nakamura * ext/socket/socket.c (tcp_svr_s_open): fix typo. +Fri Jan 18 02:27:48 2002 Yukihiro Matsumoto + + * dir.c (dir_s_glob): returns nil if block given. + + * io.c (rb_io_each_byte): should return self. + + * io.c (rb_io_close_m): close check added. + + * dir.c (dir_seek): should return pos. + +Fri Jan 18 01:21:53 2002 Yukihiro Matsumoto + + * parse.y (fixpos): orig may be (NODE*)1, which should not be + dereferenced. + Thu Jan 17 16:21:42 2002 Yukihiro Matsumoto * eval.c (block_pass): allow "retry" from within argument passed diff --git a/dir.c b/dir.c index a7673175e5..e95c0cecb9 100644 --- a/dir.c +++ b/dir.c @@ -389,7 +389,7 @@ dir_seek(dir, pos) #ifdef HAVE_SEEKDIR GetDIR(dir, dirp); seekdir(dirp->dir, NUM2INT(pos)); - return dir; + return pos; #else rb_notimplement(); #endif @@ -930,10 +930,13 @@ dir_s_glob(dir, str) } if (buf != buffer) free(buf); - if (ary && RARRAY(ary)->len == 0) { - rb_warning("no matches found: %s", RSTRING(str)->ptr); + if (ary) { + if (RARRAY(ary)->len == 0) { + rb_warning("no matches found: %s", RSTRING(str)->ptr); + } + return ary; } - return ary; + return Qnil; } static VALUE diff --git a/error.c b/error.c index c30374f75d..b6c330748d 100644 --- a/error.c +++ b/error.c @@ -526,7 +526,7 @@ set_syserr(i, name) int ix, offset; #endif VALUE error = rb_define_class_under(rb_mErrno, name, rb_eSystemCallError); - rb_define_const(error, "Errno", INT2FIX(i)); + rb_define_const(error, "Errno", INT2NUM(i)); #ifdef __BEOS__ if (i == B_ERROR) { syserr_error = error; @@ -764,7 +764,7 @@ rb_sys_fail(mesg) } #endif ee = rb_exc_new2(ee, buf); - rb_iv_set(ee, "errno", INT2FIX(n)); + rb_iv_set(ee, "errno", INT2NUM(n)); rb_exc_raise(ee); } diff --git a/eval.c b/eval.c index 069c7be361..ee6ed1ac4c 100644 --- a/eval.c +++ b/eval.c @@ -6672,7 +6672,7 @@ block_pass(self, node) } struct METHOD { - VALUE klass, oklass; + VALUE klass, rklass; VALUE recv; ID id, oid; NODE *body; @@ -6682,7 +6682,7 @@ static void bm_mark(data) struct METHOD *data; { - rb_gc_mark(data->oklass); + rb_gc_mark(data->rklass); rb_gc_mark(data->klass); rb_gc_mark(data->recv); rb_gc_mark((VALUE)data->body); @@ -6697,12 +6697,12 @@ mnew(klass, obj, id, mklass) NODE *body; int noex; struct METHOD *data; - VALUE oklass = klass; + VALUE rklass = klass; ID oid = id; again: if ((body = rb_get_method_body(&klass, &id, &noex)) == 0) { - print_undef(oklass, oid); + print_undef(rklass, oid); } if (nd_type(body) == NODE_ZSUPER) { @@ -6715,7 +6715,7 @@ mnew(klass, obj, id, mklass) data->recv = obj; data->id = id; data->body = body; - data->oklass = oklass; + data->rklass = rklass; data->oid = oid; OBJ_INFECT(method, klass); @@ -6736,7 +6736,7 @@ method_eq(method, other) Data_Get_Struct(method, struct METHOD, m1); Data_Get_Struct(other, struct METHOD, m2); - if (m1->klass != m2->klass || m1->oklass != m2->oklass || + if (m1->klass != m2->klass || m1->rklass != m2->rklass || m1->recv != m2->recv || m1->body != m2->body) return Qfalse; @@ -6756,7 +6756,7 @@ method_unbind(obj) data->recv = 0; data->id = orig->id; data->body = orig->body; - data->oklass = orig->oklass; + data->rklass = orig->rklass; data->oid = orig->oid; OBJ_INFECT(method, obj); @@ -6845,19 +6845,19 @@ umethod_bind(method, recv) struct METHOD *data, *bound; Data_Get_Struct(method, struct METHOD, data); - if (data->oklass != CLASS_OF(recv)) { - if (FL_TEST(data->oklass, FL_SINGLETON)) { + if (data->rklass != CLASS_OF(recv)) { + if (FL_TEST(data->rklass, FL_SINGLETON)) { rb_raise(rb_eTypeError, "singleton method called for a different object"); } if (FL_TEST(CLASS_OF(recv), FL_SINGLETON) && st_lookup(RCLASS(CLASS_OF(recv))->m_tbl, data->oid, 0)) { rb_raise(rb_eTypeError, "method `%s' overridden", rb_id2name(data->oid)); } - if (!((TYPE(data->oklass) == T_MODULE) ? - rb_obj_is_kind_of(recv, data->oklass) : - rb_obj_is_instance_of(recv, data->oklass))) { + if (!((TYPE(data->rklass) == T_MODULE) ? + rb_obj_is_kind_of(recv, data->rklass) : + rb_obj_is_instance_of(recv, data->rklass))) { rb_raise(rb_eTypeError, "bind argument must be an instance of %s", - rb_class2name(data->oklass)); + rb_class2name(data->rklass)); } } @@ -6914,7 +6914,7 @@ method_inspect(method) s = rb_class2name(CLASS_OF(method)); rb_str_buf_cat2(str, s); rb_str_buf_cat2(str, ": "); - s = rb_class2name(data->oklass); + s = rb_class2name(data->rklass); rb_str_buf_cat2(str, s); rb_str_buf_cat2(str, "("); s = rb_class2name(data->klass); diff --git a/ext/socket/socket.c b/ext/socket/socket.c index dcdefd173a..069f47dcd1 100644 --- a/ext/socket/socket.c +++ b/ext/socket/socket.c @@ -909,15 +909,6 @@ tcp_init(argc, argv, sock) local_host, local_serv, INET_CLIENT); } -static VALUE -tcp_s_open(argc, argv, klass) - int argc; - VALUE *argv; - VALUE klass; -{ - return tcp_init(argc, argv, rb_obj_alloc(klass)); -} - #ifdef SOCKS static VALUE socks_init(sock, host, serv) @@ -933,13 +924,6 @@ socks_init(sock, host, serv) return init_inetsock(class, host, serv, Qnil, Qnil, INET_SOCKS); } -static VALUE -socks_s_open(klass, host, serv) - VALUE klass, host, serv; -{ - return socks_init(rb_obj_alloc(klass), host, serv); -} - #ifdef SOCKS5 static VALUE socks_s_close(sock) @@ -1085,15 +1069,6 @@ tcp_svr_init(argc, argv, sock) return init_inetsock(sock, Qnil, arg1, NULL, Qnil, INET_SERVER); } -static VALUE -tcp_svr_s_open(argc, argv, klass) - int argc; - VALUE *argv; - VALUE klass; -{ - return tcp_svr_init(argc, argv, rb_obj_alloc(klass)); -} - static VALUE s_accept(klass, fd, sockaddr, len) VALUE klass; @@ -1264,15 +1239,6 @@ udp_init(argc, argv, sock) return init_sock(sock, fd); } -static VALUE -udp_s_open(argc, argv, klass) - int argc; - VALUE *argv; - VALUE klass; -{ - return udp_init(argc, argv, rb_obj_alloc(klass)); -} - static VALUE udp_connect(sock, host, port) VALUE sock, host, port; @@ -1361,13 +1327,6 @@ udp_send(argc, argv, sock) } #ifdef HAVE_SYS_UN_H -static VALUE -unix_s_sock_open(klass, path) - VALUE klass, path; -{ - return init_unixsock(rb_obj_alloc(klass), path, 0); -} - static VALUE unix_init(sock, path) VALUE sock, path; @@ -1574,13 +1533,6 @@ sock_init(sock, domain, type, protocol) return init_sock(sock, fd); } -static VALUE -sock_s_open(klass, domain, type, protocol) - VALUE klass, domain, type, protocol; -{ - return init_sock(rb_obj_alloc(klass), domain, type, protocol); -} - static VALUE sock_s_for_fd(klass, fd) VALUE klass, fd; @@ -2164,7 +2116,6 @@ Init_socket() rb_eSocket = rb_define_class("SocketError", rb_eStandardError); rb_cBasicSocket = rb_define_class("BasicSocket", rb_cIO); - rb_undef_method(CLASS_OF(rb_cBasicSocket), "open"); rb_undef_method(rb_cBasicSocket, "initialize"); rb_define_singleton_method(rb_cBasicSocket, "do_not_reverse_lookup", @@ -2191,14 +2142,12 @@ Init_socket() rb_cTCPSocket = rb_define_class("TCPSocket", rb_cIPSocket); rb_define_global_const("TCPsocket", rb_cTCPSocket); - rb_define_singleton_method(rb_cTCPSocket, "open", tcp_s_open, -1); rb_define_singleton_method(rb_cTCPSocket, "gethostbyname", tcp_s_gethostbyname, 1); rb_define_method(rb_cTCPSocket, "initialize", tcp_init, -1); #ifdef SOCKS rb_cSOCKSSocket = rb_define_class("SOCKSSocket", rb_cTCPSocket); rb_define_global_const("SOCKSsocket", rb_cSOCKSSocket); - rb_define_singleton_method(rb_cSOCKSSocket, "open", socks_s_open, 2); rb_define_method(rb_cSOCKSSocket, "initialize", socks_init, 2); #ifdef SOCKS5 rb_define_method(rb_cSOCKSSocket, "close", socks_s_close, 0); @@ -2207,14 +2156,12 @@ Init_socket() rb_cTCPServer = rb_define_class("TCPServer", rb_cTCPSocket); rb_define_global_const("TCPserver", rb_cTCPServer); - rb_define_singleton_method(rb_cTCPServer, "open", tcp_svr_s_open, -1); rb_define_method(rb_cTCPServer, "accept", tcp_accept, 0); rb_define_method(rb_cTCPServer, "initialize", tcp_svr_init, -1); rb_define_method(rb_cTCPServer, "listen", sock_listen, 1); rb_cUDPSocket = rb_define_class("UDPSocket", rb_cIPSocket); rb_define_global_const("UDPsocket", rb_cUDPSocket); - rb_define_singleton_method(rb_cUDPSocket, "open", udp_s_open, -1); rb_define_method(rb_cUDPSocket, "initialize", udp_init, -1); rb_define_method(rb_cUDPSocket, "connect", udp_connect, 2); rb_define_method(rb_cUDPSocket, "bind", udp_bind, 2); @@ -2223,7 +2170,6 @@ Init_socket() #ifdef HAVE_SYS_UN_H rb_cUNIXSocket = rb_define_class("UNIXSocket", rb_cBasicSocket); rb_define_global_const("UNIXsocket", rb_cUNIXSocket); - rb_define_singleton_method(rb_cUNIXSocket, "open", unix_s_sock_open, 1); rb_define_method(rb_cUNIXSocket, "initialize", unix_init, 1); rb_define_method(rb_cUNIXSocket, "path", unix_path, 0); rb_define_method(rb_cUNIXSocket, "addr", unix_addr, 0); @@ -2232,14 +2178,12 @@ Init_socket() rb_cUNIXServer = rb_define_class("UNIXServer", rb_cUNIXSocket); rb_define_global_const("UNIXserver", rb_cUNIXServer); - rb_define_singleton_method(rb_cUNIXServer, "open", unix_svr_s_open, 1); rb_define_method(rb_cUNIXServer, "initialize", unix_svr_init, 1); rb_define_method(rb_cUNIXServer, "accept", unix_accept, 0); rb_define_method(rb_cUNIXServer, "listen", sock_listen, 1); #endif rb_cSocket = rb_define_class("Socket", rb_cBasicSocket); - rb_define_singleton_method(rb_cSocket, "open", sock_s_open, 3); rb_define_singleton_method(rb_cSocket, "for_fd", sock_s_for_fd, 1); rb_define_method(rb_cSocket, "initialize", sock_init, 3); diff --git a/io.c b/io.c index 4e3101686e..54438b4f87 100644 --- a/io.c +++ b/io.c @@ -963,7 +963,7 @@ rb_io_each_byte(io) rb_yield(INT2FIX(c & 0xff)); } if (ferror(f)) rb_sys_fail(fptr->path); - return Qnil; + return io; } VALUE @@ -1127,6 +1127,7 @@ rb_io_close_m(io) if (rb_safe_level() >= 4 && !OBJ_TAINTED(io)) { rb_raise(rb_eSecurityError, "Insecure: can't close"); } + rb_io_check_closed(RFILE(io)->fptr); rb_io_close(io); return Qnil; } @@ -1665,7 +1666,7 @@ pipe_open(pname, mode) else fptr->f = f; rb_io_synchronized(fptr); } - return (VALUE)port; + return port; } #else int pid, pr[2], pw[2]; @@ -1748,7 +1749,7 @@ pipe_open(pname, mode) fptr->finalize = pipe_finalize; pipe_add_fptr(fptr); #endif - return (VALUE)port; + return port; } } #endif @@ -1835,20 +1836,18 @@ rb_open_file(argc, argv, io) } static VALUE -rb_file_s_open(argc, argv, klass) +rb_io_s_open(argc, argv, klass) int argc; VALUE *argv; VALUE klass; { - VALUE io = rb_obj_alloc(klass); + VALUE io = rb_class_new_instance(argc, argv, klass); - RFILE(io)->fptr = 0; - rb_open_file(argc, argv, (VALUE)io); if (rb_block_given_p()) { - return rb_ensure(rb_yield, (VALUE)io, rb_io_close, (VALUE)io); + return rb_ensure(rb_yield, io, rb_io_close, io); } - return (VALUE)io; + return io; } static VALUE @@ -1863,7 +1862,7 @@ rb_f_open(argc, argv) return rb_io_popen(str+1, argc, argv, rb_cIO); } } - return rb_file_s_open(argc, argv, rb_cFile); + return rb_io_s_open(argc, argv, rb_cFile); } static VALUE @@ -2083,10 +2082,10 @@ rb_io_clone(io) fptr->f = rb_fdopen(fd, "w"); } if (fptr->mode & FMODE_BINMODE) { - rb_io_binmode((VALUE)clone); + rb_io_binmode(clone); } - return (VALUE)clone; + return clone; } static VALUE @@ -2413,7 +2412,7 @@ prep_stdio(f, mode, klass) fp->f = f; fp->mode = mode; - return (VALUE)io; + return io; } static void @@ -2473,34 +2472,23 @@ rb_file_initialize(argc, argv, io) RFILE(io)->fptr = 0; } rb_open_file(argc, argv, io); - if (rb_block_given_p()) { - rb_warn("File::new() does not take block; use File::open() instead"); - } return io; } static VALUE -rb_io_s_for_fd(argc, argv, klass) +rb_io_s_new(argc, argv, klass) int argc; VALUE *argv; VALUE klass; { - VALUE fnum, mode; - OpenFile *fp; - char *m = "r"; - VALUE io = rb_obj_alloc(klass); + if (rb_block_given_p()) { + char *cname = rb_class2name(klass); - if (rb_scan_args(argc, argv, "11", &fnum, &mode) == 2) { - SafeStringValue(mode); - m = RSTRING(mode)->ptr; + rb_warn("%s::new() does not take block; use %::open() instead", + cname, cname); } - MakeOpenFile(io, fp); - - fp->f = rb_fdopen(NUM2INT(fnum), m); - fp->mode = rb_io_mode_flags(m); - - return io; + return rb_class_new_instance(argc, argv, klass); } static int binmode = 0; @@ -3524,8 +3512,9 @@ Init_IO() rb_include_module(rb_cIO, rb_mEnumerable); rb_define_singleton_method(rb_cIO, "allocate", rb_io_s_alloc, 0); - rb_define_singleton_method(rb_cIO, "for_fd", rb_io_s_for_fd, -1); - rb_define_method(rb_cIO, "initialize", rb_io_initialize, -1); + rb_define_singleton_method(rb_cIO, "new", rb_io_s_new, -1); + rb_define_singleton_method(rb_cIO, "open", rb_io_s_open, -1); + rb_define_singleton_method(rb_cIO, "for_fd", rb_class_new_instance, -1); rb_define_singleton_method(rb_cIO, "popen", rb_io_s_popen, -1); rb_define_singleton_method(rb_cIO, "foreach", rb_io_s_foreach, -1); rb_define_singleton_method(rb_cIO, "readlines", rb_io_s_readlines, -1); @@ -3533,6 +3522,8 @@ Init_IO() rb_define_singleton_method(rb_cIO, "select", rb_f_select, -1); rb_define_singleton_method(rb_cIO, "pipe", rb_io_s_pipe, 0); + rb_define_method(rb_cIO, "initialize", rb_io_initialize, -1); + rb_output_fs = Qnil; rb_define_hooked_variable("$,", &rb_output_fs, 0, rb_str_setter); @@ -3673,7 +3664,6 @@ Init_IO() Init_File(); - rb_define_singleton_method(rb_cFile, "open", rb_file_s_open, -1); rb_define_method(rb_cFile, "initialize", rb_file_initialize, -1); rb_file_const("RDONLY", INT2FIX(O_RDONLY)); diff --git a/misc/ruby-mode.el b/misc/ruby-mode.el index c365c20a3b..56cb1ed2aa 100644 --- a/misc/ruby-mode.el +++ b/misc/ruby-mode.el @@ -189,6 +189,9 @@ The variable ruby-indent-level controls the amount of indentation. (make-local-variable 'imenu-create-index-function) (setq imenu-create-index-function 'ruby-imenu-create-index) + (make-local-variable 'add-log-current-defun-function) + (setq add-log-current-defun-function 'ruby-add-log-current-method) + (run-hooks 'ruby-mode-hook)) (defun ruby-current-indentation () @@ -682,6 +685,42 @@ An end of a defun is found by moving forward from the beginning of one." (ruby-beginning-of-defun) (re-search-backward "^\n" (- (point) 1) t)) +(defun ruby-add-log-current-method () + "Return current method string." + (condition-case nil + (save-excursion + (let ((mlist nil) (indent 0)) + ;; get current method (or class/module) + (if (re-search-backward + (concat "^[ \t]*\\(def\\|class\\|module\\)[ \t]+" + "\\(" ruby-symbol-re "+\\)") + nil t) + (progn + (setq mlist (list (buffer-substring + (match-beginning 2) (match-end 2)))) + (goto-char (match-beginning 1)) + (setq indent (current-column)) + (beginning-of-line))) + ;; nest class/module + (while (and (> indent 0) + (re-search-backward + (concat + "^[ \t]*\\(class\\|module\\)[ \t]+" + "\\([A-Z]" ruby-symbol-re "+\\)") + nil t)) + (goto-char (match-beginning 1)) + (if (< (current-column) indent) + (progn + (setq mlist + (cons (buffer-substring + (match-beginning 2) (match-end 2)) mlist)) + (setq indent (current-column)) + (beginning-of-line)))) + ;; generate string + (if (consp mlist) + (mapconcat (function identity) mlist "::") + nil))))) + (cond ((featurep 'font-lock) (or (boundp 'font-lock-variable-name-face) diff --git a/parse.y b/parse.y index 1b077cb152..00aa32f884 100644 --- a/parse.y +++ b/parse.y @@ -4126,6 +4126,7 @@ fixpos(node, orig) { if (!node) return; if (!orig) return; + if (orig == (NODE*)1) return; node->nd_file = orig->nd_file; nd_set_line(node, nd_line(orig)); }