objects

package
v0.3.0 Latest Latest
Warning

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

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

Documentation

Overview

Package objects ports the gating subset of cpython/Objects/. v0.2 covers the object protocol, the type slot table, and the concrete builtins needed to construct a dict, hash a tuple, and iterate a list. Strings, bytes, set, exceptions, and the cycle collector arrive in later phases (see notes/Spec/1600/).

Index

Constants

This section is empty.

Variables

View Source
var BoolType = NewType("bool", []*Type{IntType})

BoolType is the type singleton for bool. Mirrors PyBool_Type.

CPython: Objects/boolobject.c:L222 PyBool_Type

View Source
var DictType = NewType("dict", []*Type{objectType})

DictType is the type singleton for dict. Mirrors PyDict_Type.

CPython: Objects/dictobject.c:L4023 PyDict_Type

View Source
var ErrStopIteration = errors.New("StopIteration")

ErrStopIteration signals end of iteration. Mirrors PyExc_StopIteration.

CPython: Objects/exceptions.c:L1937 PyExc_StopIteration

View Source
var FloatType = NewType("float", []*Type{objectType})

FloatType is the type singleton for float. Mirrors PyFloat_Type. Slots are wired in init() because floatHash transitively constructs Ints which would otherwise close the dep cycle.

CPython: Objects/floatobject.c:L2068 PyFloat_Type

View Source
var IntType = NewType("int", []*Type{objectType})

IntType is the type singleton for int. Mirrors PyLong_Type. Slots are wired in init() to break the variable-init dependency cycle (slots reference NewIntFromBig which references the small-int cache which references IntType).

CPython: Objects/longobject.c:L6447 PyLong_Type

View Source
var ListType = NewType("list", []*Type{objectType})

ListType is the type singleton for list. Mirrors PyList_Type.

CPython: Objects/listobject.c:L3380 PyList_Type

View Source
var RangeType = NewType("range", []*Type{objectType})

RangeType is the type singleton for range. Mirrors PyRange_Type.

CPython: Objects/rangeobject.c:L949 PyRange_Type

View Source
var SliceType = NewType("slice", []*Type{objectType})

SliceType is the type singleton for slice. Mirrors PySlice_Type.

CPython: Objects/sliceobject.c:L325 PySlice_Type

View Source
var TupleType = NewType("tuple", []*Type{objectType})

TupleType is the type singleton for tuple. Mirrors PyTuple_Type.

CPython: Objects/tupleobject.c:L961 PyTuple_Type

Functions

func Decref

func Decref(o Object)

Decref drops the refcount. If it reaches zero and the type defines a Dealloc slot, Dealloc is invoked. Until v0.10 lands the cycle collector, Dealloc is the only finalize hook.

CPython: Include/object.h:L631 Py_DECREF

func Hash

func Hash(o Object) (int64, error)

Hash returns the hash of o. Errors with errUnhashable when the type has no Hash slot. Mirrors PyObject_Hash.

CPython: Objects/object.c:L869 PyObject_Hash

func Incref

func Incref(o Object)

Incref bumps the refcount. Concurrent Increfs are safe.

CPython: Include/object.h:L605 Py_INCREF

func Is

func Is(a, b Object) bool

Is reports identity equality, the Python `is` operator. Mirrors Py_Is.

CPython: Include/object.h:L257 Py_Is

func IsNone

func IsNone(o Object) bool

IsNone reports whether o is the None singleton. Mirrors Py_IsNone.

CPython: Include/object.h:L827 Py_IsNone

func IsNotImplemented

func IsNotImplemented(o Object) bool

IsNotImplemented reports whether o is the NotImplemented singleton.

CPython: Objects/object.c:L1965 Py_IsNotImplemented

func IsSubtype

func IsSubtype(sub, super *Type) bool

IsSubtype reports whether sub is sub or one of its bases. Mirrors PyType_IsSubtype.

CPython: Objects/typeobject.c:L2556 PyType_IsSubtype

func IsTruthy

func IsTruthy(o Object) (bool, error)

IsTruthy mirrors PyObject_IsTrue.

CPython: Objects/object.c:L1671 PyObject_IsTrue

func Repr

func Repr(o Object) (string, error)

Repr returns the Python repr of o. Falls back to a generic `<type at addr>` when the type lacks a Repr slot.

CPython: Objects/object.c:L518 PyObject_Repr

func RichCmpBool

func RichCmpBool(a, b Object, op CompareOp) (bool, error)

