Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

stdin is missing for any process I pipe into which uses Windows ftype and assoc mechanism to select the correct executable

I'm on WindowsXP. This seems to affect any process, but I'll use Python3.2 to demonstrate it. A script, 'filter.py':

import sys
for line in sys.stdin:
    print(line)

Running it like this:

echo hello | filter.py

Breaks like this:

Traceback (most recent call last):
  File "F:\Documents and Settings\jhartley\docs\projects\filtercwd\filter.py", line 3, in <module>
    for line in sys.stdin:
TypeError: 'NoneType' object is not iterable

Sure enough, adding a print to discover the value of sys.stdin reports that it is None (and of NoneType.)

Alternatively, running it like this:

echo hello | python filter.py

(with an explicit invokation of python) works perfectly fine.

My .py files are hooked up to execute using Python using the assoc and ftype mechanism (the way Windows associates particular filename extensions to be executed using particular programs):

> assoc .py
.py=Python.File
> ftype Python.File
Python.File="F:\Python32\python.exe" "%1" %*

(this is the same 'python.exe' that is foremost on my path)

Update: This isn't a Python thing. The same thing happens if I create filter.sh, run using cygwin bash. Explicitly running 'echo hello | bash filter.sh' works fine, but 'echo hello | filter.sh' which executes filter.sh using bash via the assoc and ftype mechanism, fails with '/dev/stdin: No such file or directory'.

So I do I have to add this explicit 'python' to all my command lines? Plus, I'm curious about why it's breaking. Is it just some peculiarity of my machine, or do other people see this too?

like image 269
Jonathan Hartley Avatar asked Nov 13 '22 20:11

Jonathan Hartley


1 Answers

What echo are you using? Do you have a UNIX-style echo utility on your path or are you just using the Microsoft one (which does not do what you want it to)? What happens if you type echo on at the command prompt?

I am running in Windows 7 and was unable to reproduce this. I have a UNIX-style echo utility on my path. I have installed ruby 1.9.2 and written tiny program in test.rb:

while s = $stdin.gets
  print s
end

The program runs when I type any of these commands:

  1. test.rb
  2. ruby test.rb
  3. echo hello | test.rb
  4. echo hello | ruby test.rb

Commands 1 and 2 behave identically. Commands 3 and 4 behave identically. Everything behaves as I would expect.

EDIT 1: Here are some commands I ran:

C:\Users\David\Documents\scraps\test_ruby>ls
test.rb

C:\Users\David\Documents\scraps\test_ruby>assoc .rb
File association not found for extension .rb

C:\Users\David\Documents\scraps\test_ruby>echo hi | test.rb
hi

I don't know much about ftype, but maybe you should install ruby on your system and see if my example program works for you.

like image 82
David Grayson Avatar answered Dec 05 '22 14:12

David Grayson