def write(self, data):
if isinstance(data, text_type):<EOL><INDENT>data = data.encode("<STR_LIT:utf-8>")<EOL><DEDENT>if self._closed:<EOL><INDENT>raise IOError("<STR_LIT>")<EOL><DEDENT>if not (self._flags & self.FLAG_WRITE):<EOL><INDENT>raise IOError("<STR_LIT>")<EOL><DEDENT>if not (self._flags & self.FLAG_BUFFERED):<EOL><INDENT>self._write_all(data)<EOL>return<EOL><DEDENT>self._wbuffer.write(data)<EOL>if self._flags & self.FLAG_LINE_BUFFERED:<EOL><INDENT>last_newline_pos = data.rfind(linefeed_byte)<EOL>if last_newline_pos >= <NUM_LIT:0>:<EOL><INDENT>wbuf = self._wbuffer.getvalue()<EOL>last_newline_pos += len(wbuf) - len(data)<EOL>self._write_all(wbuf[: last_newline_pos + <NUM_LIT:1>])<EOL>self._wbuffer = BytesIO()<EOL>self._wbuffer.write(wbuf[last_newline_pos + <NUM_LIT:1> :])<EOL><DEDENT>return<EOL><DEDENT>if self._wbuffer.tell() >= self._bufsize:<EOL><INDENT>self.flush()<EOL><DEDENT>return<EOL>
Write data to the file.  If write buffering is on (``bufsize`` was
specified and non-zero), some or all of the data may not actually be
written yet.  (Use `flush` or `close` to force buffered data to be
written out.)

:param data: ``str``/``bytes`` data to write