RichCmpBool runs RichCmp and converts the result to a Go bool. Mirrors PyObject_RichCompareBool.

CPython: Objects/object.c:L795 PyObject_RichCompareBool

func Str

func Str(o Object) (string, error)

Str returns the Python str of o. Falls back to Repr.

CPython: Objects/object.c:L463 PyObject_Str

Types

type Bool

type Bool struct {
	Int
}

Bool is the Python bool. CPython makes bool a subclass of int with just two singleton instances; gopy follows the same pattern.

CPython: Objects/boolobject.c:L24 PyBool_Type

type CompareOp

type CompareOp int

CompareOp identifies the six comparison operators Python exposes through __lt__/__le__/__eq__/__ne__/__gt__/__ge__. Numeric values match the C macros Py_LT..Py_GE so direct ports of slot implementations can pass the integer through.

CPython: Include/object.h:L375 Py_LT

const (
	// CompareLT is `<`.
	CompareLT CompareOp = iota
	// CompareLE is `<=`.
	CompareLE
	// CompareEQ is `==`.
	CompareEQ
	// CompareNE is `!=`.
	CompareNE
	// CompareGT is `>`.
	CompareGT
	// CompareGE is `>=`.
	CompareGE
)

type Dict

type Dict struct {
	Header
	// contains filtered or unexported fields
}

Dict is the Python dict.

CPython: Include/cpython/dictobject.h:L19 PyDictObject

func NewDict

func NewDict() *Dict

NewDict builds an empty dict.

CPython: Objects/dictobject.c:L765 PyDict_New

func (*Dict) Contains

func (d *Dict) Contains(key Object) (bool, error)

Contains reports whether key is in the dict.

CPython: Objects/dictobject.c:L2495 PyDict_Contains

func (*Dict) DelItem

func (d *Dict) DelItem(key Object) error

DelItem removes key. Mirrors PyDict_DelItem.

CPython: Objects/dictobject.c:L2154 PyDict_DelItem

func (*Dict) GetItem

func (d *Dict) GetItem(key Object) (Object, error)

GetItem looks up key. Returns errKeyNotFound when absent.

CPython: Objects/dictobject.c:L1925 PyDict_GetItemWithError

func (*Dict) Len

func (d *Dict) Len() int

Len returns the number of items.

CPython: Objects/dictobject.c:L3138 PyDict_Size

func (*Dict) SetItem

func (d *Dict) SetItem(key, value Object) error

SetItem inserts or replaces key. Mirrors PyDict_SetItem.

CPython: Objects/dictobject.c:L1985 PyDict_SetItem

type Float

type Float struct {
	Header
	// contains filtered or unexported fields
}

Float is the Python float, an IEEE-754 double.

CPython: Include/cpython/floatobject.h:L7 PyFloatObject

func NewFloat

func NewFloat(x float64) *Float

NewFloat builds a float.

CPython: Objects/floatobject.c:L132 PyFloat_FromDouble

func (*Float) Float64

func (f *Float) Float64() float64

Float64 returns the underlying double.

type Header struct {
	// contains filtered or unexported fields
}

Header is the per-object header. Mirrors struct _object in cpython/Include/object.h. The zero value is invalid; constructors set refcount to 1 and install the type pointer.

CPython: Include/object.h:L107 _object

func (*Header) Hdr

func (h *Header) Hdr() *Header

Hdr returns the receiver. Implements Object for any value that embeds *Header.

func (*Header) Init added in v0.3.0

func (h *Header) Init(t *Type)

Init is the cross-package entry point to init. Out-of-package types such as the exception in errors/ embed Header and need to bind their Header.typ to a *Type without re-implementing the refcount dance.

CPython: Objects/object.c:L184 _PyObject_Init

func (*Header) Refcnt

func (h *Header) Refcnt() int64

Refcnt returns the current refcount. Test-only; production code should not depend on the exact value because Go's GC reclaims memory independently.

CPython: Include/object.h:L246 Py_REFCNT

func (*Header) Type

func (h *Header) Type() *Type

Type returns the type pointer stored in the header.

CPython: Include/object.h:L249 Py_TYPE

type Int

type Int struct {
	Header
	// contains filtered or unexported fields
}

Int is the Python int. CPython stores arbitrary-precision integers as a sign + digit array on PyLong; gopy uses math/big.Int for the same semantics. The fast-path for machine-word ints lands in v0.4 alongside the longobject.c port proper.

CPython: Include/cpython/longintrepr.h:L11 _PyLongValue

func NewInt

func NewInt(x int64) *Int

NewInt builds an int from an int64. Returns the cached singleton for the small-int range.

CPython: Objects/longobject.c:L322 PyLong_FromLong

func NewIntFromBig

func NewIntFromBig(b *big.Int) *Int

NewIntFromBig builds an int from a math/big.Int. Returns the cached singleton when the value fits the small-int window.

CPython: Objects/longobject.c:L156 _PyLong_FromBytes (adapted from)

func (*Int) BigInt

func (i *Int) BigInt() *big.Int

BigInt returns a copy of the underlying big.Int.

func (*Int) Int64

func (i *Int) Int64() (val int64, ok bool)

Int64 returns the value as int64 if it fits; ok is false otherwise.

CPython: Objects/longobject.c:L666 PyLong_AsLongAndOverflow (adapted from)

type List

type List struct {
	VarHeader
	// contains filtered or unexported fields
}

List is the Python list, a mutable ordered sequence.

CPython: Include/cpython/listobject.h:L7 PyListObject

func NewList

func NewList(items []Object) *List

NewList builds a list from items. The slice is copied.

CPython: Objects/listobject.c:L156 PyList_New

func (*List) Append

func (l *List) Append(v Object)

Append adds v to the end. Mirrors PyList_Append.

CPython: Objects/listobject.c:L351 PyList_Append

func (*List) Item

func (l *List) Item(i int) Object

Item returns the item at index i without bounds check.

CPython: Objects/listobject.c:L308 PyList_GetItem

func (*List) Len

func (l *List) Len() int

Len returns the number of items.

CPython: Objects/listobject.c:L286 PyList_Size

type MappingMethods

type MappingMethods struct {
	Length  func(o Object) (int, error)
	GetItem func(o, key Object) (Object, error)
	SetItem func(o, key, v Object) error
	DelItem func(o, key Object) error
}

MappingMethods is the v0.2 subset of tp_as_mapping.

CPython: Include/cpython/object.h:L255 PyMappingMethods

type NumberMethods

type NumberMethods struct {
	Add      func(a, b Object) (Object, error)
	Subtract func(a, b Object) (Object, error)
	Multiply func(a, b Object) (Object, error)
	Negative func(o Object) (Object, error)
	Bool     func(o Object) (bool, error)
	Int      func(o Object) (Object, error)
	Float    func(o Object) (Object, error)
}

NumberMethods is the v0.2 subset of tp_as_number. The reflected variants are handled by the abstract layer; concrete types only implement the forward direction.

CPython: Include/cpython/object.h:L195 PyNumberMethods

type Object

type Object interface {
	// Type returns the object's type. Mirrors Py_TYPE.
	Type() *Type
	// Hdr returns a pointer to the embedded Header so generic
	// runtime code can reach the refcount without knowing the
	// concrete shape. CPython reaches the same field by casting to
	// PyObject*.
	Hdr() *Header
}

Object is the interface every Python value satisfies. Concrete types embed *Header (or *VarHeader) and add their own fields.

CPython: Include/object.h:L107 PyObject (interface analog)

func False

func False() Object

False returns the False singleton. Mirrors Py_False.

CPython: Include/object.h:L857 Py_False

func NewBool

func NewBool(b bool) Object

NewBool returns the True or False singleton. Mirrors PyBool_FromLong.

CPython: Objects/boolobject.c:L33 PyBool_FromLong

func NewStr added in v0.3.0

func NewStr(s string) Object

NewStr wraps s in the v0.3 placeholder str object. The real unicodeobject port (v0.4) will replace this with a kind/length/data layout that matches CPython's compact-string representation.

CPython: Objects/unicodeobject.c:L1985 PyUnicode_FromString

func None

func None() Object

None returns the singleton None value. Mirrors Py_None.

CPython: Include/object.h:L820 Py_None

func NotImplemented

func NotImplemented() Object

NotImplemented returns the singleton NotImplemented value. Mirrors Py_NotImplemented.

CPython: Include/object.h:L863 Py_NotImplemented

func RichCmp

func RichCmp(a, b Object, op CompareOp) (Object, error)

RichCmp dispatches to tp_richcompare. Returns NotImplemented when neither operand handles the comparison. Mirrors PyObject_RichCompare.

CPython: Objects/object.c:L737 PyObject_RichCompare

func True

func True() Object

True returns the True singleton. Mirrors Py_True.

CPython: Include/object.h:L860 Py_True

