[python-daemon-Bugs][315090] Redirecting `sys.stdout` fails when `sys.stdout` is not a real stream

python-daemon-bugs at alioth.debian.org python-daemon-bugs at alioth.debian.org
Sun Jun 21 08:12:44 UTC 2015


python-daemon-Bugs item #315090 was changed at 2015-06-21 18:12 by Ben Finney
You can respond by visiting: 
https://alioth.debian.org/tracker/?func=detail&atid=413098&aid=315090&group_id=100328

Status: Open
Priority: 3
Submitted By: Torfus Polymorphus (torf-guest)
Assigned to: Nobody (None)
Summary: Redirecting `sys.stdout` fails when `sys.stdout` is not a real stream 


Initial Comment:
A feature of many Python testing frameworks (e.g. nose) is to capture output to STDOUT (the captured output is only displayed if a test fails). Usually, this is accomplished by replacing `sys.stdout` with another file-like object, e.g. a `cStringIO.StringIO` instance. However, this breaks DaemonContext, since DaemonContext tries to access `sys.stdout.fileno` in `redirect_stream` -- the capturing objects usually don't have a `fileno` attribute:

  File "/home/torf/projects/coba/venv/local/lib/python2.7/site-packages/daemon/daemon.py", line 380, in __enter__
    self.open()
  File "/home/torf/projects/coba/venv/local/lib/python2.7/site-packages/daemon/daemon.py", line 367, in open
    redirect_stream(sys.stdin, self.stdin)
  File "/home/torf/projects/coba/venv/local/lib/python2.7/site-packages/daemon/daemon.py", line 845, in redirect_stream
    os.dup2(target_fd, system_stream.fileno())
AttributeError: 'cStringIO.StringIO' object has no attribute 'fileno'

In my opinion, DaemonContext should handle this situation gracefully.


----------------------------------------------------------------------

>Comment By: Ben Finney (bignose-guest)
Date: 2015-06-21 18:12

Message:
Yes, a standard-library StringIO will not work for redirection, because redirection of a stream's file handle requires that there *be* a file handle.

The recommendation, if your test cases require a test double for I/O streams, would be to subclass StringIO to add a file handle and use that for the test double. That applies with any library which needs file features StringIO does not have.

Patching ‘python-daemon’ to special-case fake file objects will only help with ‘python-daemon’; the correct solution IMO is to provide a test double that behaves the way is needed for whatever you're testing with.

I'm open to argument though. What would you suggest is appropriate behaviour for ‘python-daemon’ in the face of a file that isn't actually a file?


----------------------------------------------------------------------

You can respond by visiting: 
https://alioth.debian.org/tracker/?func=detail&atid=413098&aid=315090&group_id=100328



More information about the python-daemon-bugs mailing list