{"record":{"id":"737e567a6cac1515","repo":"RustPython/RustPython","slug":"file-or-stream-is-not-seekable","errorCode":null,"errorMessage":"File or stream is not seekable.","messagePattern":"File or stream is not seekable\\.","errorType":"exception","errorClass":"UnsupportedOperation","httpStatus":null,"severity":"error","filePath":"Lib/_pyio.py","lineNumber":434,"sourceCode":"        # If close() fails, the caller logs the exception with\n        # sys.unraisablehook. close() must be called at the end at __del__().\n        self.close()\n\n    ### Inquiries ###\n\n    def seekable(self):\n        \"\"\"Return a bool indicating whether object supports random access.\n\n        If False, seek(), tell() and truncate() will raise OSError.\n        This method may need to do a test seek().\n        \"\"\"\n        return False\n\n    def _checkSeekable(self, msg=None):\n        \"\"\"Internal: raise UnsupportedOperation if file is not seekable\n        \"\"\"\n        if not self.seekable():\n            raise UnsupportedOperation(\"File or stream is not seekable.\"\n                                       if msg is None else msg)\n\n    def readable(self):\n        \"\"\"Return a bool indicating whether object was opened for reading.\n\n        If False, read() will raise OSError.\n        \"\"\"\n        return False\n\n    def _checkReadable(self, msg=None):\n        \"\"\"Internal: raise UnsupportedOperation if file is not readable\n        \"\"\"\n        if not self.readable():\n            raise UnsupportedOperation(\"File or stream is not readable.\"\n                                       if msg is None else msg)\n\n    def writable(self):\n        \"\"\"Return a bool indicating whether object was opened for writing.","sourceCodeStart":416,"sourceCodeEnd":452,"githubUrl":"https://github.com/RustPython/RustPython/blob/aaeab4f754b4f40efc0c8ab39cf7c4a3c35a8cfd/Lib/_pyio.py#L416-L452","documentation":"io.UnsupportedOperation raised through IOBase._checkSeekable when a positioning operation (seek, tell, truncate) is attempted on a stream whose seekable() returns False. Seekability is a property of the underlying channel - pipes, sockets, and some wrapped streams are not seekable, while regular files and BytesIO are. The optional msg parameter lets subclasses customize the text; the default message is shown here.","triggerScenarios":"f.seek(0) on sys.stdin/sys.stdout when they are pipes; seek()/tell() on a socket file object from sock.makefile(); seeking a subprocess stdout pipe; a custom stream subclass that left seekable() returning the IOBase default False.","commonSituations":"Code tested against real files but run with piped stdin (curl ... | script); seek(0) to rewind for a second parse pass; serialization helpers that tell() to record offsets; multipart parsers that need lookahead.","solutions":["Branch on the capability: if f.seekable(): f.seek(0) else re-read from the source","Buffer the stream first: data = f.read(), then work on io.BytesIO(data), which is seekable","For sockets/pipes, design a streaming parse that never rewinds"],"exampleFix":"# before\nf.seek(0)                    # dies when f is a pipe\nheader = f.read(16)\n\n# after\nfrom io import BytesIO\ndata = f.read()\nbuf = BytesIO(data)          # seekable in-memory copy\nheader = buf.read(16)","handlingStrategy":"validation","validationCode":"if f.seekable():\n    f.seek(0)\n    header = f.read(16)\nelse:                                    # pipe/socket: buffer it instead\n    data = f.read()\n    header = data[:16]","typeGuard":"def is_seekable(f) -> bool:\n    return bool(getattr(f, 'seekable', lambda: False)())","tryCatchPattern":"import io\ntry:\n    f.seek(0)\nexcept io.UnsupportedOperation:\n    data = f.read()      # stream once, buffer locally","preventionTips":["Branch on f.seekable() before seek/tell/truncate","Design parsers to stream forward-only when the source may be a pipe","Wrap non-seekable sources in BytesIO when re-reads are needed"],"tags":["io","seek","tell","unsupported-operation","pipes","sockets","stream-capability"],"backgroundTag":"stream-not-seekable","analyzedSha":"aaeab4f754b4f40efc0c8ab39cf7c4a3c35a8cfd","analyzedAt":"2026-08-17T00:37:52.100Z","schemaVersion":2},"datasetVersion":"2026-08-17T04:17:16.089Z"}