Could you provide a minimal example that would allow us to reproduce the issue? With core, the following works for me (I don't have compat installed):

import sys, StringIO
oldstderr = sys.stderr
oldstdout = sys.stdout

sys.stderr = sys.stdout = StringIO.StringIO()
sys.stderr.fileno = lambda: 10

import pywikibot
oldstdout.write("Before output\n")

oldstdout.write("After output; this is in our buffer:\n")

$ PYWIKIBOT2_DIR=. python
Before output
After output; this is in our buffer:
'WARNING: Running on Windows and transliteration_target is not set.\nPlease see\ntest

Nonetheless, I would suggest to use normal shell redirection techniques, instead of monkey-patching python's sys module. If you want to use python, the following should work (untested):

import subprocess
stdout, stderr = subprocess.Popen(["c:\python27\python", ""], stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
# do something with stdout and stderr

I *think* it should also be possible to do this with the logging module, but I'm not sure how to do that in a quick-and-easy way.


On 24 June 2014 09:52, Jonathan Goble <> wrote:
When the error began occurring, I was using the Pywikibot nightly dump from February 25 of this year. It continues to occur with the nightly dump from yesterday (June 23). I just tried the fileno = lambda: 10 trick and it had no effect.

As for core, it's been four months since I last tried it so I don't recall the issues. It's past bedtime for me right now, but I'll look into giving it another try within the next day or two when I have some time.

On Tue, Jun 24, 2014 at 3:17 AM, Merlijn van Deen <> wrote:
On 24 June 2014 06:17, Jonathan Goble <> 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.

  - trick the code into thinking you're doing 'regular' redirection by adding a fileno function to your streams:

    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.


Pywikipedia-l mailing list

Pywikipedia-l mailing list