python - python2.7 peek at stdin -
i call sys.stdin.readlines()
without removing stdin. using python2.7 on linux.
for example, want is:
x = sys.stdin.readlines() y = sys.stdin.readlines()
then x , y have identical strings. acceptable if read stdin , put contents in.
background:
i have module either takes 1 file input , -optional argument or "some input piped module" , -optional argument
mymodule.py myfile -option or echo "some input" | mymodule.py -option
i have working, , works fine. checking sys.stdin.isatty()
determine if there input piped in. if there is, module throw error if there more 1 argument command line (there can 1 -optional argument, no files specified if there stdin)
the reason i'm having problem because i'm required have unit tests pass on command line in eclipse. works fine on command line, looks pyunit plugin eclipse uses sys.stdin
also.
if call sys.stdin.readlines()
, eclipse gives on running unit tests. additionally, eclipse pushing things sys.stdin
when don't specify arguments, makes difficult determine whether there valid arguments or not.
it seems me somehow getting sys.stdin.readlines()
without changing contents solution, don't know how this. answer solving problem satisfactory.
possible duplicate: peek stream of popen pipeline in python
thanks!
edit: not having luck like...
foo = sys.stdin.readlines() sys.stdin.write(foo)
edit: removed restoring stdin in teardown , putting in test function instead, no effect
when read pipe, data not in pipe anymore. can use polling mechanism in order see if there data read @ all, cannot @ contents without removing contents, @ least not on linux.
i ignore stdin if file provided (i.e. check file argument first) and, if no file provided argument, validate stdin.
Comments
Post a Comment