(instream, outstream)
Copy data from one stream to another.

    :type instream: :class:`io.BytesIO` or :class:`io.StringIO` or file
    :param instream: A byte stream or open file to read from.
    :param file outstream: The file descriptor of a tmpfile to write to.
Copy data from one stream to another.
Copy data from one stream to another .
def _copy_data(instream, outstream):
    """Copy data from one stream to another.

    :type instream: :class:`io.BytesIO` or :class:`io.StringIO` or file
    :param instream: A byte stream or open file to read from.
    :param file outstream: The file descriptor of a tmpfile to write to.
    """
    sent = 0

    while True:
        if ((_py3k and isinstance(instream, str)) or
            (not _py3k and isinstance(instream, basestring))):
            data = instream[:1024]
            instream = instream[1024:]
        else:
            data = instream.read(1024)
        if len(data) == 0:
            break

        sent += len(data)
        if ((_py3k and isinstance(data, str)) or
            (not _py3k and isinstance(data, basestring))):
            encoded = binary(data)
        else:
            encoded = data
        log.debug("Sending %d bytes of data..." % sent)
        log.debug("Encoded data (type %s):\n%s" % (type(encoded), encoded))

        if not _py3k:
            try:
                outstream.write(encoded)
            except IOError as ioe:
                # Can get 'broken pipe' errors even when all data was sent
                if 'Broken pipe' in str(ioe):
                    log.error('Error sending data: Broken pipe')
                else:
                    log.exception(ioe)
                break
            else:
                log.debug("Wrote data type <type 'str'> to outstream.")
        else:
            try:
                outstream.write(bytes(encoded))
            except TypeError as te:
                # XXX FIXME This appears to happen because
                # _threaded_copy_data() sometimes passes the `outstream` as an
                # object with type <_io.BufferredWriter> and at other times
                # with type <encodings.utf_8.StreamWriter>.  We hit the
                # following error when the `outstream` has type
                # <encodings.utf_8.StreamWriter>.
                if not "convert 'bytes' object to str implicitly" in str(te):
                    log.error(str(te))
                try:
                    outstream.write(encoded.decode())
                except TypeError as yate:
                    # We hit the "'str' does not support the buffer interface"
                    # error in Python3 when the `outstream` is an io.BytesIO and
                    # we try to write a str to it.  We don't care about that
                    # error, we'll just try again with bytes.
                    if not "does not support the buffer interface" in str(yate):
                        log.error(str(yate))
                except IOError as ioe:
                    # Can get 'broken pipe' errors even when all data was sent
                    if 'Broken pipe' in str(ioe):
                        log.error('Error sending data: Broken pipe')
                    else:
                        log.exception(ioe)
                    break
                else:
                    log.debug("Wrote data type <class 'str'> outstream.")
            except IOError as ioe:
                # Can get 'broken pipe' errors even when all data was sent
                if 'Broken pipe' in str(ioe):
                    log.error('Error sending data: Broken pipe')
                else:
                    log.exception(ioe)
                break
            else:
                log.debug("Wrote data type <class 'bytes'> to outstream.")

    try:
        outstream.close()
    except IOError as ioe:
        log.error("Unable to close outstream %s:\r\t%s" % (outstream, ioe))
    else:
        log.debug("Closed outstream: %d bytes sent." % sent)
def _copy_data ( instream , outstream ) : sent = 0 while True : if ( ( _py3k and isinstance ( instream , str ) ) or ( not _py3k and isinstance ( instream , basestring ) ) ) : data = instream [ : 1024 ] instream = instream [ 1024 : ] else : data = instream . read ( 1024 ) if len ( data ) == 0 : break sent += len ( data ) if ( ( _py3k and isinstance ( data , str ) ) or ( not _py3k and isinstance ( data , basestring ) ) ) : encoded = binary ( data ) else : encoded = data log . debug ( "Sending %d bytes of data..." % sent ) log . debug ( "Encoded data (type %s):\n%s" % ( type ( encoded ) , encoded ) ) if not _py3k : try : outstream . write ( encoded ) except IOError as ioe : # Can get 'broken pipe' errors even when all data was sent if 'Broken pipe' in str ( ioe ) : log . error ( 'Error sending data: Broken pipe' ) else : log . exception ( ioe ) break else : log . debug ( "Wrote data type <type 'str'> to outstream." ) else : try : outstream . write ( bytes ( encoded ) ) except TypeError as te : # XXX FIXME This appears to happen because # _threaded_copy_data() sometimes passes the `outstream` as an # object with type <_io.BufferredWriter> and at other times # with type <encodings.utf_8.StreamWriter>.  We hit the # following error when the `outstream` has type # <encodings.utf_8.StreamWriter>. if not "convert 'bytes' object to str implicitly" in str ( te ) : log . error ( str ( te ) ) try : outstream . write ( encoded . decode ( ) ) except TypeError as yate : # We hit the "'str' does not support the buffer interface" # error in Python3 when the `outstream` is an io.BytesIO and # we try to write a str to it.  We don't care about that # error, we'll just try again with bytes. if not "does not support the buffer interface" in str ( yate ) : log . error ( str ( yate ) ) except IOError as ioe : # Can get 'broken pipe' errors even when all data was sent if 'Broken pipe' in str ( ioe ) : log . error ( 'Error sending data: Broken pipe' ) else : log . exception ( ioe ) break else : log . debug ( "Wrote data type <class 'str'> outstream." ) except IOError as ioe : # Can get 'broken pipe' errors even when all data was sent if 'Broken pipe' in str ( ioe ) : log . error ( 'Error sending data: Broken pipe' ) else : log . exception ( ioe ) break else : log . debug ( "Wrote data type <class 'bytes'> to outstream." ) try : outstream . close ( ) except IOError as ioe : log . error ( "Unable to close outstream %s:\r\t%s" % ( outstream , ioe ) ) else : log . debug ( "Closed outstream: %d bytes sent." % sent )