Skip to content

Use _PyLong_GetOne() and _PyLong_GetZero() in long_invmod() #125044

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

Merged
merged 1 commit into from
Oct 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 2 additions & 11 deletions Objects/longobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -4828,21 +4828,12 @@ long_divmod(PyObject *a, PyObject *b)
static PyLongObject *
long_invmod(PyLongObject *a, PyLongObject *n)
{
PyLongObject *b, *c;

/* Should only ever be called for positive n */
assert(_PyLong_IsPositive(n));

b = (PyLongObject *)PyLong_FromLong(1L);
if (b == NULL) {
return NULL;
}
c = (PyLongObject *)PyLong_FromLong(0L);
if (c == NULL) {
Py_DECREF(b);
return NULL;
}
Py_INCREF(a);
PyLongObject *b = (PyLongObject *)Py_NewRef(_PyLong_GetOne());
Copy link
Contributor

Choose a reason for hiding this comment

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

@vstinner Out of curiosity: why did you add the Py_NewRef when _PyLong_GetOne is documented to return an immortal singleton?
(or can modern compilers optimize this away?)

Copy link
Member Author

Choose a reason for hiding this comment

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

The Py_NewRef() is useless. I added it because the code wants a strong refererence and I didn't pay attention that these two constants are immortal.

(or can modern compilers optimize this away?)

No, Py_NewRef() is not optimized.

See also my PR gh-124076 attempt.

PyLongObject *c = (PyLongObject *)Py_NewRef(_PyLong_GetZero());
Py_INCREF(n);

/* references now owned: a, b, c, n */
Expand Down
Loading