def test_fileDescriptorsReleasedOnFailure(self):
        """
        L{_ExhaustsFileDescriptors.exhaust} closes any opened file
        descriptors if an exception occurs during its exhaustion loop.
        """
        fileDescriptors = []

        def failsAfterThree():
            if len(fileDescriptors) == 3:
                raise ValueError(
                    "test_fileDescriptorsReleasedOnFailure"
                    " fake open exception")
            else:
                fd = os.dup(0)
                fileDescriptors.append(fd)
                return fd

        exhauster = _ExhaustsFileDescriptors(failsAfterThree)
        self.addCleanup(exhauster.release)

        self.assertRaises(ValueError, exhauster.exhaust)
        self.assertEqual(len(fileDescriptors), 3)
        self.assertEqual(exhauster.count(), 0)

        for fd in fileDescriptors:
            exception = self.assertRaises(OSError, os.fstat, fd)
            self.assertEqual(exception.errno, errno.EBADF)

        L{_ExhaustsFileDescriptors.exhaust} closes any opened file
        descriptors if an exception occurs during its exhaustion loop.
        
stackoverflow/venv/lib/python3.6/site-packages/twisted/internet/test/test_tcp.py