def print_attachments(stream, test, all_channels=False):
    """Print out subunit attachments.

    Print out subunit attachments that contain content. This
    runs in 2 modes, one for successes where we print out just stdout
    and stderr, and an override that dumps all the attachments.
    """
    channels = 'stdout', 'stderr'
    for name, detail in test['details'].items():
        name = name.split(':')[0]
        if detail.content_type.type == 'test':
            detail.content_type.type = 'text'
        if (all_channels or name in channels) and detail.as_text():
            title = 'Captured %s:' % name
            stream.write('\n%s\n%s\n' % (title, '~' * len(title)))
            for line in detail.as_text().split('\n'):
                line = line.encode('utf8')
                stream.write('    %s\n' % line)
