-
-
Notifications
You must be signed in to change notification settings - Fork 30.8k
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-33073: Adding as_integer_ratio to ints. #8750
Changes from 1 commit
ff0790e
d1daadc
3b89b6f
48c7dd0
4ca566a
2336cb4
673a0ca
5102589
a935a7b
3cd076c
028417a
2028d97
89a0d9d
148225c
ac0f11f
cb9c978
d311b83
d3ef843
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1349,6 +1349,11 @@ def test_shift_bool(self): | |
self.assertEqual(type(value << shift), int) | ||
self.assertEqual(type(value >> shift), int) | ||
|
||
def test_as_integer_ratio(self): | ||
tests = [10, 0, -10, 1, 3] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why so much similar cases are needed? Add tests for booleans and other int subclasses. Check types of numerator and denominator. |
||
for value in tests: | ||
self.assertEqual((value).as_integer_ratio(), (value, 1)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: no parens needed around |
||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Added as_integer_ratio to ints to make them more interoperable with floats. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5260,6 +5260,40 @@ long_is_finite(PyObject *v) | |
} | ||
#endif | ||
|
||
/*[clinic input] | ||
int.as_integer_ratio | ||
|
||
Return integer ratio. | ||
|
||
Return a pair of integers, whose ratio is exactly equal to the original int | ||
and with a positive denominator. | ||
|
||
Raise OverflowError on infinities and a ValueError on NaNs. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. An int can never be any of these things - remove this line There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. |
||
|
||
>>> (10).as_integer_ratio() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need so much examples in a docstring? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's useful to have at least one positive and one negative example, so that it's obvious at a glance that the behaviour for negatives is to give (for example) |
||
(10, 1) | ||
>>> (0).as_integer_ratio() | ||
(0, 1) | ||
>>> (11).as_integer_ratio() | ||
(11, 1) | ||
>>> (-10).as_integer_ratio() | ||
(-10, 1) | ||
[clinic start generated code]*/ | ||
|
||
static PyObject * | ||
int_as_integer_ratio_impl(PyObject *self) | ||
/*[clinic end generated code: output=e60803ae1cc8621a input=ce9c7768a1287fb9]*/ | ||
{ | ||
PyObject *denominator = NULL; | ||
PyObject *result_pair = NULL; | ||
|
||
denominator = PyLong_FromLong(1); | ||
result_pair = PyTuple_Pack(2, self, denominator); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the whole function can be replaced with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @pablogsal But PyLong_FromLong always receive the 1. How could it fail? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The contract of |
||
|
||
Py_DECREF(denominator); | ||
return result_pair; | ||
} | ||
|
||
/*[clinic input] | ||
int.to_bytes | ||
|
||
|
@@ -5392,6 +5426,7 @@ static PyMethodDef long_methods[] = { | |
#endif | ||
INT_TO_BYTES_METHODDEF | ||
INT_FROM_BYTES_METHODDEF | ||
INT_AS_INTEGER_RATIO_METHODDEF | ||
{"__trunc__", long_long_meth, METH_NOARGS, | ||
"Truncating an Integral returns itself."}, | ||
{"__floor__", long_long_meth, METH_NOARGS, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A
versionadded
directive needs to be added. Also, the developer's guide states that reST files should use an indentation of 3 spaces.