type Range

type Range struct {
	Header
	Start *Int
	Stop  *Int
	Step  *Int
}

Range is the Python range object. Bounds are stored as Int so the arithmetic stays correct for the full int range.

CPython: Objects/rangeobject.c:L19 rangeobject

func NewRange

func NewRange(start, stop, step *Int) (*Range, error)

NewRange builds a range. Step must be non-zero.

CPython: Objects/rangeobject.c:L92 range_from_array

type SequenceMethods

type SequenceMethods struct {
	Length   func(o Object) (int, error)
	Concat   func(a, b Object) (Object, error)
	Repeat   func(o Object, n int) (Object, error)
	GetItem  func(o Object, i int) (Object, error)
	SetItem  func(o Object, i int, v Object) error
	Contains func(o, v Object) (bool, error)
}

SequenceMethods is the v0.2 subset of tp_as_sequence.

CPython: Include/cpython/object.h:L262 PySequenceMethods

type Slice

type Slice struct {
	Header
	Start Object
	Stop  Object
	Step  Object
}

Slice is the Python slice object: start, stop, step.

CPython: Include/cpython/sliceobject.h:L8 PySliceObject

func NewSlice

func NewSlice(start, stop, step Object) *Slice

NewSlice builds a slice. Any of start/stop/step may be None.

CPython: Objects/sliceobject.c:L165 PySlice_New

type Tuple

type Tuple struct {
	VarHeader
	// contains filtered or unexported fields
}

Tuple is the Python tuple, an immutable ordered sequence.

CPython: Include/cpython/tupleobject.h:L8 PyTupleObject

func NewTuple

func NewTuple(items []Object) *Tuple

NewTuple builds a tuple from items. The empty tuple returns the cached singleton.

CPython: Objects/tupleobject.c:L155 PyTuple_New

func (*Tuple) Item

func (t *Tuple) Item(i int) Object

Item returns the item at index i. No bounds check; callers are expected to clamp.

CPython: Objects/tupleobject.c:L319 PyTuple_GetItem

func (*Tuple) Len

func (t *Tuple) Len() int

Len returns the number of items.

CPython: Objects/tupleobject.c:L296 PyTuple_Size

type Type

type Type struct {
	Header

	Name     string
	BaseSize int
	ItemSize int

	Bases []*Type
	MRO   []*Type

	Repr     func(o Object) (string, error)
	Str      func(o Object) (string, error)
	Hash     func(o Object) (int64, error)
	RichCmp  func(a, b Object, op CompareOp) (Object, error)
	Iter     func(o Object) (Object, error)
	IterNext func(o Object) (Object, error)
	Call     func(o Object, args []Object, kwargs map[string]Object) (Object, error)
	Dealloc  func(o Object)

	Number   *NumberMethods
	Sequence *SequenceMethods
	Mapping  *MappingMethods
}

Type is the meta-class for every Python value. Slots not yet implemented in v0.2 are nil; the runtime checks for nil before dispatching.

CPython: Include/cpython/typeobject.h:L139 _typeobject

func NewType

func NewType(name string, bases []*Type) *Type

NewType constructs a built-in type with the given name and bases. Bases must be non-empty for everything except `object`. The MRO is computed via C3 linearization.

CPython: Objects/typeobject.c:L4153 type_new (adapted from)

func NoneType

func NoneType() *Type

NoneType returns the type singleton for None.

CPython: Objects/object.c:L1893 _PyNone_Type

func ObjectType

func ObjectType() *Type

ObjectType returns the root `object` type singleton. Mirrors PyBaseObject_Type.

CPython: Objects/typeobject.c:L6299 PyBaseObject_Type

func StrType added in v0.3.0

func StrType() *Type

StrType returns the type singleton for str.

CPython: Objects/unicodeobject.c:L15188 PyUnicode_Type

func TypeType

func TypeType() *Type

TypeType returns the type singleton for `type` itself. Mirrors PyType_Type.

CPython: Objects/typeobject.c:L6361 PyType_Type

type VarHeader

type VarHeader struct {
	Header
	// contains filtered or unexported fields
}

VarHeader extends Header with ob_size for variable-length builtins such as tuple, str, bytes, and int.

CPython: Include/object.h:L121 PyVarObject

func (*VarHeader) Size

func (v *VarHeader) Size() int64

Size returns ob_size. Only meaningful for VarHeader-backed types.

CPython: Include/object.h:L252 Py_SIZE

Jump to

Keyboard shortcuts

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