diff options
author | Lisa Roach <lisaroach14@gmail.com> | 2018-09-14 06:56:23 (GMT) |
---|---|---|
committer | Raymond Hettinger <rhettinger@users.noreply.github.com> | 2018-09-14 06:56:23 (GMT) |
commit | 5ac704306f4b81ae3f28d8742408d3214b145e8a (patch) | |
tree | 34a39f0216d918b679367a84e05f2326aef70a7c /Objects | |
parent | 83df50ea5757816c7338d27f21fd18b1e79206f7 (diff) | |
download | cpython-5ac704306f4b81ae3f28d8742408d3214b145e8a.zip cpython-5ac704306f4b81ae3f28d8742408d3214b145e8a.tar.gz cpython-5ac704306f4b81ae3f28d8742408d3214b145e8a.tar.bz2 |
bpo-33073: Adding as_integer_ratio to ints. (GH-8750)
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/clinic/longobject.c.h | 30 | ||||
-rw-r--r-- | Objects/longobject.c | 31 |
2 files changed, 60 insertions, 1 deletions
diff --git a/Objects/clinic/longobject.c.h b/Objects/clinic/longobject.c.h index 14f5515..0e70fe5 100644 --- a/Objects/clinic/longobject.c.h +++ b/Objects/clinic/longobject.c.h @@ -118,6 +118,34 @@ int_bit_length(PyObject *self, PyObject *Py_UNUSED(ignored)) return int_bit_length_impl(self); } +PyDoc_STRVAR(int_as_integer_ratio__doc__, +"as_integer_ratio($self, /)\n" +"--\n" +"\n" +"Return integer ratio.\n" +"\n" +"Return a pair of integers, whose ratio is exactly equal to the original int\n" +"and with a positive denominator.\n" +"\n" +">>> (10).as_integer_ratio()\n" +"(10, 1)\n" +">>> (-10).as_integer_ratio()\n" +"(-10, 1)\n" +">>> (0).as_integer_ratio()\n" +"(0, 1)"); + +#define INT_AS_INTEGER_RATIO_METHODDEF \ + {"as_integer_ratio", (PyCFunction)int_as_integer_ratio, METH_NOARGS, int_as_integer_ratio__doc__}, + +static PyObject * +int_as_integer_ratio_impl(PyObject *self); + +static PyObject * +int_as_integer_ratio(PyObject *self, PyObject *Py_UNUSED(ignored)) +{ + return int_as_integer_ratio_impl(self); +} + PyDoc_STRVAR(int_to_bytes__doc__, "to_bytes($self, /, length, byteorder, *, signed=False)\n" "--\n" @@ -211,4 +239,4 @@ int_from_bytes(PyTypeObject *type, PyObject *const *args, Py_ssize_t nargs, PyOb exit: return return_value; } -/*[clinic end generated code: output=fd64beb83bd16df3 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=6d5e92d7dc803751 input=a9049054013a1b77]*/ diff --git a/Objects/longobject.c b/Objects/longobject.c index 399d354..98ff9a8 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -5261,6 +5261,36 @@ 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. + +>>> (10).as_integer_ratio() +(10, 1) +>>> (-10).as_integer_ratio() +(-10, 1) +>>> (0).as_integer_ratio() +(0, 1) +[clinic start generated code]*/ + +static PyObject * +int_as_integer_ratio_impl(PyObject *self) +/*[clinic end generated code: output=e60803ae1cc8621a input=55ce3058e15de393]*/ +{ + if PyLong_CheckExact(self) { + return PyTuple_Pack(2, self, _PyLong_One); + } else { + PyObject *numerator = _PyLong_Copy(self); + PyObject *ratio_tuple = PyTuple_Pack(2, numerator, _PyLong_One); + Py_DECREF(numerator); + return ratio_tuple; + } +} + +/*[clinic input] int.to_bytes length: Py_ssize_t @@ -5392,6 +5422,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, |