Note that there are some explanatory texts on larger screens.

plurals
  1. POpython2.7 peek at stdin
    text
    copied!<p>I would like to call <code>sys.stdin.readlines()</code> without removing anything from stdin. I am using Python2.7 on Linux.</p> <p>For example, what I want is:</p> <pre><code>x = sys.stdin.readlines() y = sys.stdin.readlines() </code></pre> <p>then x and y have identical strings. It would be acceptable if I could read from stdin and put the contents back in.</p> <p>Background:</p> <p>I have a module that EITHER takes one file as input and an -optional argument OR "some input piped into the module" and an -optional argument</p> <pre><code>mymodule.py myfile -option or echo "some input" | mymodule.py -option </code></pre> <p>I have this working, and it works fine. I am checking <code>sys.stdin.isatty()</code> to determine if there is any input piped in. If there is, the module will throw an error if there is more than one argument from the command line (there can be one -optional argument, but no files specified if there is stdin)</p> <p>The reason I'm having a problem is because I'm required to have unit tests pass on the command line but also in eclipse. Everything works fine on the command line, but it looks like the PyUnit plugin for eclipse uses <code>sys.stdin</code> also.</p> <p>If I call <code>sys.stdin.readlines()</code>, then eclipse gives up on running unit tests. Additionally, eclipse is pushing things into <code>sys.stdin</code> even when I don't specify any arguments, which makes it difficult to determine whether there are valid arguments or not.</p> <p>It seems to me that somehow getting <code>sys.stdin.readlines()</code> without changing the contents would be a solution, but I don't know how to do this. Any answer solving this problem would be satisfactory.</p> <p>Possible duplicate: <a href="https://stackoverflow.com/questions/12771540/peek-into-stream-of-popen-pipeline-in-python">Peek into stream of Popen pipeline in Python</a></p> <p>Thanks!</p> <p><b>Edit:</b> not having any luck with something like...</p> <pre><code>foo = sys.stdin.readlines() sys.stdin.write(foo) </code></pre> <p><b>Edit:</b> Removed restoring stdin in tearDown and putting it in test function instead, but no effect</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload