vec_norm2(v: Vec) -> int#
The squared length of v (a Vec, or a subclass).
setu_class' module functions: every parameter type Setu converts, as Python sees it.
vec_norm2vec_scalevec_selflabel_ofbuffer_sumbuffer_lenbuffer_fillreadseq_sumseq_joinseq_norm2seq_buffer_lenholdownowned_sumbench_nopbench_recvbench_recv_takebench_recv_ownedbench_sendmake_secretsecret_valuecheck_errnoraise_os_errornot_implementedis_intpath_bytespaths_lenkeep_pathsmall_sumraise_longno_memoryint_pairtext_listtext_tupleThe squared length of v (a Vec, or a subclass).
Multiplies the coordinates of v by k, in place.
# a second block: a new namespace, so no `v` >>> w = Vec(1, -2) >>> vec_scale(w, -1) >>> w Vec(-1, 2)
v itself: the object back from its instance data (VecType.object).
return |
Vec |
The label of v, or None without one (v is None or not given).
return |
object |
The sum of the bytes of data (any bytes-like object).
>>> buffer_sum(b'abc') 294 >>> buffer_sum(memoryview(b'abc')[1:]) 197
The length of data, or None without one.
>>> buffer_len(bytearray(5)) 5 >>> buffer_len(None)
Sets every byte of buf (a writable bytes-like object) to value; returns how many.
>>> b = bytearray(3) >>> buffer_fill(b, 7) 3 >>> b bytearray(b'\x07\x07\x07')
Reads up to n bytes (default: all of buf) from fd into buf at offset; returns how many.
>>> import os >>> fd = os.open('a.txt', os.O_RDWR | os.O_CREAT) >>> os.write(fd, b'hello world') 11 >>> b = bytearray(8) >>> read(fd, b, 4, offset=6) 4 >>> b bytearray(b'worl\x00\x00\x00\x00') >>> os.close(fd)
The sum of values (a list, tuple, range, ... of u32s).
parts joined: b''.join(parts).
return |
bytes |
>>> seq_join([b'ab', b'c']) b'abc'
The squared lengths of vs (Vecs), added up.
The total length of bufs (bytes-like objects), or None without them.
>>> seq_buffer_len([b'ab', bytearray(3)]) 5
Releases what the last call kept, then keeps buf, data and bufs (take): locked until the next call. hold() only releases.
>>> b = bytearray(3) >>> hold(b) >>> b.append(1) BufferError: Existing exports of data: object cannot be re-sized >>> hold() >>> b.append(1)
Releases what the last call kept, then keeps buf and data as they come (py.OwnedWritableBuffer / py.OwnedBuffer, no second view): locked until the next call. own() only releases; with drop=True the function releases them itself before returning.
>>> b = bytearray(3) >>> own(b) >>> b.append(1) BufferError: Existing exports of data: object cannot be re-sized >>> own() >>> b.append(1)
The sum of the bytes of data, kept by the call (py.OwnedBuffer) and released by it.
>>> owned_sum(b'abc') 294
v converted, nothing else: the cheapest call for tool/bench.py (Liburing's io_uring_prep_nop(sqe)).
The arguments of Liburing's io_uring_prep_recv(sqe, sockfd, buf, flags) (https://man7.org/linux/man-pages/man3/io_uring_prep_recv.3.html), converted and released after the call (tool/bench.py); returns fd.
>>> bench_recv(Vec(), -1, bytearray(4), 0) -1
K_ONE the first K_ constant, linked like ANSWERK_TWO (default)0 none, see https://man7.org/linux/man-pages/man2/recv.2.html.bench_recv, keeping buf until the next call with a second view (take); buf=None only releases (tool/bench.py).
>>> bench_recv_take(Vec(), -1, bytearray(4)) -1
bench_recv, keeping buf until the next call as it comes (py.OwnedWritableBuffer); buf=None only releases (tool/bench.py).
>>> bench_recv_owned(Vec(), -1, bytearray(4)) -1
The arguments of Liburing's io_uring_prep_send(sqe, sockfd, buf, flags), converted and released after the call (tool/bench.py); returns fd.
>>> bench_send(Vec(), -1, b'abc') -1
A Secret (a hidden class) holding value.
>>> type(make_secret(3)).__name__ 'Secret'
The value of s (a Secret, Any in the stub).
>>> secret_value(make_secret(3)) 3
res itself when it is >= 0, else OSError for the errno -res (py.checkErrno).
>>> check_errno(3) 3 >>> check_errno(-2) FileNotFoundError: [Errno 2] No such file or directory
Raises OSError(errno, msg) with a message of Setu's (py.raiseOSError): CPython picks the subclass.
>>> raise_os_error(17) FileExistsError: [Errno 17] errno 17 from Setu
Raises NotImplementedError naming what (py.raiseNotImplementedError).
>>> not_implemented('spin') NotImplementedError: `spin` is not implemented
Is obj an int (bool too, a subclass): True / False from py.trueObj() / py.falseObj().
obj |
object |
|
return |
bool |
path encoded like os.fsencode (py.Path).
return |
bytes |
>>> path_bytes('/tmp/x') b'/tmp/x' >>> path_bytes(b'/tmp') b'/tmp' >>> path_bytes(3) TypeError: expected str, bytes or os.PathLike object, not int
The total length of paths, each encoded like os.fsencode (py.Sequence(py.Path)).
>>> paths_len(['/a', b'/bc']) 5
Releases the path the last call kept, then keeps path (Path.take). keep_path() only releases.
obj converted by hand: a u8 (py.fromPython), or the sum of a sequence of them (py.Sequence(u8).from), as an i8 (py.narrow).
obj |
int | Sequence[int] |
>>> small_sum(5) 5 >>> small_sum([100, 27]) 127 >>> small_sum([100, 28]) OverflowError: `small_sum(obj)` = 128 is out of range -128..127 >>> small_sum(300) OverflowError: 300 is out of range 0..255
Raises ValueError(text) through py.raise (a formatted message): a text longer than 511 bytes is cut to the first 511.
>>> raise_long('x' * 3) ValueError: xxx
Fails like a Zig allocation: MemoryError, from py.raiseNoMemory(), or with zig_error from returning error.OutOfMemory.
big and small back: an i64 takes the fast path, a larger u64 the one past it.
>>> int_pair(2**64 - 1, -128) (18446744073709551615, -128) >>> int_pair(-1, 0) OverflowError: `int_pair(big)` = -1 is out of range 0..18446744073709551615
['a', 'b', 'c']; with bad the middle text is not UTF-8, so making the list fails midway.
>>> text_list(False) ['a', 'b', 'c'] >>> text_list(True) UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte
(first, text, last), text decoded as UTF-8: invalid UTF-8 fails the tuple midway, which drops last (a new reference not yet in it).
first |
object |
|
last |
object |
|
return |
tuple[object, str, object] |
>>> text_tuple(1, b'x', 2) (1, 'x', 2) >>> text_tuple(1, b'\xff', 2) UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte