io

package
v0.12.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 15, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
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

View Source
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

View Source
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

View Source
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

View Source
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

View Source
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

View Source
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

View Source
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

View Source
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

View Source
var IncrementalNewlineDecoderType = objects.NewType("_io.IncrementalNewlineDecoder", []*objects.Type{objects.ObjectType()})

IncrementalNewlineDecoderType is the type for _io.IncrementalNewlineDecoder.

View Source
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

View Source
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

View Source
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

View Source
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

func IOBaseCannotPickle(self objects.Object) (objects.Object, error)

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

func Open(args []objects.Object, kwargs map[string]objects.Object) (objects.Object, error)

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

type Buffered struct {
	objects.Header
	// contains filtered or unexported fields
}

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

type BytesIO struct {
	objects.Header
	// contains filtered or unexported fields
}

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

func NewBytesIO(initial []byte) *BytesIO

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

func (b *BytesIO) Close() error

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

func (b *BytesIO) GetValue() []byte

GetValue returns the logical contents as a new bytes object.

CPython: Modules/_io/bytesio.c:369 _io_BytesIO_getvalue_impl

func (*BytesIO) Read

func (b *BytesIO) Read(size int) []byte

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

func (b *BytesIO) Seek(pos, whence int) (int, error)

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

func (b *BytesIO) Tell() int

Tell returns the current cursor position.

CPython: Modules/_io/bytesio.c:415 _io_BytesIO_tell_impl

func (*BytesIO) Truncate

func (b *BytesIO) Truncate(size int) (int, error)

Truncate cuts the buffer to at most size bytes. The cursor is unchanged. Errors out if exports are held.

CPython: Modules/_io/bytesio.c:646 _io_BytesIO_truncate_impl

func (*BytesIO) Write

func (b *BytesIO) Write(p []byte) int

Write copies p into the buffer at pos, zero-padding any over-seek gap between string_size and pos. Returns the number of bytes written. Mirrors write_bytes_lock_held.

CPython: Modules/_io/bytesio.c:192 write_bytes_lock_held

type FileIO

type FileIO struct {
	objects.Header
	// contains filtered or unexported fields
}

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

func NewFileIO(f *stdos.File, name, mode string, readable, writable bool) *FileIO

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

func (fi *FileIO) Close() error

Close releases the descriptor unless closefd is False.

CPython: Modules/_io/fileio.c:159 _io_FileIO_close_impl

func (*FileIO) Fileno

func (fi *FileIO) Fileno() (int64, error)

Fileno returns the underlying OS descriptor.

CPython: Modules/_io/fileio.c:602 _io_FileIO_fileno_impl

func (*FileIO) Read

func (fi *FileIO) Read(size int) ([]byte, error)

Read reads at most size bytes. size<0 delegates to ReadAll.

CPython: Modules/_io/fileio.c:863 _io_FileIO_read_impl

func (*FileIO) ReadAll

func (fi *FileIO) ReadAll() ([]byte, error)

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

func (fi *FileIO) ReadInto(dst []byte) (int, error)

ReadInto fills dst, returning the number of bytes actually read.

CPython: Modules/_io/fileio.c:676 _io_FileIO_readinto_impl

func (*FileIO) Seek

func (fi *FileIO) Seek(pos int64, whence int) (int64, error)

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

func (fi *FileIO) Tell() (int64, error)

Tell returns the current offset.

CPython: Modules/_io/fileio.c:1055 _io_FileIO_tell_impl

func (*FileIO) Truncate

func (fi *FileIO) Truncate(size int64) (int64, error)

Truncate cuts the file to at most size bytes.

CPython: Modules/_io/fileio.c:1078 _io_FileIO_truncate_impl

func (*FileIO) Write

func (fi *FileIO) Write(data []byte) (int, error)

Write writes data, returning bytes written.

CPython: Modules/_io/fileio.c:925 _io_FileIO_write_impl

type IOBase

type IOBase struct {
	objects.Header
	// contains filtered or unexported fields
}

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

type IncrementalNewlineDecoder struct {
	objects.Header
	// contains filtered or unexported fields
}

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 RWPair

type RWPair struct {
	objects.Header
	// contains filtered or unexported fields
}

RWPair backs BufferedRWPair.

CPython: Modules/_io/bufferedio.c:2230 rwpair (typedef)

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

type StringIO struct {
	objects.Header
	// contains filtered or unexported fields
}

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) Closed

func (s *StringIO) Closed() bool

Closed reports whether the StringIO has been closed.

func (*StringIO) GetValue

func (s *StringIO) GetValue() string

GetValue returns the logical contents as a Go string.

CPython: Modules/_io/stringio.c:284 _io_StringIO_getvalue_impl

func (*StringIO) Read

func (s *StringIO) Read(size int) string

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

func (s *StringIO) Seek(pos, whence int) (int, error)

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

func (s *StringIO) Tell() int

Tell returns the cursor position in characters.

CPython: Modules/_io/stringio.c:303 _io_StringIO_tell_impl

func (*StringIO) Truncate

func (s *StringIO) Truncate(size int) (int, error)

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

func (*StringIO) Write

func (s *StringIO) Write(text string) int

Write appends s to the buffer at the current cursor, applying newline translation per the constructor's newline mode and zero-padding any over-seek gap. Returns the number of characters written (post-translation length).

CPython: Modules/_io/stringio.c:189 write_str

type TextIOWrapper

type TextIOWrapper struct {
	objects.Header
	// contains filtered or unexported fields
}

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

func (t *TextIOWrapper) SeekCookie(cookieInt *big.Int, whence int) (*big.Int, error)

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

func (*TextIOWrapper) Truncate

func (t *TextIOWrapper) Truncate(pos int64, hasPos bool) (int64, error)

Truncate truncates the file at the current position (or pos if given).

CPython: Modules/_io/textio.c:2968 textiowrapper_truncate_impl

func (*TextIOWrapper) Write

func (t *TextIOWrapper) Write(s string) (int, error)

Write encodes and writes a string to the underlying buffer.

CPython: Modules/_io/textio.c:1662 textiowrapper_write_impl

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL