Documentation
¶
Index ¶
- Constants
- Variables
- func IOBaseCannotPickle(self objects.Object) (objects.Object, error)
- func Open(args []objects.Object, kwargs map[string]objects.Object) (objects.Object, error)
- type Buffered
- type BytesIO
- type FileIO
- func (fi *FileIO) Close() error
- func (fi *FileIO) Fileno() (int64, error)
- func (fi *FileIO) Read(size int) ([]byte, error)
- func (fi *FileIO) ReadAll() ([]byte, error)
- func (fi *FileIO) ReadInto(dst []byte) (int, error)
- func (fi *FileIO) Seek(pos int64, whence int) (int64, error)
- func (fi *FileIO) Tell() (int64, error)
- func (fi *FileIO) Truncate(size int64) (int64, error)
- func (fi *FileIO) Write(data []byte) (int, error)
- type IOBase
- type IncrementalDecoder
- type IncrementalEncoder
- type IncrementalNewlineDecoder
- type RWPair
- type RawIOBase
- type StringIO
- func (s *StringIO) Close()
- func (s *StringIO) Closed() bool
- func (s *StringIO) GetValue() string
- func (s *StringIO) Read(size int) string
- func (s *StringIO) Seek(pos, whence int) (int, error)
- func (s *StringIO) Tell() int
- func (s *StringIO) Truncate(size int) (int, error)
- func (s *StringIO) Write(text string) int
- type TextIOWrapper
- func (t *TextIOWrapper) Close() error
- func (t *TextIOWrapper) Detach() (objects.Object, error)
- func (t *TextIOWrapper) Flush() error
- func (t *TextIOWrapper) Read(size int) (string, error)
- func (t *TextIOWrapper) Readline(size int) (string, error)
- func (t *TextIOWrapper) Seek(pos int64, whence int) (int64, error)
- func (t *TextIOWrapper) SeekCookie(cookieInt *big.Int, whence int) (*big.Int, error)
- func (t *TextIOWrapper) Tell() (int64, error)
- func (t *TextIOWrapper) TellCookie() (*big.Int, error)
- func (t *TextIOWrapper) Truncate(pos int64, hasPos bool) (int64, error)
- func (t *TextIOWrapper) Write(s string) (int, error)
Constants ¶
const DefaultBufferSize = 8192
DefaultBufferSize is the value Lib/io.py copies into its module-level constant of the same name. Mirrors the macro in _iomodule.h.
CPython: Modules/_io/_iomodule.h:14 DEFAULT_BUFFER_SIZE
Variables ¶
var BufferedIOBaseType = objects.NewType("_io._BufferedIOBase", []*objects.Type{IOBaseType})
BufferedIOBaseType is the type singleton for _io._BufferedIOBase.
CPython: Modules/_io/bufferedio.c:2528 bufferediobase_spec
var BufferedRWPairType = objects.NewType("_io.BufferedRWPair", []*objects.Type{BufferedIOBaseType})
BufferedRWPairType is the type singleton for _io.BufferedRWPair.
CPython: Modules/_io/bufferedio.c:2699 bufferedrwpair_spec
var BufferedRandomType = objects.NewType("_io.BufferedRandom", []*objects.Type{BufferedIOBaseType})
BufferedRandomType is the type singleton for _io.BufferedRandom.
CPython: Modules/_io/bufferedio.c:2767 bufferedrandom_spec
var BufferedReaderType = objects.NewType("_io.BufferedReader", []*objects.Type{BufferedIOBaseType})
BufferedReaderType is the type singleton for _io.BufferedReader.
CPython: Modules/_io/bufferedio.c:2591 bufferedreader_spec
var BufferedWriterType = objects.NewType("_io.BufferedWriter", []*objects.Type{BufferedIOBaseType})
BufferedWriterType is the type singleton for _io.BufferedWriter.
CPython: Modules/_io/bufferedio.c:2649 bufferedwriter_spec
var BytesIOType = objects.NewType("_io.BytesIO", []*objects.Type{objects.ObjectType()})
BytesIOType is the type singleton for _io.BytesIO.
CPython: Modules/_io/bytesio.c PyBytesIO_Type
var FileIOType = objects.NewType("_io.FileIO", []*objects.Type{objects.ObjectType()})
FileIOType is the type singleton for _io.FileIO.
CPython: Modules/_io/fileio.c:1245 PyFileIO_Type
var IOBaseType = objects.NewType("_io._IOBase", []*objects.Type{objects.ObjectType()})
IOBaseType is the type singleton for _io._IOBase.
CPython: Modules/_io/iobase.c:887 iobase_spec
var IncrementalNewlineDecoderType = objects.NewType("_io.IncrementalNewlineDecoder", []*objects.Type{objects.ObjectType()})
IncrementalNewlineDecoderType is the type for _io.IncrementalNewlineDecoder.
var RawIOBaseType = objects.NewType("_io._RawIOBase", []*objects.Type{IOBaseType})
RawIOBaseType is the type singleton for _io._RawIOBase.
CPython: Modules/_io/iobase.c:1050 rawiobase_spec
var StringIOType = objects.NewType("_io.StringIO", []*objects.Type{objects.ObjectType()})
StringIOType is the type singleton for _io.StringIO.
CPython: Modules/_io/stringio.c:1073 stringio_slots
var TextIOBaseType = objects.NewType("_io._TextIOBase", []*objects.Type{IOBaseType})
TextIOBaseType is the type singleton for _io._TextIOBase.
CPython: Modules/_io/textio.c:202 textiobase_slots / textiobase_spec
var TextIOWrapperType = objects.NewType("_io.TextIOWrapper", []*objects.Type{objects.ObjectType()})
TextIOWrapperType is the type singleton for _io.TextIOWrapper.
CPython: Modules/_io/textio.c PyTextIOWrapper_Type equivalent
Functions ¶
func IOBaseCannotPickle ¶
IOBaseCannotPickle is exported so fileio / bufferedio / textio / stringio / bytesio can install it as __getstate__ / __reduce__ / __reduce_ex__.
CPython: Modules/_io/iobase.c:248 _PyIOBase_cannot_pickle
func Open ¶
Open implements _io.open(). It validates the mode string, opens the raw FileIO, and wraps it in TextIOWrapper for text mode or returns the FileIO directly for binary mode. Mirrors the CPython _io_open_impl logic: create raw, optionally wrap in buffered, optionally wrap in text.
CPython: Modules/_io/_iomodule.c:218 _io_open_impl
Types ¶
type Buffered ¶
Buffered backs BufferedReader, BufferedWriter, and BufferedRandom.
Field names mirror CPython's `buffered` struct exactly so the ported logic reads side-by-side with the C source.
CPython: Modules/_io/bufferedio.c:222 buffered (typedef)
type BytesIO ¶
BytesIO mirrors the C struct: buf is the heap-allocated bytes object, pos is the read/write cursor, stringSize tracks the logical length (which can differ from len(buf) when truncate/over-seek leave allocated capacity past the end), exports counts outstanding memoryview/getbuffer views.
CPython: Modules/_io/bytesio.c:17 bytesio
func NewBytesIO ¶
NewBytesIO creates an empty BytesIO. Caller may provide initial bytes.
CPython: Modules/_io/bytesio.c:1003 bytesio_new + _io_BytesIO___init___impl
func (*BytesIO) Close ¶
Close marks the BytesIO as closed and releases the buffer. Cannot be called while exports are live.
CPython: Modules/_io/bytesio.c:835 _io_BytesIO_close_impl
func (*BytesIO) GetValue ¶
GetValue returns the logical contents as a new bytes object.
CPython: Modules/_io/bytesio.c:369 _io_BytesIO_getvalue_impl
func (*BytesIO) Read ¶
Read returns up to size bytes; size<0 reads until end-of-buffer.
CPython: Modules/_io/bytesio.c:463 _io_BytesIO_read_impl
func (*BytesIO) Seek ¶
Seek changes the cursor position. whence=1/2 with a negative resulting position clamps to zero (matching CPython).
CPython: Modules/_io/bytesio.c:723 _io_BytesIO_seek_impl
func (*BytesIO) Tell ¶
Tell returns the current cursor position.
CPython: Modules/_io/bytesio.c:415 _io_BytesIO_tell_impl
type FileIO ¶
FileIO wraps an *stdos.File plus the bookkeeping CPython carries on its fileio struct: capability flags, mode bits, statinfo collected at open, and the seekable tri-state probed lazily on first call.
CPython: Modules/_io/fileio.c:65 fileio
func NewFileIO ¶
NewFileIO wraps an already-open *stdos.File. closefd defaults to true. Used by the open() helper and external callers that already hold a file.
CPython: Modules/_io/fileio.c:244 _io_FileIO___init___impl (analog)
func (*FileIO) Close ¶
Close releases the descriptor unless closefd is False.
CPython: Modules/_io/fileio.c:159 _io_FileIO_close_impl
func (*FileIO) Fileno ¶
Fileno returns the underlying OS descriptor.
CPython: Modules/_io/fileio.c:602 _io_FileIO_fileno_impl
func (*FileIO) Read ¶
Read reads at most size bytes. size<0 delegates to ReadAll.
CPython: Modules/_io/fileio.c:863 _io_FileIO_read_impl
func (*FileIO) ReadAll ¶
ReadAll reads until EOF with stat-informed pre-allocation and the new_buffersize growth schedule.
CPython: Modules/_io/fileio.c:736 _io_FileIO_readall_impl
func (*FileIO) ReadInto ¶
ReadInto fills dst, returning the number of bytes actually read.
CPython: Modules/_io/fileio.c:676 _io_FileIO_readinto_impl
func (*FileIO) Seek ¶
Seek moves to a new offset and returns it.
CPython: Modules/_io/fileio.c:1037 _io_FileIO_seek_impl via portable_lseek
func (*FileIO) Tell ¶
Tell returns the current offset.
CPython: Modules/_io/fileio.c:1055 _io_FileIO_tell_impl
type IOBase ¶
IOBase is the runtime object for _io._IOBase instances.
CPython: Modules/_io/iobase.c:33 iobase struct
type IncrementalDecoder ¶
type IncrementalDecoder interface {
// Decode consumes input and returns whatever characters it can
// fully decode. Bytes that begin an incomplete sequence stay
// buffered for the next call. final=true forces a flush; any
// still-pending bytes raise UnicodeDecodeError.
Decode(input []byte, final bool) (string, error)
// GetState returns (unconsumed-bytes, codec-specific-flags).
// CPython: `codecs.IncrementalDecoder.getstate` (`Lib/codecs.py:285`).
GetState() ([]byte, int64)
// SetState restores a snapshot. Used by TextIOWrapper.Seek.
SetState(buffer []byte, flags int64) error
// Reset drops the buffer and flag state.
Reset()
}
IncrementalDecoder is the stateful decoder protocol every codec exposes to TextIOWrapper. The contract matches CPython's `codecs.IncrementalDecoder` (`Lib/codecs.py:251`).
type IncrementalEncoder ¶
type IncrementalEncoder interface {
Encode(input string, final bool) ([]byte, error)
GetState() int64
SetState(state int64) error
Reset()
}
IncrementalEncoder mirrors `codecs.IncrementalEncoder` (`Lib/codecs.py:174`). Stateful encoders track whether a leading signature (BOM) has been emitted yet.
type IncrementalNewlineDecoder ¶
IncrementalNewlineDecoder wraps another codec decoder and handles universal newline translation. Implements the public interface from textio.c.
CPython: Modules/_io/textio.c:219 nldecoder_object
type RawIOBase ¶
type RawIOBase struct {
IOBase
}
RawIOBase is the runtime object for _io._RawIOBase instances.
CPython: Modules/_io/iobase.c:1050 rawiobase_spec
type StringIO ¶
StringIO mirrors the C struct, minus the accumulating-writer optimisation (we always work directly against the rune slab).
readTranslate (newline is None): writes have \r\n / \r converted to \n, reads return lines terminated by \n.
readUniversal (newline in {None, ""}): readline accepts \r / \n / \r\n as terminators.
readNL is the configured newline; writeNL is set when newline is "\r" or "\r\n" so writes translate \n to that sequence.
CPython: Modules/_io/stringio.c:20 stringio
func NewStringIO ¶
func NewStringIO() *StringIO
NewStringIO returns an empty StringIO in default-newline mode (newline="\n"). Callers populate the initial value through the constructor path.
CPython: Modules/_io/stringio.c:637 stringio_new
func (*StringIO) Close ¶
func (s *StringIO) Close()
Close marks the StringIO as closed and drops the buffer.
CPython: Modules/_io/stringio.c:580 _io_StringIO_close_impl
func (*StringIO) GetValue ¶
GetValue returns the logical contents as a Go string.
CPython: Modules/_io/stringio.c:284 _io_StringIO_getvalue_impl
func (*StringIO) Read ¶
Read returns up to size characters starting at the cursor; size<0 reads until end of stream.
CPython: Modules/_io/stringio.c:324 _io_StringIO_read_impl
func (*StringIO) Seek ¶
Seek changes the cursor position. whence=1/2 require pos==0 (matches CPython). whence=0 with pos<0 raises ValueError.
CPython: Modules/_io/stringio.c:497 _io_StringIO_seek_impl
func (*StringIO) Tell ¶
Tell returns the cursor position in characters.
CPython: Modules/_io/stringio.c:303 _io_StringIO_tell_impl
func (*StringIO) Truncate ¶
Truncate cuts the logical length to size characters; the underlying rune slab is shrunk to match. The cursor is unchanged.
CPython: Modules/_io/stringio.c:458 _io_StringIO_truncate_impl
type TextIOWrapper ¶
TextIOWrapper wraps a binary buffer with text encoding.
CPython: Modules/_io/textio.c:668 textio (struct)
func NewTextIOWrapper ¶
func NewTextIOWrapper(raw *FileIO, encoding, errors, name, mode string) *TextIOWrapper
NewTextIOWrapper wraps a binary buffer (typically FileIO) in text mode.
CPython: Modules/_io/textio.c:1102 textiowrapper_init
func (*TextIOWrapper) Close ¶
func (t *TextIOWrapper) Close() error
Close flushes and closes the underlying buffer.
CPython: Modules/_io/textio.c:3138 textiowrapper_close_impl
func (*TextIOWrapper) Detach ¶
func (t *TextIOWrapper) Detach() (objects.Object, error)
Detach flushes and detaches the underlying buffer.
CPython: Modules/_io/textio.c:1566 textiowrapper_detach_impl
func (*TextIOWrapper) Flush ¶
func (t *TextIOWrapper) Flush() error
Flush flushes the write buffer and the underlying buffer.
CPython: Modules/_io/textio.c:3121 textiowrapper_flush_impl
func (*TextIOWrapper) Read ¶
func (t *TextIOWrapper) Read(size int) (string, error)
Read is the Go-level read method (used by tests and internal code). size < 0 reads all.
func (*TextIOWrapper) Readline ¶
func (t *TextIOWrapper) Readline(size int) (string, error)
Readline is the Go-level readline method.
func (*TextIOWrapper) Seek ¶
func (t *TextIOWrapper) Seek(pos int64, whence int) (int64, error)
Seek is the int64 convenience wrapper used by callers that do not care about cookies (e.g. test code seeking to byte 0).
CPython: Modules/_io/textio.c:2536 textiowrapper_seek_impl
func (*TextIOWrapper) SeekCookie ¶
SeekCookie parses a cookie produced by Tell and reconstructs the decode state at that point. Whence must be 0 (SEEK_SET) when the cookie carries a non-zero replay payload, matching CPython which rejects whence!=0 unless the value is 0/EOF.
CPython: Modules/_io/textio.c:2536 textiowrapper_seek_impl
func (*TextIOWrapper) Tell ¶
func (t *TextIOWrapper) Tell() (int64, error)
Tell returns the current stream position as an int64 for callers that do not need the full cookie shape.
func (*TextIOWrapper) TellCookie ¶
func (t *TextIOWrapper) TellCookie() (*big.Int, error)
TellCookie returns the current stream position as a cookie integer. When decoded characters are buffered ahead of the caller, the cookie carries the snapshot state needed to round-trip via SeekCookie.
CPython: Modules/_io/textio.c:2734 textiowrapper_tell_impl