sqlalchemyorg / zimports

Python import rewriter
MIT License
75 stars 6 forks source link

zimports cannot read from /dev/stdin anymore #17

Closed schmir closed 4 years ago

schmir commented 4 years ago

When calling zimports with /dev/stdin as input file, zimports 0.2.1 crashes with OSError: [Errno 29] Illegal seek This is a regression from 0.2.0.

ralf@triton ~/t % cat zimports.py|zimports --stdout /dev/stdin
Traceback (most recent call last):
  File "/home/ralf/.local/bin/zimports", line 8, in <module>
    sys.exit(main())
  File "/home/ralf/.local/pipx/venvs/zimports/lib64/python3.8/site-packages/zimports.py", line 766, in main
    _run_file(options, filename)
  File "/home/ralf/.local/pipx/venvs/zimports/lib64/python3.8/site-packages/zimports.py", line 622, in _run_file
    lines, encoding_comment = _read_python_source(filename)
  File "/home/ralf/.local/pipx/venvs/zimports/lib64/python3.8/site-packages/zimports.py", line 558, in _read_python_source
    encoding_comment = _parse_magic_encoding_comment(file_)
  File "/home/ralf/.local/pipx/venvs/zimports/lib64/python3.8/site-packages/zimports.py", line 581, in _parse_magic_encoding_comment
    pos = fp.tell()
OSError: [Errno 29] Illegal seek

I'm using this in my zimports.el emacs package

zzzeek commented 4 years ago

OK this one, was not a thing I knew you could even do. i guess, I have to put whatever python io.Buffered thing aroudn the file handle.

zzzeek commented 4 years ago

OK it would help if you could check my work on this one, then I can release 0.2.2. I don't see any obvious way of unit testing this one without pulling out a whole bunch of IO mechanics. feel free to propose writing some kind of test here but otherwise just make sure on your end it works and I'll just comment the code why it's needed.

schmir commented 4 years ago

That fix works for me. I've opened PR #18 as an enhancement for this one. It makes 'zimports -' read from stdin and write to stdout. Using '-' as a filename is more or less a standard way on unix to read input from stdin (e.g. /usr/bin/cat supports it)