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-31478: Fix an assertion failure in random.seed() in case the 'a' arg has a bad __abs__() method #3596

Merged
merged 5 commits into from
Sep 28, 2017

Conversation

orenmn
Copy link
Contributor

@orenmn orenmn commented Sep 15, 2017

  • in _randommodule.c: add a check whether PyNumber_Absolute() hasn't returned an integer.
  • in test_random.py: add a test to verify that the assertion failure is no more.

https://bugs.python.org/issue31478

}
if (!PyLong_Check(n)) {
PyErr_Format(PyExc_TypeError,
"abs(a) must return an integer, not '%.200s'",
Copy link
Member

Choose a reason for hiding this comment

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

abs(seed)?

Copy link
Contributor Author

@orenmn orenmn Sep 15, 2017

Choose a reason for hiding this comment

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

i agree this seems weird, but that's what the docs say (https://docs.python.org/3.7/library/random.html#random.seed).
should i change to abs(seed) anyway? (or should i also update the docs?)

Copy link
Member

Choose a reason for hiding this comment

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

The doc is about random.Random.seed(). This is _random.Random.seed(). It doesn't have named arguments. I don't know what is the best wording, but mentioning a whose name is arbitrary and not related to this function looks incorrect to me.

I leave up this to @rhettinger.

@@ -0,0 +1,2 @@
Fix an assertion failure in `random.seed()` in case the `a` argument has a
Copy link
Member

Choose a reason for hiding this comment

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

What is `a`?

class BadInt(int):
def __abs__(self):
return None
with self.assertRaises(TypeError):
self.gen.seed(BadInt())
self.gen.seed(BadInt())
Copy link
Member

Choose a reason for hiding this comment

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

I think that if wrap this with try: ... except TypeError: pass, the cpython_only decorator can be removed.

Py_TYPE(n)->tp_name);
goto Done;
}
n = PyLong_Type.tp_as_number->nb_absolute(arg);
Copy link
Member

Choose a reason for hiding this comment

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

Please add a short comment explaining why this is written in such way.

@serhiy-storchaka
Copy link
Member

@rhettinger, if you don't have comments I'll merge this PR.

@serhiy-storchaka serhiy-storchaka self-assigned this Sep 26, 2017
@serhiy-storchaka serhiy-storchaka added needs backport to 2.7 type-bug An unexpected behavior, bug, or error labels Sep 26, 2017
@serhiy-storchaka serhiy-storchaka merged commit d780b2d into python:master Sep 28, 2017
@miss-islington
Copy link
Contributor

Thanks @orenmn for the PR, and @serhiy-storchaka for merging it 🌮🎉.. I'm working now to backport this PR to: 2.7, 3.6.
🐍🍒⛏🤖

@miss-islington
Copy link
Contributor

Sorry @orenmn and @serhiy-storchaka, I had trouble checking out the 3.6 backport branch.
Please backport using cherry_picker on command line.
cherry_picker d780b2d588e68bd7047ef5d1f04e36da38b7a350 3.6

@miss-islington
Copy link
Contributor

Sorry, @orenmn and @serhiy-storchaka, I could not cleanly backport this to 2.7 due to a conflict.
Please backport using cherry_picker on command line.
cherry_picker d780b2d588e68bd7047ef5d1f04e36da38b7a350 2.7

serhiy-storchaka pushed a commit to serhiy-storchaka/cpython that referenced this pull request Sep 28, 2017
…seed has a bad __abs__() method. (pythonGH-3596)

(cherry picked from commit d780b2d)
@bedevere-bot
Copy link

GH-3794 is a backport of this pull request to the 3.6 branch.

@serhiy-storchaka
Copy link
Member

Would you mind to create a backport to the 2.7 branch @orenmn?

@orenmn
Copy link
Contributor Author

orenmn commented Sep 28, 2017

sure

serhiy-storchaka added a commit that referenced this pull request Sep 28, 2017
…seed has a bad __abs__() method. (GH-3596) (#3794)

(cherry picked from commit d780b2d)
@orenmn
Copy link
Contributor Author

orenmn commented Oct 1, 2017

The behavior is different in 2.7, so i wasn't sure how to proceed, and asked for advice in https://bugs.python.org/issue31478#msg303207..

@bedevere-bot
Copy link

GH-3845 is a backport of this pull request to the 2.7 branch.

serhiy-storchaka pushed a commit that referenced this pull request Oct 2, 2017
@serhiy-storchaka serhiy-storchaka removed their assignment Dec 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants