def write(self, b):
        if self.closed:
            raise ValueError("write to closed file")
        if isinstance(b, unicode):
            raise TypeError("can't write unicode to binary stream")
        with self._write_lock:
            # XXX we can implement some more tricks to try and avoid
            # partial writes
            if len(self._write_buf) > self.buffer_size:
                # We're full, so let's pre-flush the buffer
                try:
                    self._flush_unlocked()
                except BlockingIOError as e:
                    # We can't accept anything else.
                    # XXX Why not just let the exception pass through?
                    raise BlockingIOError(e.errno, e.strerror, 0)
            before = len(self._write_buf)
            self._write_buf.extend(b)
            written = len(self._write_buf) - before
            if len(self._write_buf) > self.buffer_size:
                try:
                    self._flush_unlocked()
                except BlockingIOError as e:
                    if len(self._write_buf) > self.max_buffer_size:
                        # We've hit max_buffer_size. We have to accept a
                        # partial write and cut back our buffer.
                        overage = len(self._write_buf) - self.max_buffer_size
                        self._write_buf = self._write_buf[:self.max_buffer_size]
                        raise BlockingIOError(e.errno, e.strerror, overage)
            return written
dataset/ETHPy150Open deanhiller/databus/webapp/play1.3.x/python/Lib/io.py/BufferedWriter.write/original