RustPython/RustPython · error · ValueError
getbuffer on closed file
Error message
getbuffer on closed file
What it means
BytesIO.getbuffer (Lib/_pyio.py:907) raises ValueError('getbuffer on closed file') when called after close(). The method returns a writable memoryview over the internal bytearray; once close() has cleared that bytearray the view is impossible to produce, so the guard fires instead of returning an empty view. Unlike getvalue(), a successfully obtained buffer is also invalidated once the BytesIO closes.
Source
Thrown at Lib/_pyio.py:907
self._pos = 0
def __getstate__(self):
if self.closed:
raise ValueError("__getstate__ on closed file")
return self.__dict__.copy()
def getvalue(self):
"""Return the bytes value (contents) of the buffer
"""
if self.closed:
raise ValueError("getvalue on closed file")
return bytes(self._buffer)
def getbuffer(self):
"""Return a readable and writable view of the buffer.
"""
if self.closed:
raise ValueError("getbuffer on closed file")
return memoryview(self._buffer)
def close(self):
if self._buffer is not None:
self._buffer.clear()
super().close()
def read(self, size=-1):
if self.closed:
raise ValueError("read from closed file")
if size is None:
size = -1
else:
try:
size_index = size.__index__
except AttributeError:
raise TypeError(f"{size!r} is not an integer")
else:View on GitHub (pinned to aaeab4f754)
Solutions
- Obtain the memoryview while the BytesIO is open and finish using it before close()
- If you only need the bytes, use getvalue() before closing and keep the bytes object
- Don't close the BytesIO while any memoryview from getbuffer() is still alive
Example fix
// before buf = io.BytesIO(b"abc") buf.close() view = buf.getbuffer() # ValueError: getbuffer on closed file // after buf = io.BytesIO(b"abc") view = buf.getbuffer() data = bytes(view) # consume while open buf.close()
Defensive patterns
Strategy: validation
Validate before calling
view = buf.getbuffer() if not buf.closed else memoryview(b"")
Try / catch
try:
view = buf.getbuffer()
except ValueError as e:
if "getbuffer on closed file" in str(e):
view = memoryview(b"")
else:
raise Prevention
- Take the memoryview while the BytesIO is open and release it before close
- Don't hold getbuffer() views across a later close of the owning buffer
- Snapshot with bytes(view) if the view must outlive the buffer
When it happens
Trigger: buf.close() followed by buf.getbuffer(); taking the memoryview inside a with-block but dereferencing/slicing it after exit; resize helpers that grab getbuffer() after cleanup closed the buffer.
Common situations: Zero-copy handoff of in-memory payloads to numpy or C extensions; slicing the buffer view in later processing stages after the owning BytesIO was closed; test teardown closing fixtures while views are still used.
Related errors
- __getstate__ on closed file
- getvalue on closed file
- read from closed file
- write to closed file
- seek on closed file
AI-assisted analysis of RustPython/RustPython@aaeab4f754 (2026-08-17).
Data as JSON: /api/errors/20cf499037fb0b24.
Report an issue: GitHub.