On 24 June 2014 06:17, Jonathan Goble <jcgoble3@gmail.com> wrote:
I've given up trying to solve a bug that popped up in my scripts a couple days ago. I run a bot for Wookieepedia, over at Wikia, and run three simple scripts on a daily basis. They are set up to run automatically through Windows Task Scheduler. Since they run automatically, they run in the background through pythonw.exe, i.e. without a console, and therefore I need a means of getting the output. My solution for the past two months has been to redirect sys.stdout and sys.stderr to the same StringIO() instance, then at the end call getvalue() on that and email it to myself.

Could it be you haven't updated in a few years? It sounds like it's related to a feature that I added two or three years ago, that allowed Windows users to get full unicode out- and input. However, that does mean sys.stdin and sys.stdout are no longer being used. We /do/ check whether the user has redirected the output using normal shell redirection, but your method doesn't do that.

There are a two options I can think of.
  - use normal shell redirection, e.g. https://stackoverflow.com/questions/8662024/how-do-i-capture-the-output-of-a-script-if-it-is-being-ran-by-the-task-scheduler

  - trick the code into thinking you're doing 'regular' redirection by adding a fileno function to your streams:
    see https://github.com/wikimedia/pywikibot-compat/blob/master/userinterfaces/win32_unicode.py#L92

    you'd need something like x = StringIO.StringIO(); x.fileno = lambda: 10
    and the same for stdout.

(By the way, the answer is NOT "switch to core". I have tried to get core to run on my system and failed miserably after two hours of repeated attempts without even getting it to talk to the wiki. Compat worked perfectly on the first try. Until such time as core can be installed by a beginner, it is not for me.)

I would appreciate it if you could clarify what the issues were you ran into.