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
assertion failure in random.seed() in case the seed argument has a bad __abs__() method #75659
Comments
The following code causes an assertion failure: import random
random.seed(BadInt()) this is because random_seed() (in Modules/_randommodule.c) assumes that what should we do in such a case? |
It would make sense to verify that an actual int was returned and to raise a TypeError if it wasn't. Do you want to submit a PR (with a test case)? |
sure. |
There are several ways of solving this issue:
|
i opened a PR that implements the first option, but of course I wouldn't |
So floats (and complexes) cannot be seeds anymore? :-o Or this pertains only to ints? In this case, I think the easiest doc fix is to change If a is an int, it is used directly. to If a is an int, its absolute value is used. |
The more I think about this the more I like the idea of using int.__abs__() directly (PyLong_Type.tp_as_number->nb_absolute() in C). The C code doesn't call potentially overridable methods bit_length() and to_bytes(), it uses concrete implementations directly. I don't see reasons why it should obey overriding the __abs__() method. This will save us from the problem with the wording of the error message. I mentioned a drawback, but the current implementation has the same drawback. We can avoid copying positive integer subtypes by using more complex code, but I think it isn't worth. |
I concur. |
With regard to backporting to 2.7: In 2.7 also, PyNumber_Absolute() is called, and its return value is stored in
So I think a backport is still desirable, but i am not sure about the test. Maybe we should use @cpython_only, and make sure that no error is raised? What do you think? |
I agree that a backport is still desirable. Your plan LGTM. Implement __abs__ raising an exception (test both int ant long subclasses). Make sure that no error is raised. Make sure that random() returns the same value as when seeding with an exact int and long. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: