| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
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
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 | ||||
|---|---|---|---|---|---|---|
| Expand Up | @@ -3306,9 +3306,87 @@ math_comb_impl(PyObject *module, PyObject *n, PyObject *k) | |||||
| } | ||||||
|
|
||||||
|
|
||||||
| /*[clinic input] | ||||||
| math._as_integer_ratio | ||||||
| x: object | ||||||
| / | ||||||
|
|
||||||
| Return integer ratio. | ||||||
|
|
||||||
| Return a pair of integers, whose ratio is exactly equal to the original | ||||||
|
Comment thread
Copy link
Copy Markdown
Contributor
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityCan we specify that the pair of integers returned are contained within a tuple?
Suggested change
Sorry, something went wrong.
All reactions
Copy link
Copy Markdown
Member
Author
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityThis was copied from the docstring of float.as_integer_ratio. Other as_integer_ratio methods have similar wording.
Sorry, something went wrong.
All reactions
Copy link
Copy Markdown
Contributor
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityThis suggestion was based on the docstring Raymond recently created for Fraction.as_integer_ratio(), I made a similar suggestion that he added to the PR. If it would be helpful, I could create a separate PR to make a similar change to float.as_integer_ratio().
Sorry, something went wrong.
All reactions
Copy link
Copy Markdown
Contributor
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityThe tuple is the pair, so I find "tuple containing a pair" a bit confusing. That almost sounds like ((numerator, denominator),) or something. Why not be explicit and write "a tuple (numerator, denominator)"?
Sorry, something went wrong.
All reactions
Copy link
Copy Markdown
Contributor
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality
That would be an improvement. I mostly just wanted to specify that the function returned a tuple.
Sorry, something went wrong.
All reactions
Copy link
Copy Markdown
Member
Author
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality"Pair" is a synonym of 2-element tuple. If you think that this term is incorrect, please open a separate issue and analyze all uses of in in the code and in the documentation (there are a lot of occurrences).
Sorry, something went wrong.
All reactions
Copy link
Copy Markdown
Contributor
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality
Either is probably fine, I just figured it was worth trying to be more technically descriptive. Thanks.
Sorry, something went wrong.
All reactions
|
||||||
| number and with a positive denominator. | ||||||
| [clinic start generated code]*/ | ||||||
|
|
||||||
| static PyObject * | ||||||
| math__as_integer_ratio(PyObject *module, PyObject *x) | ||||||
| /*[clinic end generated code: output=2e4f43d93f6e7850 input=b54b48dd6bbe22ea]*/ | ||||||
| { | ||||||
| _Py_IDENTIFIER(as_integer_ratio); | ||||||
| _Py_IDENTIFIER(numerator); | ||||||
| _Py_IDENTIFIER(denominator); | ||||||
| PyObject *ratio, *as_integer_ratio, *numerator, *denominator; | ||||||
|
|
||||||
| if (PyLong_CheckExact(x)) { | ||||||
| return PyTuple_Pack(2, x, _PyLong_One); | ||||||
| } | ||||||
|
|
||||||
| if (_PyObject_LookupAttrId(x, &PyId_as_integer_ratio, &as_integer_ratio) < 0) { | ||||||
|
Comment thread
Copy link
Copy Markdown
Contributor
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityBased on the context, I can roughly tell what this conditional is doing. From my understanding, _PyObject_LookupAttrId() is assessing whether or not the PyObject x contains an as_integer_ratio attribute. If a value less than zero is returned (usually -1), it does not contain that attribute. However, I'm not certain that I understand where PyId_as_integer_ratio is coming from or how PyId actually works. I was unable to find any documentation on PyId or _Py_IDENTIFIER(), so I'm guessing it's an internal part of the C-API (since it's prefixed with an underscore). My best guess is that a reference to PyId_as_integer_ratio was created when _Py_IDENTIFIER(as_integer_ratio) was used. I'm fairly new to the C-API, so I'm trying to learn more about it so that I can be more helpful in PR reviews that involve it. Particularly the internal implementation details that aren't in the documentation.
Sorry, something went wrong.
All reactions
Copy link
Copy Markdown
Member
Author
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityIt is described in the header: Include/cpython/object.h.
Sorry, something went wrong.
aeros reacted with thumbs up emoji
All reactions
Copy link
Copy Markdown
Contributor
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityAh, thanks for letting me know where to look. The code comments there addressed my question:
Sorry, something went wrong.
All reactions
|
||||||
| return NULL; | ||||||
| } | ||||||
| if (as_integer_ratio) { | ||||||
| ratio = _PyObject_CallNoArg(as_integer_ratio); | ||||||
| Py_DECREF(as_integer_ratio); | ||||||
| if (ratio == NULL) { | ||||||
| return NULL; | ||||||
| } | ||||||
| if (!PyTuple_Check(ratio)) { | ||||||
| PyErr_Format(PyExc_TypeError, | ||||||
| "unexpected return type from as_integer_ratio(): " | ||||||
| "expected tuple, got '%.200s'", | ||||||
| Py_TYPE(ratio)->tp_name); | ||||||
| Py_DECREF(ratio); | ||||||
| return NULL; | ||||||
| } | ||||||
| if (PyTuple_GET_SIZE(ratio) != 2) { | ||||||
| PyErr_SetString(PyExc_ValueError, | ||||||
| "as_integer_ratio() must return a 2-tuple"); | ||||||
| Py_DECREF(ratio); | ||||||
| return NULL; | ||||||
| } | ||||||
| } | ||||||
| else { | ||||||
| if (_PyObject_LookupAttrId(x, &PyId_numerator, &numerator) < 0) { | ||||||
| return NULL; | ||||||
| } | ||||||
| if (numerator == NULL) { | ||||||
| PyErr_Format(PyExc_TypeError, | ||||||
| "required a number, not '%.200s'", | ||||||
| Py_TYPE(x)->tp_name); | ||||||
| return NULL; | ||||||
| } | ||||||
| if (_PyObject_LookupAttrId(x, &PyId_denominator, &denominator) < 0) { | ||||||
| Py_DECREF(numerator); | ||||||
| return NULL; | ||||||
| } | ||||||
| if (denominator == NULL) { | ||||||
| Py_DECREF(numerator); | ||||||
| PyErr_Format(PyExc_TypeError, | ||||||
| "required a number, not '%.200s'", | ||||||
| Py_TYPE(x)->tp_name); | ||||||
| return NULL; | ||||||
| } | ||||||
| ratio = PyTuple_Pack(2, numerator, denominator); | ||||||
| Py_DECREF(numerator); | ||||||
| Py_DECREF(denominator); | ||||||
| } | ||||||
| return ratio; | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
| static PyMethodDef math_methods[] = { | ||||||
| {"acos", math_acos, METH_O, math_acos_doc}, | ||||||
| {"acosh", math_acosh, METH_O, math_acosh_doc}, | ||||||
| MATH__AS_INTEGER_RATIO_METHODDEF | ||||||
| {"asin", math_asin, METH_O, math_asin_doc}, | ||||||
| {"asinh", math_asinh, METH_O, math_asinh_doc}, | ||||||
| {"atan", math_atan, METH_O, math_atan_doc}, | ||||||
| Expand Down | ||||||
| Back | FazBrowse Home | New Git URL |
Uh oh!
There was an error while loading. Please reload this page.
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.
Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualitySince the name of the encompassing type is "numeric" rather than "number", can we adjust the error message?
Source: https://docs.python.org/3.9/library/stdtypes.html#numeric-types-int-float-complex
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityBut an instance of a numeric type is a number, is not?
And if use "argument type", it should be "str", not "string".
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
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.
Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityIt is, but I think that it's a bit more useful to users to specify the actual types in this case since it's a TypeError. Also, when searching the docs for "number", the relevant documentation page ("Built-in Types") does not come up as a suggestion, instead they'll likely encounter the page for the "numbers" module (which would not be relevant to the error). When searching for "numeric", more relevant results are found, including "Built-in Types".
I'll update the suggestion accordingly.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityThis is a standard message used in many sites. For example:
If you want to change it, please open a separate issue.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityAlright, I'll take a look at what other areas use this error message and consider whether or not it should be addressed. That would definitely go outside of the scope of this PR.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.