| Issue 243: | sys.stdout modification interferes with output collection | |
| 1 person starred this issue and may be notified of changes. | Back to list |
What steps will reproduce the problem?
------------------
# nose_test.py
import sys
import os
sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
def test():
assert 1
--------------------------
$nosetests nose_test.py # throws error
$nosetests -s nose_test.py # runs fine
What is the expected output? What do you see instead?
expected: OK
instead:
nosetests
E
======================================================================
ERROR: Failure: AttributeError ('cStringIO.StringO' object has no attribute
'fileno')
----------------------------------------------------------------------
Traceback (most recent call last):
File
"/usr/lib/python2.4/site-packages/nose-0.10.4-py2.4.egg/nose/loader.py",
line 363, in loadTestsFromName
module = self.importer.importFromPath(
File
"/usr/lib/python2.4/site-packages/nose-0.10.4-py2.4.egg/nose/importer.py",
line 39, in importFromPath
return self.importFromDir(dir_path, fqname)
File
"/usr/lib/python2.4/site-packages/nose-0.10.4-py2.4.egg/nose/importer.py",
line 84, in importFromDir
mod = load_module(part_fqname, fh, filename, desc)
File "/nfs/data/home/gregg/local/src/gits/oui/trunk/nose_test.py", line
3, in ?
sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
AttributeError: 'cStringIO.StringO' object has no attribute 'fileno'
----------------------------------------------------------------------
What version of the product are you using? On what operating system?
nosetests version 0.10.4
Linux FC5 on x64
|
|
,
Mar 06, 2009
If you want to capture sys.stdout yourself, you should turn off nose's capturing of it with the --nocapture flag.
Status: Invalid
|
|
,
Mar 06, 2009
I don't want to capture output myself. I want nosetests to automatically sniff out my tests. When sys.stdout is changed to nobuffer in this way, nosetests fails. |
|
,
Mar 06, 2009
nosetests is failing because when nose's output capture is enabled, sys.stdout doesn't have a fileno, because it's just a StringIO buffer -- like the exception says. If you want this to work when output capture is enabled, you'll have to find some other way to do what you're trying to do, or submit a patch implementing output capture with a buffer that behaves more like a real file. Without a patch, I can't prioritize changing nose's behavior, sorry. |
|
,
Mar 06, 2009
Thank you, that is quite a helpful response.
Specifically, the standard library docs say:
fileno( )
***Note: File-like objects which do not have a real file descriptor should not
provide this method!***
Here is workaround code:
-----
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
|
|
|
|