In some of our server code, we like to insure we get unbuffered output, like in perl.  In Python, this is easy to do:


import sys

sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)

However, using the wonderful nosetest for testing will barf on this code, because it reassigns stdout to a cStringIO for capturing.

This is a workaround:


import sys
import os

try:   # get unbuffered output
    sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
except AttributeError, exc: # under nose, sys.stdout is reassigned to a string buffer
    pass

def test():
    assert 1

Alternately, run nosetests -s, which disables the output capture feature.

Cf:  my embarrassing bug report over at nose

2 Responses to “Adventures in Nose: nosetests and unbuffered stdout.”

  1. Russell Says:

    Surely, you like to *insure* you get buffered output? It’s easy to be unsure!

  2. Gregg Lind Says:

    I’m a big fan of non-deterministic computing, acutally, so it’s more exciting when I’m *unsure* about things. Correctness through approximation.


Leave a Reply