qdb swallows (some) program output
I noticed when running the example that the print statements don't actually come out on stdout anymore, but instead go to the tmp file. With some testing I determined this is all print statements, but not writes directly to sys.stdout.
I don't necessarily disagree with having program output written to the tmp file (having the program output and the debugger output in the same place is convenient, and even mimics was the standard pdb does), nor am I advocating for use of print statements in code, but I definitely don't think that it should be modifying program behaviour like this, and this may cause qdb to be unusable in some situations. I haven't investigated how this gagging is done so I also don't know if it could affect things.
TLDR;
Code run with set_trace() should produce the same output as code run normally.
Can you clarify on what you are expecting the behaviour to be?
One of the arguments to set_trace (that can be passed through the .qdb file) is called redirect_output, this flag will redirect stdout and stderr to the client.
This is to make sure that a truly remote client can see what the stdout and stderr are logging.
If you don't want this behaviour, you can always change this flag.
In the repl though, all stdout will ALWAYS come back to the user, regardless of the status of that flag.
Do you think that if this flag is True that output should be sent to both std(out|err) and the client?
I wasn't aware that was a configurable option. Is redirect_output by default set to True? Because I'm running the server, client, and set_trace() all in the same directory (without a .qdb file) and I'm getting this behaviour. Having this as an option is neat, but I didn't set any options so I was a bit taken aback by it.
As an aside, in my trying to pin down what was going on I found examples where this redirection doesn't work:
import qdb
from sys import stdout
qdb.set_trace()
print "this gets redirected"
stdout.write("this doesn't\n")
Not sure if this matters to you, but felt it worth mentioning.
@llllllllll would it make sense, rather than redirecting stdout in this case, to tee it, so that writes to stdout appear both in the window of the running script and in the output of the debugging terminal? I can see how it might be surprising to step over a print statement in your script and not see the print appear in the window running that script.
@ssanderson I like that approach. It removes any potential issues surrounding dependency on std(err|out).
About the stdout.write case, the reason that doesn't work is that you are holding a reference to stdout from before qdb could patch it. I am not sure if I want to support this case, as it would be a more in depth patching process.
I like the idea of a tee for standard output streams; however, I just don't want people to step into that logic when stepping into a print. I will try to think of a good means of implementing this. of course, if you have any ideas, let me know.
@llllllllll I think you just need to replace the StringIO created when redirect_output=True with something like https://github.com/ipython/ipython/blob/master/IPython/utils/io.py#L110.
The original implementation was similar to this; however, the task is to not make this visible to the user. Basically, this would make it so that you could step into a print and it would dump you in the write method of the Tee object which is confusing.
Why don't you get dumped into the write method for StringIO currently?
Erm, yes, this is a good question. I think I need to look over this section of code again.
It might be that we don't trace C function calls, and I am using cStringIO. I don't think I remember writing anything more robust around this; but, I could be wrong.
Given that we never explicitly call self.stdout|err.write in tracer.py, I'm pretty sure those writes happen internally in the Bdb superclass, which means I don't think it'll drop you into that logic regardless. Though there's an easy way to find out :).
This is a great discussion, but I would like to re-raise my original point:
redirect_output appears to be enabled by default. First, is this intended? Second, I would like to raise the argument that it shouldn't be.
redirect_output is enabled by default intentionally. The reason is that if you are running in a remote setting, you may have no other means of seeing the std(out|err) of the program.
I do not think that it would be valuable to change this default until we have some means of tee'ing the output to go to both the clients and the original stdout because as a user, I would not want to lose potentially valuable debugging information because I forgot to set a flag. I think that more commonly a program's execution does not depend on the stdout and stderr, especially in a debugging situation; however, it appears your use case requires this. Because you explicitly know that you need stdout and stderr to NOT be redirected, you should have to set this flag.
Please keep in mind that I write this in total positivity, that I love your tool, and that everything is awesome. And that I use 'you' impersonally. I understand this is a judgement call, since it's not actually breaking functionality (the option exists), but I would like to defend my opinion.
The Zen of Python has one line in particular that I have seen utilized in describing what Pythonic means more than any other:
Explicit is better than implicit.
When you run qdb.set_trace() you expect similar functionality to running pdb.set_trace() (or as I prefer, ipdb.set_trace()), which does not modify program behaviour / do monkey patching, etc. Yet qdb.set_trace() implicitly monkey patches things to redirect output of existing commands elsewhere, were as qdb.set_trace(redirect_output=True) (or using a custom .qdb file) is nicely explicit, and does exactly what you've told it to do.
Then, to take my point about changing things implicitly further:
With existing debuggers, when you run .set_trace() it's expected that the user is sitting at the terminal at stdout, so pdb sends additional information there, intermixing program output with trace information etc. so you're one: already unable to depend on output since the debug information is being added, and two: you're sitting at a terminal, not spying on a pipe, so the likelyhood that you depend on this output in any important way is slim. But when you use qdb it's mostly explicitly for a program where you don't have this access. Say a web process, a thread, or a just a remote machine. In these instances it is much more likely you depend on this output. Two proof of concept examples: subprocess.check_output() and the related .PIPE and .STDOUT, and of course any CGI app ever (that's how CGI works).
At this point I'd like to quote another line from The Zen of Python:
Special cases aren't special enough to break the rules.:
I would argue that yours is the special case (where this change in behaviour is acceptable). One of the core ideas behind Unix functionality is the idea of communication via plain text, you can read a bit about this here (a paper quoting a bunch of big people at Bell Labs who inveted Unix, summerizing the Unix philosophy). Which, to pull another quote from:
Expect the output of every program to become the input to another, as yet unknown, program.
In the case of traditional debuggers, you are already agreeing that the output of this program is not the input of another (well, a terminal, but semantics). One of the problems that qdb actually aims to solve (as I see it) is when you need to debug a program that is used as input to another program:
cat this.log | grep $SOME_PATTERN | python myscript.py | mysql
^ this is broken
or a CGI app see as previous, or a process created with subprocess.popen() as previously stated. Because of these use cases, no such agreement is made, and by (defaultly) modifying the programs output, you are violating many as-yet-unknown dependancies between programs.
It's a cool feature, but the thing about debuggers is you're usually already going into a broken program, and I'd much rather have a single "Doh, forgot to redirect output" when I go to debug something and don't see output I already know exists, than spend hours trying to figure out some odd behaviour, only to later realize most of what I was trying to figure out was actually caused by an undocumented default behaviour of my debugger.
And again, positivity! Everything is awesome! Great tool, and great work!
But, I respect your opinion, and pointedly disagree.
Sorry if I came off as harsh before, I totally agree with what you are saying. I didn't consider the pipeline use case and think that this should work. I think the new behavior should be: by default redirect_output is True meaning that output goes to BOTH the client and the normal streams.
If the user sets redirect_output to False, then it will go ONLY to the normal output streams.
How does this behavior sound?
Also, as a user, how would you feel if calling step on a print statement stopped you in some patched write function? Maybe one with a docstring that explains why you stepped into this function?
As long as there's that docstring I don't think people will mind, being left confused is what gets people frustrated, not necessarily seeing something new. I would even encourage adding instructions on the redirect_output parameter be put there, so the reason and option are presented all at once.
@conslo Just wanted to see if the behavior in #22 makes sense to you.
Closing as this has been added in #22