adjust the code to deal with the case of sys.stdout == None