'Check that a buffered write, when it gets interrupted (either
returning a partial result or EINTR), properly invokes the signal
handler and retries if the latter returned successfully.'

@unittest.skipUnless(threading, u'Threading    required    for    this    test.')
def check_interrupted_write_retry(self, item, **fdopen_kwargs):

    select = support.import_module(u'select')
    N = (1024 * 1024)
    (r, w) = os.pipe()
    fdopen_kwargs[u'closefd'] = False
    read_results = []
    write_finished = False
    def _read():
        while (not write_finished):
            while (r in select.select([r], [], [], 1.0)[0]):
                s = os.read(r, 1024)
                read_results.append(s)
    t = threading.Thread(target=_read)
    t.daemon = True
    def alarm1(sig, frame):
        signal.signal(signal.SIGALRM, alarm2)
        signal.alarm(1)
    def alarm2(sig, frame):
        t.start()
    signal.signal(signal.SIGALRM, alarm1)
    try:
        wio = self.io.open(w, **fdopen_kwargs)
        signal.alarm(1)
        self.assertEqual(N, wio.write((item * N)))
        wio.flush()
        write_finished = True
        t.join()
        self.assertEqual(N, sum((len(x) for x in read_results)))
    finally:
        write_finished = True
        os.close(w)
        os.close(r)
        try:
            wio.close()
        except IOError as e:
            if (e.errno != errno.EBADF):
                raise
