def my_warning_wrapper(message, category, filename, lineno, file=myStringIO,
    line=None):
    warnings.show_warning(message, category, filename, lineno, file, line)
warnings._show_warning = my_warning_wrapper
How to redirect python warnings to a custom stream?