picop.cybytes#

Fast bytes accessors.

class picop.cybytes.BytesWriter#

Bases: object

Incremental bytes builder over CPython’s writer.

max_size is a capacity hint, honoured on CPython 3.8-3.14 (internal _PyBytesWriter) and on 3.15+ (PyBytesWriter_Create + PyBytesWriter_Resize(writer, 0) reserve capacity without any pre-written content).

size() reports bytes written; grow(), data() and finish_with_size() are cimport-only direct-fill helpers.

__enter__()#

Return the writer.

Return type:

BytesWriter

__exit__(exc, value, tb)#

Discard the writer if it is still open.

Return type:

bool

discard()#

Release the writer’s buffer without producing a result.

Return type:

None

finish()#

Return the built bytes and close the writer.

Return type:

bytes

size()#

Return the number of bytes written so far.

Return type:

int

write(data)#

Append data (bytes or bytearray) to the writer.

Parameters:

data (bytes | bytearray)

Return type:

None

write_char(ch)#

Append a single byte 0 <= ch <= 255.

Parameters:

ch (int)

Return type:

None

picop.cybytes.bytes_bytearray_eq(a, b)#

Return True if bytes/bytearray contents match.

Notes

Either argument order is accepted; compares with memcmp after a length gate.

Parameters:
Return type:

bool

picop.cybytes.bytes_check(p)#

Return True if p is bytes or a subtype (PyBytes_Check).

Parameters:

p (object)

Return type:

bool

picop.cybytes.bytes_check_exact(p)#

Return True if type(p) is bytes (PyBytes_CheckExact).

Parameters:

p (object)

Return type:

bool

picop.cybytes.bytes_contains(haystack, needle)#

Return True if needle is in haystack.

Notes

Uses memchr/memmem under 256B, else the builtin in path.

Parameters:
Return type:

bool

picop.cybytes.bytes_endswith(s, suffix)#

Return True if typed s ends with suffix.

Notes

Length gate plus tail memcmp on the typed suffix region.

Parameters:
Return type:

bool

picop.cybytes.bytes_eq(a, b)#

Return True if a == b.

Notes

Identity/len short-circuit plus memcmp on typed bytes.

Parameters:
Return type:

bool

picop.cybytes.bytes_from_object(o)#

Return bytes from a buffer-protocol object (PyBytes_FromObject).

Parameters:

o (object)

Return type:

bytes

picop.cybytes.bytes_len(b)#

Return len(b) via PyBytes_GET_SIZE.

Parameters:

b (bytes)

Return type:

int

picop.cybytes.bytes_ne(a, b)#

Return True if a != b (inverse of bytes_eq).

Parameters:
Return type:

bool

picop.cybytes.bytes_size(b)#

Return len(b) via checked PyBytes_Size.

Notes

Prefer bytes_len on a typed bytes hot path.

Parameters:

b (object)

Return type:

int

picop.cybytes.bytes_startswith(s, prefix)#

Return True if typed s begins with prefix.

Notes

Length gate plus memcmp on the typed prefix region.

Parameters:
Return type:

bool