patch for socketmodule

tnb2d@brunelleschi.cs.virginia.edu
Wed, 7 Sep 94 09:46:36 EDT

Some folks here wanted to add non-blocking read and write
functionality to the socketmodule, so we did. It is implemented via a
single method on a socket object called "setblocking". You simply
pass in 1 or 0 (yes or no, true or false-- you get the idea) and until
you set it again all reads and writes will be either blocking or
non-blocking, whichever you chose. The one problem with it is if you
have set a socket to be nonblocking and attempt to read when there is
nothing ready you will get an exception:

socket.error(35, "Operation would block")

so you need to catch it, e.g.,

try:
thisMessage = sock.read(1024)
except:
print "There wasn't anything to read"

SO, here is the diff file from the socketmodule distributed
with 1.0.3 to the socketmodule with setblocking added. Use it in good
health and let me know if this does not work somewhere.

---------%< snip %<------------------%< snip %<---------

55a56
> - s.setblocking(1 | 0) --> None
77a79
> #include <fcntl.h>
483a486
> /* s.setblocking(1 | 0) method */
484a488,509
> static object *
> sock_setblocking(s, args)
> sockobject *s;
> object *args;
> {
> int block;
> if (!getintarg(args, &block))
> return NULL;
> BGN_SAVE
> int delay_flag = fcntl (s->sock_fd, F_GETFL, 0);
> if (block)
> delay_flag &= (~O_NDELAY);
> else
> delay_flag |= O_NDELAY;
> fcntl (s->sock_fd, F_SETFL, delay_flag);
> END_SAVE
>
> INCREF(None);
> return None;
> }
>
>
803a829
> {"setblocking", (method)sock_setblocking},

-------> Tommy.

"I am who I am who I am. Well, who am I?" -- Dave Matthews