Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
6b52ec3
WIP librt.random
JukkaL Mar 21, 2026
861b10f
Add random()
JukkaL Mar 21, 2026
4051186
Add ChaCha8 RNG
JukkaL Mar 21, 2026
1fb4997
Add module-level functions
JukkaL Mar 21, 2026
4518d40
Add randrange
JukkaL Mar 21, 2026
474c625
Support explicit seeds
JukkaL Mar 21, 2026
34dd760
Use i64 as integer type
JukkaL Mar 21, 2026
fec5583
Add randbits62()
JukkaL Mar 21, 2026
db1e2d4
Add few Random primitives
JukkaL Mar 21, 2026
dc3b904
Add randbits62() primitive
JukkaL Mar 21, 2026
22da84a
Unrelated: fix librt optimized build
JukkaL Mar 21, 2026
f7df2e8
Add primitive for random()
JukkaL Mar 21, 2026
00c2e85
Addd randbits31()
JukkaL Mar 21, 2026
a0095a8
Primitives for randint and randrange
JukkaL Mar 21, 2026
c836447
Add more module level function primitives
JukkaL Mar 21, 2026
9d82d78
Add more primitives
JukkaL Mar 21, 2026
65109ce
Handle larger ranges better and improve error handling
JukkaL Mar 21, 2026
af12b54
Improve quality of random numbers
JukkaL Mar 21, 2026
9703fdb
Add missing file
JukkaL Apr 11, 2026
52d1406
Update api definition to match current approach
JukkaL May 7, 2026
cbe4c4b
Drop randbits31 and ranbits62 as low value
JukkaL May 7, 2026
4841850
Add missing #include
JukkaL May 7, 2026
d6a3e5b
Fixes to large integer ranges
JukkaL May 7, 2026
11b937f
Make Random final
JukkaL May 7, 2026
125bdee
Improve error handling
JukkaL May 7, 2026
0fde34c
Avoid the use of 128 bit integers for better portability
JukkaL May 7, 2026
2e2925e
Minor fixes
JukkaL May 7, 2026
062fa12
Lint
JukkaL May 7, 2026
99fb114
Refactor tests
JukkaL May 8, 2026
5a58dfc
Test calls using Any
JukkaL May 8, 2026
17c2b17
Make get_thread_rng inline function
JukkaL May 8, 2026
4137427
Don't put it behind experimental feature flag
JukkaL May 8, 2026
bd432db
Address code review
JukkaL May 8, 2026
0124255
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 8, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions mypy/typeshed/stubs/librt/librt/random.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from typing import final, overload

from mypy_extensions import i64

def random() -> float: ...
def randint(a: i64, b: i64) -> i64: ...
@overload
def randrange(stop: i64, /) -> i64: ...
@overload
def randrange(start: i64, stop: i64, /) -> i64: ...
def seed(n: i64, /) -> None: ...

@final
class Random:
def __init__(self, seed: i64 | None = None) -> None: ...
def randint(self, a: i64, b: i64) -> i64: ...
@overload
def randrange(self, stop: i64, /) -> i64: ...
@overload
def randrange(self, start: i64, stop: i64, /) -> i64: ...
def random(self) -> float: ...
def seed(self, n: i64, /) -> None: ...
4 changes: 4 additions & 0 deletions mypyc/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ class ModDesc(NamedTuple):
["vecs"],
),
ModDesc("librt.time", ["time/librt_time.c"], ["time/librt_time.h"], []),
ModDesc("librt.random", ["random/librt_random.c"], ["random/librt_random.h"], ["random"]),
]

try:
Expand Down Expand Up @@ -631,6 +632,9 @@ def get_cflags(
# Disables C Preprocessor (cpp) warnings
# See https://github.com/mypyc/mypyc/issues/956
"-Wno-cpp",
"-Wno-array-bounds",
"-Wno-stringop-overread",
"-Wno-stringop-overflow",
]
if log_trace:
cflags.append("-DMYPYC_LOG_TRACE")
Expand Down
5 changes: 5 additions & 0 deletions mypyc/codegen/emitmodule.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
from mypyc.errors import Errors
from mypyc.ir.deps import (
LIBRT_BASE64,
LIBRT_RANDOM,
LIBRT_STRINGS,
LIBRT_TIME,
LIBRT_VECS,
Expand Down Expand Up @@ -1224,6 +1225,10 @@ def emit_module_exec_func(
emitter.emit_line("if (import_librt_vecs() < 0) {")
emitter.emit_line("return -1;")
emitter.emit_line("}")
if LIBRT_RANDOM in module.dependencies:
emitter.emit_line("if (import_librt_random() < 0) {")
emitter.emit_line("return -1;")
emitter.emit_line("}")
emitter.emit_line("PyObject* modname = NULL;")
if self.multi_phase_init:
emitter.emit_line(f"{module_static} = module;")
Expand Down
1 change: 1 addition & 0 deletions mypyc/ir/deps.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ def get_header(self) -> str:
LIBRT_BASE64: Final = Capsule("librt.base64")
LIBRT_VECS: Final = Capsule("librt.vecs")
LIBRT_TIME: Final = Capsule("librt.time")
LIBRT_RANDOM: Final = Capsule("librt.random")

BYTES_EXTRA_OPS: Final = SourceDep("bytes_extra_ops.c")
BYTES_WRITER_EXTRA_OPS: Final = SourceDep("byteswriter_extra_ops.c")
Expand Down
7 changes: 6 additions & 1 deletion mypyc/ir/rtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class to enable the new behavior. In rare cases, adding a new
from typing import TYPE_CHECKING, ClassVar, Final, Generic, TypeGuard, TypeVar, Union, final

from mypyc.common import HAVE_IMMORTAL, IS_32_BIT_PLATFORM, PLATFORM_SIZE, JsonDict, short_name
from mypyc.ir.deps import LIBRT_STRINGS, LIBRT_VECS, Dependency
from mypyc.ir.deps import LIBRT_RANDOM, LIBRT_STRINGS, LIBRT_VECS, Dependency
from mypyc.namegen import NameGenerator

if TYPE_CHECKING:
Expand Down Expand Up @@ -544,10 +544,15 @@ def __hash__(self) -> int:
("librt.strings.BytesWriter", (LIBRT_STRINGS,)),
("librt.strings.StringWriter", (LIBRT_STRINGS,)),
]
} | {
"librt.random.Random": RPrimitive(
"librt.random.Random", is_unboxed=False, is_refcounted=True, dependencies=(LIBRT_RANDOM,)
)
}

bytes_writer_rprimitive: Final = KNOWN_NATIVE_TYPES["librt.strings.BytesWriter"]
string_writer_rprimitive: Final = KNOWN_NATIVE_TYPES["librt.strings.StringWriter"]
random_rprimitive: Final = KNOWN_NATIVE_TYPES["librt.random.Random"]


def is_native_rprimitive(rtype: RType) -> bool:
Expand Down
Loading
Loading