dumping ground for random patches and texts
 help / color / mirror / Atom feed
From: Eric Wong <e@80x24.org>
To: spew@80x24.org
Subject: [WIP] use rb_get_kwargs for exception:false
Date: Thu, 12 Mar 2015 03:00:16 +0000	[thread overview]
Message-ID: <20150312030016.GA10183@dcvr.yhbt.net> (raw)

diff --git a/ext/openssl/ossl_ssl.c b/ext/openssl/ossl_ssl.c
index 48e9730..adf381c 100644
--- a/ext/openssl/ossl_ssl.c
+++ b/ext/openssl/ossl_ssl.c
@@ -107,7 +107,8 @@ static const char *ossl_ssl_attrs[] = {
 
 ID ID_callback_state;
 
-static VALUE sym_exception;
+static ID id_exception;
+static VALUE sym_wait_readable, sym_wait_writable;
 
 /*
  * SSLContext class
@@ -1272,7 +1273,7 @@ read_would_block(int nonblock)
 
 static VALUE
 ossl_start_ssl(VALUE self, int (*func)(), const char *funcname,
-		int nonblock, int no_exception)
+		int nonblock, VALUE except)
 {
     SSL *ssl;
     rb_io_t *fptr;
@@ -1296,12 +1297,12 @@ ossl_start_ssl(VALUE self, int (*func)(), const char *funcname,
 
 	switch((ret2 = ssl_get_error(ssl, ret))){
 	case SSL_ERROR_WANT_WRITE:
-            if (no_exception) { return ID2SYM(rb_intern("wait_writable")); }
+            if (except == Qfalse) { return sym_wait_writable; }
             write_would_block(nonblock);
             rb_io_wait_writable(FPTR_TO_FD(fptr));
             continue;
 	case SSL_ERROR_WANT_READ:
-            if (no_exception) { return ID2SYM(rb_intern("wait_readable")); }
+            if (except == Qfalse) { return sym_wait_readable; }
             read_would_block(nonblock);
             rb_io_wait_readable(FPTR_TO_FD(fptr));
             continue;
@@ -1327,7 +1328,7 @@ static VALUE
 ossl_ssl_connect(VALUE self)
 {
     ossl_ssl_setup(self);
-    return ossl_start_ssl(self, SSL_connect, "SSL_connect", 0, 0);
+    return ossl_start_ssl(self, SSL_connect, "SSL_connect", 0, Qtrue);
 }
 
 /*
@@ -1352,7 +1353,7 @@ static VALUE
 ossl_ssl_connect_nonblock(VALUE self)
 {
     ossl_ssl_setup(self);
-    return ossl_start_ssl(self, SSL_connect, "SSL_connect", 1, 0);
+    return ossl_start_ssl(self, SSL_connect, "SSL_connect", 1, Qtrue);
 }
 
 /*
@@ -1366,7 +1367,7 @@ static VALUE
 ossl_ssl_accept(VALUE self)
 {
     ossl_ssl_setup(self);
-    return ossl_start_ssl(self, SSL_accept, "SSL_accept", 0, 0);
+    return ossl_start_ssl(self, SSL_accept, "SSL_accept", 0, Qtrue);
 }
 
 /*
@@ -1394,16 +1395,14 @@ ossl_ssl_accept(VALUE self)
 static VALUE
 ossl_ssl_accept_nonblock(int argc, VALUE *argv, VALUE self)
 {
-    int no_exception = 0;
     VALUE opts = Qnil;
+    VALUE except = Qtrue;
 
     rb_scan_args(argc, argv, "0:", &opts);
-
-    if (!NIL_P(opts) && Qfalse == rb_hash_aref(opts, sym_exception))
-	no_exception = 1;
+    rb_get_kwargs(opts, &id_exception, 0, 1, &except);
 
     ossl_ssl_setup(self);
-    return ossl_start_ssl(self, SSL_accept, "SSL_accept", 1, no_exception);
+    return ossl_start_ssl(self, SSL_accept, "SSL_accept", 1, except);
 }
 
 static VALUE
@@ -1418,8 +1417,22 @@ ossl_ssl_read_internal(int argc, VALUE *argv, VALUE self, int nonblock)
 
     rb_scan_args(argc, argv, "11:", &len, &str, &opts);
 
-    if (!NIL_P(opts) && Qfalse == rb_hash_aref(opts, sym_exception))
-	no_exception = 1;
+    if (1) {
+      /* FIXME:
+       * Work-in-progress new code, possible bug in rb_get_kwargs ?
+       * I'm not sure why rb_get_kwargs(extract_kwarg macro) calls
+       * st_delete, maybe r44077 interacts badly with kwargs
+       * optimizations?
+       */
+      VALUE except = Qtrue;
+
+      rb_get_kwargs(opts, &id_exception, 0, 1, &except);
+      if (except == Qfalse)
+           no_exception = 1;
+    } else {
+      if (!NIL_P(opts) && Qfalse == rb_hash_aref(opts, ID2SYM(id_exception)))
+          no_exception = 1;
+    }
 
     ilen = NUM2INT(len);
     if(NIL_P(str)) str = rb_str_new(0, ilen);
@@ -1515,7 +1528,7 @@ ossl_ssl_read_nonblock(int argc, VALUE *argv, VALUE self)
 }
 
 static VALUE
-ossl_ssl_write_internal(VALUE self, VALUE str, int nonblock, int no_exception)
+ossl_ssl_write_internal(VALUE self, VALUE str, int nonblock, VALUE except)
 {
     SSL *ssl;
     int nwrite = 0;
@@ -1532,12 +1545,12 @@ ossl_ssl_write_internal(VALUE self, VALUE str, int nonblock, int no_exception)
 	    case SSL_ERROR_NONE:
 		goto end;
 	    case SSL_ERROR_WANT_WRITE:
-		if (no_exception) { return ID2SYM(rb_intern("wait_writable")); }
+		if (except == Qfalse) { return sym_wait_writable; }
                 write_would_block(nonblock);
                 rb_io_wait_writable(FPTR_TO_FD(fptr));
                 continue;
 	    case SSL_ERROR_WANT_READ:
-		if (no_exception) { return ID2SYM(rb_intern("wait_readable")); }
+		if (except == Qfalse) { return sym_wait_readable; }
                 read_would_block(nonblock);
                 rb_io_wait_readable(FPTR_TO_FD(fptr));
                 continue;
@@ -1567,7 +1580,7 @@ ossl_ssl_write_internal(VALUE self, VALUE str, int nonblock, int no_exception)
 static VALUE
 ossl_ssl_write(VALUE self, VALUE str)
 {
-    return ossl_ssl_write_internal(self, str, 0, 0);
+    return ossl_ssl_write_internal(self, str, 0, Qtrue);
 }
 
 /*
@@ -1582,14 +1595,12 @@ ossl_ssl_write_nonblock(int argc, VALUE *argv, VALUE self)
 {
     VALUE str;
     VALUE opts = Qnil;
-    int no_exception = 0;
+    VALUE except = Qtrue;
 
     rb_scan_args(argc, argv, "1:", &str, &opts);
+    rb_get_kwargs(opts, &id_exception, 0, 1, &except);
 
-    if (!NIL_P(opts) && Qfalse == rb_hash_aref(opts, sym_exception))
-	no_exception = 1;
-
-    return ossl_ssl_write_internal(self, str, 1, no_exception);
+    return ossl_ssl_write_internal(self, str, 1, except);
 }
 
 /*
@@ -2313,5 +2324,8 @@ Init_ossl_ssl(void)
     ossl_ssl_def_const(OP_NETSCAPE_CA_DN_BUG);
     ossl_ssl_def_const(OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG);
 
-    sym_exception = ID2SYM(rb_intern("exception"));
+#undef rb_intern
+    id_exception = rb_intern("exception");
+    sym_wait_readable = ID2SYM(rb_intern("wait_readable"));
+    sym_wait_writable = ID2SYM(rb_intern("wait_writable"));
 }
diff --git a/ext/socket/init.c b/ext/socket/init.c
index b94b250..495f08c 100644
--- a/ext/socket/init.c
+++ b/ext/socket/init.c
@@ -29,7 +29,8 @@ VALUE rb_cSOCKSSocket;
 #endif
 
 int rsock_do_not_reverse_lookup = 1;
-static VALUE sym_exception, sym_wait_readable;
+static ID id_exception;
+static VALUE sym_wait_readable;
 
 void
 rsock_raise_socket_error(const char *reason, int error)
@@ -511,13 +512,11 @@ rsock_s_accept_nonblock(int argc, VALUE *argv, VALUE klass, rb_io_t *fptr,
 			struct sockaddr *sockaddr, socklen_t *len)
 {
     int fd2;
-    int ex = 1;
     VALUE opts = Qnil;
+    VALUE except = Qtrue;
 
     rb_scan_args(argc, argv, "0:", &opts);
-
-    if (!NIL_P(opts) && Qfalse == rb_hash_aref(opts, sym_exception))
-	ex = 0;
+    rb_get_kwargs(opts, &id_exception, 0, 1, &except);
 
     rb_secure(3);
     rb_io_set_nonblock(fptr);
@@ -532,7 +531,7 @@ rsock_s_accept_nonblock(int argc, VALUE *argv, VALUE klass, rb_io_t *fptr,
 #if defined EPROTO
 	  case EPROTO:
 #endif
-            if (!ex)
+            if (except == Qfalse)
 		return sym_wait_readable;
             rb_readwrite_sys_fail(RB_IO_WAIT_READABLE, "accept(2) would block");
 	}
@@ -624,6 +623,6 @@ rsock_init_socket_init(void)
     rsock_init_socket_constants();
 
 #undef rb_intern
-    sym_exception = ID2SYM(rb_intern("exception"));
+    id_exception = rb_intern("exception");
     sym_wait_readable = ID2SYM(rb_intern("wait_readable"));
 }

                 reply	other threads:[~2015-03-12  3:00 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20150312030016.GA10183@dcvr.yhbt.net \
    --to=e@80x24.org \
    --cc=spew@80x24.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).