Skip to content
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

bpo-42008: Fix _random.Random() seeding #22668

Merged

Conversation

amiremohamadi
Copy link
Contributor

@amiremohamadi amiremohamadi commented Oct 12, 2020

calling random_seed() with first element of the args tuple.

https://bugs.python.org/issue42008

Copy link
Member

@Fidget-Spinner Fidget-Spinner left a comment

Overall, this looks very good to me, other than a few things I pointed out in the reviews :)

Py_DECREF(self);
return NULL;

if (PyTuple_GET_SIZE(args) == 1) {
Copy link
Member

@Fidget-Spinner Fidget-Spinner Oct 12, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct me if I'm wrong, but afaik, random_new is called with args of _random.Random's init, which can contain more than 1 argument. So it might be more robust to check for len(args) > 0 instead.

>>> import random
>>> random.Random(1,2,3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Random.__init__() takes from 1 to 2 positional arguments but 3 were given
>>>
>>> import _random
>>> _random.Random(1,2,3)
>>> # no error
Suggested change
if (PyTuple_GET_SIZE(args) == 1) {
if (PyTuple_GET_SIZE(args) > 0) {

Copy link
Contributor Author

@amiremohamadi amiremohamadi Oct 12, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Fidget-Spinner thanks for the review!
I think, in previous versions (e.g. 3.6) it just accepts one argument. So I'm not sure if it is a good behavior for random_new to accept more arguments or not! (I think, it should raise an exception in case of being called with 2 or more args)
After this commit it is allowed to pass more arguments (don't know it's a bug or feature).
Dear @rhettinger, do you have any idea about it?

Copy link
Contributor

@rhettinger rhettinger Oct 19, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only accept one argument. If there are more, raise a TypeError.

@rhettinger rhettinger self-assigned this Oct 12, 2020
@amiremohamadi
Copy link
Contributor Author

amiremohamadi commented Oct 19, 2020

Changed the code to only accept 0 or 1 argument.

Also if random_new is called without any arguments, it should call random_seed with NULL or Py_None to use current time as seed but after _randommodule was converted to argument clinic, random_new always called random_seed with a tuple as argument, even if it doesn't have any arguments which means if you try this:

r = _random.Random()

it uses an empty tuple as seed (not current time).

This problem also solved by this PR.

@github-actions
Copy link

github-actions bot commented Dec 17, 2020

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions bot added the stale Stale PR or inactive for long period of time. label Dec 17, 2020
@rhettinger
Copy link
Contributor

rhettinger commented Dec 19, 2020

Can you please resolve the branch conflict.

@github-actions github-actions bot removed the stale Stale PR or inactive for long period of time. label Dec 20, 2020
@amiremohamadi
Copy link
Contributor Author

amiremohamadi commented Dec 20, 2020

Sure

@amiremohamadi
Copy link
Contributor Author

amiremohamadi commented Dec 20, 2020

Actually I think Azure Pipeline failure is not related to my changes

@rhettinger rhettinger merged commit b8fde8b into python:master Dec 21, 2020
2 of 3 checks passed
adorilson pushed a commit to adorilson/cpython that referenced this pull request Mar 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants