bpo-42161: Use _PyLong_GetZero() and _PyLong_GetOne() (GH-22995)

Use _PyLong_GetZero() and _PyLong_GetOne()
in Objects/ and Python/ directories.
This commit is contained in:
Victor Stinner 2020-10-27 02:24:34 +01:00 committed by GitHub
parent 303aac8c56
commit c9bc290dd6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 68 additions and 40 deletions

View file

@ -6,6 +6,7 @@
/* Submitted by Jim Hugunin */
#include "Python.h"
#include "pycore_long.h" // _PyLong_GetZero()
#include "pycore_object.h" // _PyObject_Init()
#include "structmember.h" // PyMemberDef
@ -870,7 +871,7 @@ complex_subtype_from_string(PyTypeObject *type, PyObject *v)
/*[clinic input]
@classmethod
complex.__new__ as complex_new
real as r: object(c_default="_PyLong_Zero") = 0
real as r: object(c_default="NULL") = 0
imag as i: object(c_default="NULL") = 0
Create a complex number from a real part and an optional imaginary part.
@ -880,7 +881,7 @@ This is equivalent to (real + imag*1j) where imag defaults to 0.
static PyObject *
complex_new_impl(PyTypeObject *type, PyObject *r, PyObject *i)
/*[clinic end generated code: output=b6c7dd577b537dc1 input=6f6b0bedba29bcb5]*/
/*[clinic end generated code: output=b6c7dd577b537dc1 input=f4c667f2596d4fd1]*/
{
PyObject *tmp;
PyNumberMethods *nbr, *nbi = NULL;
@ -889,6 +890,10 @@ complex_new_impl(PyTypeObject *type, PyObject *r, PyObject *i)
int cr_is_complex = 0;
int ci_is_complex = 0;
if (r == NULL) {
r = _PyLong_GetZero();
}
/* Special-case for a single argument when type(arg) is complex. */
if (PyComplex_CheckExact(r) && i == NULL &&
type == &PyComplex_Type) {