title: Why do I have to type ctrl-d twice?
body: <p>For my own amusement, I've cooked up a python script that allows me to use python for bash one-liners; Supply a python generator expression; and the script iterates over it.  Here's the script:</p>
  
  <pre><code>DEFAULT_MODULES = ['os', 're', 'sys']
  
  _g = {}
  for m in DEFAULT_MODULES:
      _g[m] = __import__(m)
  
  import sys
  sys.stdout.writelines(eval(sys.argv[1], _g))
  </code></pre>
  
  <p>And here's how you might use it.</p>
  
  <pre><code>$ groups | python pype.py '(l.upper() for l in sys.stdin)'
  DBORNSIDE
  $ 
  </code></pre>
  
  <p>For the intended use, it works perfectly!</p>
  
  <p>But when I don't feed it with pipe and just invoke it directly, for instance: <sub>[emphasis added to show what I type]</sub></p>
  
  <pre>
  $ python pype.py '("%r\n" % (l,) for l in sys.stdin)'
  <strong>foo<kbd>Enter</kbd>
  bar<kbd>Enter</kbd>
  baz<kbd>Enter</kbd>
  <kbd>Ctrl D</kbd><kbd>Ctrl D</kbd></strong>'foo\n'
  'bar\n'
  'baz\n'
  $ 
  </pre>
  
  <p>In order to stop accepting input and produce any output, I have to type either <kbd>Enter</kbd> - <kbd>Ctrl D</kbd> - <kbd>Ctrl D</kbd> or  <kbd>Ctrl D</kbd> - <kbd>Ctrl D</kbd> - <kbd>Ctrl D</kbd>.  This violates my expectations, that each line should be processed as entered, and that typing <kbd>Ctrl D</kbd> at any time will end the script.  Where is the gap in my understanding?</p>
  
  <p>EDIT: I've updated the interactive example to show that I'm not seeing the quoting wim describes in his answer, and some more examples too.</p>
  
  <pre>
  $ python pype.py '("%r\n" % (l,) for l in sys.stdin)'
  <strong>foo<kbd>Ctrl D</kbd><kbd>Ctrl D</kbd>bar<kbd>Enter</kbd>
  <kbd>Ctrl D</kbd><kbd>Ctrl D</kbd></strong>'foobar\n'
  $ python pype.py '("%r\n" % (l,) for l in sys.stdin)'
  <strong>foo<kbd>Ctrl V</kbd><kbd>Ctrl D</kbd></strong>^D<strong>bar<kbd>Enter</kbd>
  <kbd>Ctrl D</kbd><kbd>Ctrl D</kbd></strong>'foo\x04bar\n'
  $ 
  </pre>