How to redirect Python’s sys.stdout to StringIO()