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 ¶
- Variables
- func Decref(o Object)
- func Hash(o Object) (int64, error)
- func Incref(o Object)
- func Is(a, b Object) bool
- func IsNone(o Object) bool
- func IsNotImplemented(o Object) bool
- func IsSubtype(sub, super *Type) bool
- func IsTruthy(o Object) (bool, error)
- func Repr(o Object) (string, error)
- func RichCmpBool(a, b Object, op CompareOp) (bool, error)
- func Str(o Object) (string, error)
- type Bool
- type CompareOp
- type Dict
- type Float
- type Header
- type Int
- type List
- type MappingMethods
- type NumberMethods
- type Object
- type Range
- type SequenceMethods
- type Slice
- type Tuple
- type Type
- type VarHeader
Constants ¶
This section is empty.
Variables ¶
var BoolType = NewType("bool", []*Type{IntType})
BoolType is the type singleton for bool. Mirrors PyBool_Type.
CPython: Objects/boolobject.c:L222 PyBool_Type
var DictType = NewType("dict", []*Type{objectType})
DictType is the type singleton for dict. Mirrors PyDict_Type.
CPython: Objects/dictobject.c:L4023 PyDict_Type
var ErrStopIteration = errors.New("StopIteration")
ErrStopIteration signals end of iteration. Mirrors PyExc_StopIteration.
CPython: Objects/exceptions.c:L1937 PyExc_StopIteration
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
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
var ListType = NewType("list", []*Type{objectType})
ListType is the type singleton for list. Mirrors PyList_Type.
CPython: Objects/listobject.c:L3380 PyList_Type
var RangeType = NewType("range", []*Type{objectType})
RangeType is the type singleton for range. Mirrors PyRange_Type.
CPython: Objects/rangeobject.c:L949 PyRange_Type
var SliceType = NewType("slice", []*Type{objectType})
SliceType is the type singleton for slice. Mirrors PySlice_Type.
CPython: Objects/sliceobject.c:L325 PySlice_Type
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 ¶
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 ¶
Is reports identity equality, the Python `is` operator. Mirrors Py_Is.
CPython: Include/object.h:L257 Py_Is
func IsNone ¶
IsNone reports whether o is the None singleton. Mirrors Py_IsNone.
CPython: Include/object.h:L827 Py_IsNone
func IsNotImplemented ¶
IsNotImplemented reports whether o is the NotImplemented singleton.
CPython: Objects/object.c:L1965 Py_IsNotImplemented
func IsSubtype ¶
IsSubtype reports whether sub is sub or one of its bases. Mirrors PyType_IsSubtype.
CPython: Objects/typeobject.c:L2556 PyType_IsSubtype
func Repr ¶
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 ¶
RichCmpBool runs RichCmp and converts the result to a Go bool. Mirrors PyObject_RichCompareBool.
CPython: Objects/object.c:L795 PyObject_RichCompareBool
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
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 ¶
Contains reports whether key is in the dict.
CPython: Objects/dictobject.c:L2495 PyDict_Contains
func (*Dict) DelItem ¶
DelItem removes key. Mirrors PyDict_DelItem.
CPython: Objects/dictobject.c:L2154 PyDict_DelItem
func (*Dict) GetItem ¶
GetItem looks up key. Returns errKeyNotFound when absent.
CPython: Objects/dictobject.c:L1925 PyDict_GetItemWithError
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
type Header ¶
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) Init ¶ added in v0.3.0
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
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 ¶
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 ¶
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)
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 ¶
NewList builds a list from items. The slice is copied.
CPython: Objects/listobject.c:L156 PyList_New
func (*List) Append ¶
Append adds v to the end. Mirrors PyList_Append.
CPython: Objects/listobject.c:L351 PyList_Append
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 ¶
NewBool returns the True or False singleton. Mirrors PyBool_FromLong.
CPython: Objects/boolobject.c:L33 PyBool_FromLong
func NewStr ¶ added in v0.3.0
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
type Range ¶
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
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 ¶
Slice is the Python slice object: start, stop, step.
CPython: Include/cpython/sliceobject.h:L8 PySliceObject
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 ¶
NewTuple builds a tuple from items. The empty tuple returns the cached singleton.
CPython: Objects/tupleobject.c:L155 PyTuple_New
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 ¶
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