-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
bpo-40346: Add random.BaseRandom #19631
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
I'm not sure if cc @pitrou |
Ah, I checked: it works as expected :-) But it would be worth it to add tests on class inheritance, to check which method is called ;-) |
TODO:
Example of subclass (it doesn't handle gauss_next):
|
Oh, in fact gauss_next only looks like an optimization. I moved it from BaseRandom to Random. BaseRandom.gauss() is now thread state. Random getstate() and setstate() don't have to bother with an attribute which came from its parent class. |
BaseRandom implements random() and randbytes() using getrandbits(). It has no state and its gauss() method is thread safe. It has no VERSION attribute and its seed() method has no version parameter. The implementation of random.Random, random.SystemRandom and random.Random subclasses are not affected by this change. Changes: * random.SystemRandom now inherits from BaseRandom instead of Random. It no longer inherits from _random.Random. An instance now only takes 48 bytes of memory, rather than 2568 bytes (on x86-64). * Move random() method implementation from SystemRandom to BaseRandom. random.Radom overrides it with _random.Random.random(). * Move the gauss_next attribute of the gauss() method optimization to random.Random class. * Mark randbytes() parameter as positional-only.
I rebased my PR and squashed commits to be able to edit the commit message. New changes:
|
@serhiy-storchaka, @tim-one: Ok, the PR is now ready for a review. Sorry, earlier versions of the PR were not complete. It was more complex than expected to implement this new base class. |
I abandon my this PR and my PR #19700 since there is no clear consensus on these changes: see https://bugs.python.org/issue40346 I wrote PR #19797 instead: "Remove C implementation of Random.randbytes()". |
BaseRandom implements random() and randbytes() using getrandbits().
It has no state and its gauss() method is thread safe. It has no
VERSION attribute and its seed() method has no version parameter.
The implementation of random.Random, random.SystemRandom and
random.Random subclasses are not affected by this change.
Changes:
It no longer inherits from _random.Random. An instance now only
takes 48 bytes of memory, rather than 2568 bytes (on x86-64).
BaseRandom. random.Radom overrides it with _random.Random.random().
random.Random class.
https://bugs.python.org/issue40346