[py-dev] Issue with py.test and stderr

Grig Gheorghiu grig at gheorghiu.net
Fri Jan 14 20:56:48 CET 2005


Hi, Holger

I started to play with py.test after seeing it mentioned on a lot of
Python-related blogs. I find it really easy to use and powerful at the
same time, so congrats for a fine piece of work.

I did run into a slight issue though. At some point I was instantiating
a class which was redirecting stderr to stdout under some
circumstances. The __del__ method of my class was resetting sys.stderr
to sys.__stderr__. I think this caused a problem with py.test, in that
it interfered with py.test's own manipulation of sys.stderr. 

The end result was that I was getting a rather cryptic error message
from py.test:

# py.test -v test_clients.py
============================= test process starts
=============================
testing-mode: inprocess
executable  : /usr/bin/python  (2.2.3-final-0)
using py lib: /local/dist-py/py <rev 8277>
initial testconfig 0: /local/dist-py/py/test/defaultconfig
===============================================================================

Traceback (most recent call last):
  File "/local/dist-py/py/bin/py.test", line 5, in ?
    main()
  File "/local/dist-py/py/test/cmdline.py", line 32, in main
    run.inprocess(args, filenames)
  File "/local/dist-py/py/test/run.py", line 141, in inprocess
    driver.run(fncollectors)
  File "/local/dist-py/py/test/drive.py", line 43, in run
    self.run_collector_or_item(x)
  File "/local/dist-py/py/test/drive.py", line 61, in
run_collector_or_item
    self.runcollector(obj)
  File "/local/dist-py/py/test/drive.py", line 83, in runcollector
    self.run_collector_or_item(obj)
  File "/local/dist-py/py/test/drive.py", line 61, in
run_collector_or_item
    self.runcollector(obj)
  File "/local/dist-py/py/test/drive.py", line 83, in runcollector
    self.run_collector_or_item(obj)
  File "/local/dist-py/py/test/drive.py", line 59, in
run_collector_or_item
    self.runitem(obj)
  File "/local/dist-py/py/test/drive.py", line 103, in runitem
    self.reporter.enditem(res)
  File "/local/dist-py/py/test/report/text/reporter.py", line 141, in
enditem
    result.out, result.err = item.iocapture.reset()
  File "/local/dist-py/py/test/tool/outerrcapture.py", line 29, in
reset
    err = e.getvalue()
AttributeError: 'file' object has no attribute 'getvalue'

The only way I was able to get around this issue without modifying my
class was to edit py/test/tool/outerrcapture.py and replace:

err = e.getvalue()

with 

try:
    err = e.getvalue()
except:
    err = None

Then things seemed to work fine. 

The real solution I found was to modify my class and save sys.stderr
into a temp and restore it in __del__ to that temp, instead of
restoring it to sys.__stderr__. Then I could get rid of my
modifications of outerrcapture.py.

Anyway, I guess this is a pretty rare case, but maybe you can insert a
try/catch in outerrcapture.py to prevent things like this from
happening.

All the best,

Grig

P.S. I'm really interested in both Python and testing, so if you guys
need some help with py.test in the future, I'd be glad to pitch in. I
have a blog with some of the stuff I'm doing at
http://agiletesting.blogspot.com . 

I've been using unittest quite heavily, but I'm also trying to look
into alternatives, mainly py.test and doctest. I released a JUnitPerf
port to Python which I called pyUnitPerf, so now I'm thinking about
doing something similar and more "pythonic" with py.test. Maybe
py.test.perf?!! 



More information about the Pytest-dev mailing list