January 2, 2009

Weird Python 3.0 / SciTE Editor Issue - Console Output

My editor (SciTE) has always worked fine for programming Python 2.x. I'm now trying some Python 3.0 code and ran into an issue on my first script. The issue doesn't happen running the same code under 2.x. It is really confusing and annoying me and I have no idea who to file a bug with.

For whatever reason, as soon as I call an http.client request() method from within a while loop, nothing further is printed to the editor's console (stdout). If it is not in a loop, I get the output. The script executes fine aside from printing output. If I run the script from a regular command prompt (outside of SciTE), it works fine also.

Setup:
* Windows (tested on XP and Vista)
* Python 3.0 Final
* SciTE Version 1.75-wbd-1

This works: ('foo' is printed once to console)

import http.client

conn = http.client.HTTPConnection('www.goldb.org')
conn.request('GET', '/')
conn.close()
print('foo')

This works: ('foo' is printed repeatedly to console)

import http.client

while True:
    conn = http.client.HTTPConnection('www.goldb.org')
    # conn.request('GET', '/')
    conn.close()
    print('foo')

This doesn't work: (nothing is printed to console)

import http.client

while True:
    conn = http.client.HTTPConnection('www.goldb.org')
    conn.request('GET', '/')
    conn.close()
    print('foo')

This same exact setup works fine in Python 2.x. I've also tried starting Python with the '-u' option to get unbuffered output.

Anyone have ANY clue what could be going on?


Update: This seems to be related to Issue 4705 at bugs.python.org. It has to do with how Python 3.0 does (or doesn't) do unbuffered I/O. A patch was already submitted. Hope it's fixed in next release.

7 comments:

m said...

works for me

Corey Goldberg said...

Mischa..
what is the setup you are using?

is it Python 3.0 with SciTE on Windows?

note.. the code snippets work fine outside the editor's console (from a regular command prompt).

Anonymous said...

If the print() works before the close(), whatever Scite uses for handling stdout is being wrongly closed or redirected.

FWIW, I can reproduce on Linux and will try to understand what's going on.

Corey Goldberg said...

Daniel, thanks.

It actually happens no matter where I put the print.. even as the first statement in the loop.

If you find anything out, please let me know!

Anonymous said...

I've found a fix, first with sys.stdout.flush():

import sys
import http.client

while True:
[...]
+....sys.stdout.flush()

Then I recalled seeing this in a bug report: http://bugs.python.org/issue4705

So the workaround becomes:
import sys
sys.stdout._line_buffering = True
[your original code here]

Corey Goldberg said...

Thanks Daniel! Looks like my prob is related to Issue 4705 which seems to have been patched a few days ago.

Kent Johnson said...

It's bugs.python.org, not .com. Here is the direct link:
http://bugs.python.org/issue4705