"""Communication link driver."""

import sys
import selectors


CLIENT_TO_SERVER = object()
SERVER_TO_CLIENT = object()


def talk(socket, source=sys.stdin.buffer, sink=sys.stdout.buffer):
    """Run communication, in a loop. Input from `source` is sent on `socket`,
    and data received on `socket` is forwarded to `sink`.

    All file descriptors must be non-blocking.
    """

    OUTPUT_BUFFER_SIZE = 1024

    with selectors.DefaultSelector() as selector:
        selector.register(source, selectors.EVENT_READ, CLIENT_TO_SERVER)
        selector.register(socket, selectors.EVENT_READ, SERVER_TO_CLIENT)
        while True:
            for key, events in selector.select():
                if key.data is CLIENT_TO_SERVER:
                    data = source.readline()
                    socket.send(data)
                elif key.data is SERVER_TO_CLIENT:
                    data = socket.recv(OUTPUT_BUFFER_SIZE)
                    sink.write(data)
                    sink.flush()

"""Communication link driver."""

import sys
import select


def talk(socket, source=sys.stdin.buffer, sink=sys.stdout.buffer):
    """Run communication, in a loop. Input from `source` is sent on `socket`,
    and data received on `socket` is forwarded to `sink`.

    All file descriptors must be non-blocking.
    """

    OUTPUT_BUFFER_SIZE = 1024

    while True:
        readable, writable, exceptional = select.select((socket, source),
                                                        (),
                                                        (socket, source, sink))
        if source in readable:
            socket.send(source.readline())
        if socket in readable:
            sink.write(socket.recv(OUTPUT_BUFFER_SIZE))
            sink.flush()

Switch to just using `select` directly
Switch to just using `select` directly

This is less efficient, but does let use get the "exceptional" cases
and handle them more pleasantly.

  """Communication link driver."""
  
  import sys
- import selectors
+ import select
- 
- 
- CLIENT_TO_SERVER = object()
- SERVER_TO_CLIENT = object()
  
  
  def talk(socket, source=sys.stdin.buffer, sink=sys.stdout.buffer):
      """Run communication, in a loop. Input from `source` is sent on `socket`,
      and data received on `socket` is forwarded to `sink`.
  
      All file descriptors must be non-blocking.
      """
  
      OUTPUT_BUFFER_SIZE = 1024
  
-     with selectors.DefaultSelector() as selector:
-         selector.register(source, selectors.EVENT_READ, CLIENT_TO_SERVER)
-         selector.register(socket, selectors.EVENT_READ, SERVER_TO_CLIENT)
-         while True:
+     while True:
-             for key, events in selector.select():
-                 if key.data is CLIENT_TO_SERVER:
-                     data = source.readline()
-                     socket.send(data)
-                 elif key.data is SERVER_TO_CLIENT:
+         readable, writable, exceptional = select.select((socket, source),
+                                                         (),
+                                                         (socket, source, sink))
+         if source in readable:
+             socket.send(source.readline())
+         if socket in readable:
-                     data = socket.recv(OUTPUT_BUFFER_SIZE)
+             sink.write(socket.recv(OUTPUT_BUFFER_SIZE))
-                     sink.write(data)
-                     sink.flush()
+             sink.flush()
  
Switch to just using `select` directly
## Code Before:
"""Communication link driver."""

import sys
import selectors


CLIENT_TO_SERVER = object()
SERVER_TO_CLIENT = object()


def talk(socket, source=sys.stdin.buffer, sink=sys.stdout.buffer):
    """Run communication, in a loop. Input from `source` is sent on `socket`,
    and data received on `socket` is forwarded to `sink`.

    All file descriptors must be non-blocking.
    """

    OUTPUT_BUFFER_SIZE = 1024

    with selectors.DefaultSelector() as selector:
        selector.register(source, selectors.EVENT_READ, CLIENT_TO_SERVER)
        selector.register(socket, selectors.EVENT_READ, SERVER_TO_CLIENT)
        while True:
            for key, events in selector.select():
                if key.data is CLIENT_TO_SERVER:
                    data = source.readline()
                    socket.send(data)
                elif key.data is SERVER_TO_CLIENT:
                    data = socket.recv(OUTPUT_BUFFER_SIZE)
                    sink.write(data)
                    sink.flush()

## Instruction:
Switch to just using `select` directly
## Code After:
"""Communication link driver."""

import sys
import select


def talk(socket, source=sys.stdin.buffer, sink=sys.stdout.buffer):
    """Run communication, in a loop. Input from `source` is sent on `socket`,
    and data received on `socket` is forwarded to `sink`.

    All file descriptors must be non-blocking.
    """

    OUTPUT_BUFFER_SIZE = 1024

    while True:
        readable, writable, exceptional = select.select((socket, source),
                                                        (),
                                                        (socket, source, sink))
        if source in readable:
            socket.send(source.readline())
        if socket in readable:
            sink.write(socket.recv(OUTPUT_BUFFER_SIZE))
            sink.flush()
