This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Popen(...).stdout.seek(...) throws "Illegal seek"
Type: Stage:
Components: Versions: Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: jonash, nadeem.vawda, vstinner
Priority: normal Keywords:

Created on 2011-09-01 13:38 by jonash, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (5)
msg143323 - (view) Author: Jonas H. (jonash) * Date: 2011-09-01 13:38
from subprocess import Popen, PIPE
p = Popen(['ls'], stdout=PIPE)
p.wait()
p.stdout.seek(0)


Traceback (most recent call last):
  File "t.py", line 5, in <module>
    p.stdout.seek(0)
IOError: [Errno 29] Illegal seek

Python 2.7.2, Arch Linux x86-64 (Kernel 3.0)
msg143324 - (view) Author: Nadeem Vawda (nadeem.vawda) * (Python committer) Date: 2011-09-01 13:44
This is expected behaviour - you cannot seek on a pipe.
msg143325 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2011-09-01 13:44
stdout is a PIPE. You cannot seek in a PIPE.

Write stdout into a file, or use maybe BytesIO or StringIO?
msg143328 - (view) Author: Jonas H. (jonash) * Date: 2011-09-01 14:18
Why does it have a 'seek' method then?
msg143329 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2011-09-01 14:28
> Why does it have a 'seek' method then?

Python doesn't remove a method if the operation is forbidden. For example, Python doesn't remove write() method if the file is read only, and it doesn't remove most methods after close().

I prefer to have files with always the same API. I think that it's more practical.

Note: Text files of the io module (TextIOWrapper) raises io.UnsupportedOperation on seek() if the file is not seekable (e.g. if the file is a pipe):

$ ~/prog/python/default/python 
Python 3.3.0a0 (default:d26c7b18fc9d, Jul 22 2011, 12:04:31) 
>>> import os
>>> a,b=os.pipe()
>>> f=os.fdopen(a, 'rb')
>>> f
<_io.BufferedReader name=3>
>>> f.seek(0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IOError: [Errno 29] Illegal seek

>>> f2=os.fdopen(a, 'r')
>>> f2
<_io.TextIOWrapper name=3 mode='r' encoding='UTF-8'>
>>> f2.seek(0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
io.UnsupportedOperation: underlying stream is not seekable
History
Date User Action Args
2022-04-11 14:57:21adminsetgithub: 57086
2011-09-01 14:28:15vstinnersetmessages: + msg143329
2011-09-01 14:18:08jonashsetmessages: + msg143328
2011-09-01 13:44:32vstinnersetstatus: open -> closed

nosy: + vstinner
messages: + msg143325

resolution: not a bug
2011-09-01 13:44:15nadeem.vawdasetnosy: + nadeem.vawda
messages: + msg143324
2011-09-01 13:38:32jonashcreate