Skip to content

bpo-37999: No longer use __int__ in implicit integer conversions. #15636

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
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 7 additions & 0 deletions Doc/whatsnew/3.9.rst
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ Other Language Changes
ignored for empty strings.
(Contributed by Victor Stinner in :issue:`37388`.)

* Builtin and extension functions that take integer arguments no longer accept
:class:`~decimal.Decimal`\ s, :class:`~fractions.Fraction`\ s and other
objects that can be converted to integers only with a loss (e.g. that have
the :meth:`~object.__int__` method but do not have the
:meth:`~object.__index__` method).
(Contributed by Serhiy Storchaka in :issue:`37999`.)


New Modules
===========
Expand Down
17 changes: 0 additions & 17 deletions Include/longobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,23 +173,6 @@ PyAPI_FUNC(int) _PyLong_AsByteArray(PyLongObject* v,
unsigned char* bytes, size_t n,
int little_endian, int is_signed);

/* _PyLong_FromNbInt: Convert the given object to a PyLongObject
using the nb_int slot, if available. Raise TypeError if either the
nb_int slot is not available or the result of the call to nb_int
returns something not of type int.
*/
PyAPI_FUNC(PyObject *) _PyLong_FromNbInt(PyObject *);

/* Convert the given object to a PyLongObject using the nb_index or
nb_int slots, if available (the latter is deprecated).
Raise TypeError if either nb_index and nb_int slots are not
available or the result of the call to nb_index or nb_int
returns something not of type int.
Should be replaced with PyNumber_Index after the end of the
deprecation period.
*/
PyAPI_FUNC(PyObject *) _PyLong_FromNbIndexOrNbInt(PyObject *);

/* _PyLong_Format: Convert the long to a string object with given base,
appending a base prefix of 0[box] if base is 2, 8 or 16. */
PyAPI_FUNC(PyObject *) _PyLong_Format(PyObject *obj, int base);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Builtin and extension functions that take integer arguments no longer accept
:class:`~decimal.Decimal`\ s, :class:`~fractions.Fraction`\ s and other
objects that can be converted to integers only with a loss (e.g. that have
the :meth:`~object.__int__` method but do not have the
:meth:`~object.__index__` method).
27 changes: 1 addition & 26 deletions Modules/_blake2/clinic/blake2b_impl.c.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,6 @@ py_blake2b_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
goto skip_optional_kwonly;
}
if (fastargs[1]) {
if (PyFloat_Check(fastargs[1])) {
PyErr_SetString(PyExc_TypeError,
"integer argument expected, got float" );
goto exit;
}
digest_size = _PyLong_AsInt(fastargs[1]);
if (digest_size == -1 && PyErr_Occurred()) {
goto exit;
Expand Down Expand Up @@ -104,11 +99,6 @@ py_blake2b_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
}
}
if (fastargs[5]) {
if (PyFloat_Check(fastargs[5])) {
PyErr_SetString(PyExc_TypeError,
"integer argument expected, got float" );
goto exit;
}
fanout = _PyLong_AsInt(fastargs[5]);
if (fanout == -1 && PyErr_Occurred()) {
goto exit;
Expand All @@ -118,11 +108,6 @@ py_blake2b_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
}
}
if (fastargs[6]) {
if (PyFloat_Check(fastargs[6])) {
PyErr_SetString(PyExc_TypeError,
"integer argument expected, got float" );
goto exit;
}
depth = _PyLong_AsInt(fastargs[6]);
if (depth == -1 && PyErr_Occurred()) {
goto exit;
Expand All @@ -148,11 +133,6 @@ py_blake2b_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
}
}
if (fastargs[9]) {
if (PyFloat_Check(fastargs[9])) {
PyErr_SetString(PyExc_TypeError,
"integer argument expected, got float" );
goto exit;
}
node_depth = _PyLong_AsInt(fastargs[9]);
if (node_depth == -1 && PyErr_Occurred()) {
goto exit;
Expand All @@ -162,11 +142,6 @@ py_blake2b_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
}
}
if (fastargs[10]) {
if (PyFloat_Check(fastargs[10])) {
PyErr_SetString(PyExc_TypeError,
"integer argument expected, got float" );
goto exit;
}
inner_size = _PyLong_AsInt(fastargs[10]);
if (inner_size == -1 && PyErr_Occurred()) {
goto exit;
Expand Down Expand Up @@ -261,4 +236,4 @@ _blake2_blake2b_hexdigest(BLAKE2bObject *self, PyObject *Py_UNUSED(ignored))
{
return _blake2_blake2b_hexdigest_impl(self);
}
/*[clinic end generated code: output=cbb625d7f60c288c input=a9049054013a1b77]*/
/*[clinic end generated code: output=f6102672dca4278a input=a9049054013a1b77]*/
27 changes: 1 addition & 26 deletions Modules/_blake2/clinic/blake2s_impl.c.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,6 @@ py_blake2s_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
goto skip_optional_kwonly;
}
if (fastargs[1]) {
if (PyFloat_Check(fastargs[1])) {
PyErr_SetString(PyExc_TypeError,
"integer argument expected, got float" );
goto exit;
}
digest_size = _PyLong_AsInt(fastargs[1]);
if (digest_size == -1 && PyErr_Occurred()) {
goto exit;
Expand Down Expand Up @@ -104,11 +99,6 @@ py_blake2s_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
}
}
if (fastargs[5]) {
if (PyFloat_Check(fastargs[5])) {
PyErr_SetString(PyExc_TypeError,
"integer argument expected, got float" );
goto exit;
}
fanout = _PyLong_AsInt(fastargs[5]);
if (fanout == -1 && PyErr_Occurred()) {
goto exit;
Expand All @@ -118,11 +108,6 @@ py_blake2s_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
}
}
if (fastargs[6]) {
if (PyFloat_Check(fastargs[6])) {
PyErr_SetString(PyExc_TypeError,
"integer argument expected, got float" );
goto exit;
}
depth = _PyLong_AsInt(fastargs[6]);
if (depth == -1 && PyErr_Occurred()) {
goto exit;
Expand All @@ -148,11 +133,6 @@ py_blake2s_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
}
}
if (fastargs[9]) {
if (PyFloat_Check(fastargs[9])) {
PyErr_SetString(PyExc_TypeError,
"integer argument expected, got float" );
goto exit;
}
node_depth = _PyLong_AsInt(fastargs[9]);
if (node_depth == -1 && PyErr_Occurred()) {
goto exit;
Expand All @@ -162,11 +142,6 @@ py_blake2s_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
}
}
if (fastargs[10]) {
if (PyFloat_Check(fastargs[10])) {
PyErr_SetString(PyExc_TypeError,
"integer argument expected, got float" );
goto exit;
}
inner_size = _PyLong_AsInt(fastargs[10]);
if (inner_size == -1 && PyErr_Occurred()) {
goto exit;
Expand Down Expand Up @@ -261,4 +236,4 @@ _blake2_blake2s_hexdigest(BLAKE2sObject *self, PyObject *Py_UNUSED(ignored))
{
return _blake2_blake2s_hexdigest_impl(self);
}
/*[clinic end generated code: output=39af5a74c8805b36 input=a9049054013a1b77]*/
/*[clinic end generated code: output=ccaf156832fb9bce input=a9049054013a1b77]*/
34 changes: 4 additions & 30 deletions Modules/_ctypes/cfield.c
Original file line number Diff line number Diff line change
Expand Up @@ -353,14 +353,7 @@ PyTypeObject PyCField_Type = {
static int
get_long(PyObject *v, long *p)
{
long x;

if (PyFloat_Check(v)) {
PyErr_SetString(PyExc_TypeError,
"int expected instead of float");
return -1;
}
x = PyLong_AsUnsignedLongMask(v);
long x = PyLong_AsUnsignedLongMask(v);
if (x == -1 && PyErr_Occurred())
return -1;
*p = x;
Expand All @@ -372,14 +365,7 @@ get_long(PyObject *v, long *p)
static int
get_ulong(PyObject *v, unsigned long *p)
{
unsigned long x;

if (PyFloat_Check(v)) {
PyErr_SetString(PyExc_TypeError,
"int expected instead of float");
return -1;
}
x = PyLong_AsUnsignedLongMask(v);
unsigned long x = PyLong_AsUnsignedLongMask(v);
if (x == (unsigned long)-1 && PyErr_Occurred())
return -1;
*p = x;
Expand All @@ -391,13 +377,7 @@ get_ulong(PyObject *v, unsigned long *p)
static int
get_longlong(PyObject *v, long long *p)
{
long long x;
if (PyFloat_Check(v)) {
PyErr_SetString(PyExc_TypeError,
"int expected instead of float");
return -1;
}
x = PyLong_AsUnsignedLongLongMask(v);
long long x = PyLong_AsUnsignedLongLongMask(v);
if (x == -1 && PyErr_Occurred())
return -1;
*p = x;
Expand All @@ -409,13 +389,7 @@ get_longlong(PyObject *v, long long *p)
static int
get_ulonglong(PyObject *v, unsigned long long *p)
{
unsigned long long x;
if (PyFloat_Check(v)) {
PyErr_SetString(PyExc_TypeError,
"int expected instead of float");
return -1;
}
x = PyLong_AsUnsignedLongLongMask(v);
unsigned long long x = PyLong_AsUnsignedLongLongMask(v);
if (x == (unsigned long long)-1 && PyErr_Occurred())
return -1;
*p = x;
Expand Down
12 changes: 1 addition & 11 deletions Modules/_io/clinic/_iomodule.c.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,6 @@ _io_open(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kw
}
}
if (args[2]) {
if (PyFloat_Check(args[2])) {
PyErr_SetString(PyExc_TypeError,
"integer argument expected, got float" );
goto exit;
}
buffering = _PyLong_AsInt(args[2]);
if (buffering == -1 && PyErr_Occurred()) {
goto exit;
Expand Down Expand Up @@ -261,11 +256,6 @@ _io_open(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kw
}
}
if (args[6]) {
if (PyFloat_Check(args[6])) {
PyErr_SetString(PyExc_TypeError,
"integer argument expected, got float" );
goto exit;
}
closefd = _PyLong_AsInt(args[6]);
if (closefd == -1 && PyErr_Occurred()) {
goto exit;
Expand Down Expand Up @@ -323,4 +313,4 @@ _io_open_code(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObjec
exit:
return return_value;
}
/*[clinic end generated code: output=3df6bc6d91697545 input=a9049054013a1b77]*/
/*[clinic end generated code: output=5c0dd7a262c30ebc input=a9049054013a1b77]*/
37 changes: 1 addition & 36 deletions Modules/_io/clinic/bufferedio.c.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,6 @@ _io__Buffered_peek(buffered *self, PyObject *const *args, Py_ssize_t nargs)
if (nargs < 1) {
goto skip_optional;
}
if (PyFloat_Check(args[0])) {
PyErr_SetString(PyExc_TypeError,
"integer argument expected, got float" );
goto exit;
}
{
Py_ssize_t ival = -1;
PyObject *iobj = PyNumber_Index(args[0]);
Expand Down Expand Up @@ -200,11 +195,6 @@ _io__Buffered_read1(buffered *self, PyObject *const *args, Py_ssize_t nargs)
if (nargs < 1) {
goto skip_optional;
}
if (PyFloat_Check(args[0])) {
PyErr_SetString(PyExc_TypeError,
"integer argument expected, got float" );
goto exit;
}
{
Py_ssize_t ival = -1;
PyObject *iobj = PyNumber_Index(args[0]);
Expand Down Expand Up @@ -356,11 +346,6 @@ _io__Buffered_seek(buffered *self, PyObject *const *args, Py_ssize_t nargs)
if (nargs < 2) {
goto skip_optional;
}
if (PyFloat_Check(args[1])) {
PyErr_SetString(PyExc_TypeError,
"integer argument expected, got float" );
goto exit;
}
whence = _PyLong_AsInt(args[1]);
if (whence == -1 && PyErr_Occurred()) {
goto exit;
Expand Down Expand Up @@ -434,11 +419,6 @@ _io_BufferedReader___init__(PyObject *self, PyObject *args, PyObject *kwargs)
if (!noptargs) {
goto skip_optional_pos;
}
if (PyFloat_Check(fastargs[1])) {
PyErr_SetString(PyExc_TypeError,
"integer argument expected, got float" );
goto exit;
}
{
Py_ssize_t ival = -1;
PyObject *iobj = PyNumber_Index(fastargs[1]);
Expand Down Expand Up @@ -493,11 +473,6 @@ _io_BufferedWriter___init__(PyObject *self, PyObject *args, PyObject *kwargs)
if (!noptargs) {
goto skip_optional_pos;
}
if (PyFloat_Check(fastargs[1])) {
PyErr_SetString(PyExc_TypeError,
"integer argument expected, got float" );
goto exit;
}
{
Py_ssize_t ival = -1;
PyObject *iobj = PyNumber_Index(fastargs[1]);
Expand Down Expand Up @@ -590,11 +565,6 @@ _io_BufferedRWPair___init__(PyObject *self, PyObject *args, PyObject *kwargs)
if (PyTuple_GET_SIZE(args) < 3) {
goto skip_optional;
}
if (PyFloat_Check(PyTuple_GET_ITEM(args, 2))) {
PyErr_SetString(PyExc_TypeError,
"integer argument expected, got float" );
goto exit;
}
{
Py_ssize_t ival = -1;
PyObject *iobj = PyNumber_Index(PyTuple_GET_ITEM(args, 2));
Expand Down Expand Up @@ -649,11 +619,6 @@ _io_BufferedRandom___init__(PyObject *self, PyObject *args, PyObject *kwargs)
if (!noptargs) {
goto skip_optional_pos;
}
if (PyFloat_Check(fastargs[1])) {
PyErr_SetString(PyExc_TypeError,
"integer argument expected, got float" );
goto exit;
}
{
Py_ssize_t ival = -1;
PyObject *iobj = PyNumber_Index(fastargs[1]);
Expand All @@ -672,4 +637,4 @@ _io_BufferedRandom___init__(PyObject *self, PyObject *args, PyObject *kwargs)
exit:
return return_value;
}
/*[clinic end generated code: output=7246104f6c7d3167 input=a9049054013a1b77]*/
/*[clinic end generated code: output=056db07ecd9fdec4 input=a9049054013a1b77]*/
12 changes: 1 addition & 11 deletions Modules/_io/clinic/bytesio.c.h
Original file line number Diff line number Diff line change
Expand Up @@ -402,11 +402,6 @@ _io_BytesIO_seek(bytesio *self, PyObject *const *args, Py_ssize_t nargs)
if (!_PyArg_CheckPositional("seek", nargs, 1, 2)) {
goto exit;
}
if (PyFloat_Check(args[0])) {
PyErr_SetString(PyExc_TypeError,
"integer argument expected, got float" );
goto exit;
}
{
Py_ssize_t ival = -1;
PyObject *iobj = PyNumber_Index(args[0]);
Expand All @@ -422,11 +417,6 @@ _io_BytesIO_seek(bytesio *self, PyObject *const *args, Py_ssize_t nargs)
if (nargs < 2) {
goto skip_optional;
}
if (PyFloat_Check(args[1])) {
PyErr_SetString(PyExc_TypeError,
"integer argument expected, got float" );
goto exit;
}
whence = _PyLong_AsInt(args[1]);
if (whence == -1 && PyErr_Occurred()) {
goto exit;
Expand Down Expand Up @@ -515,4 +505,4 @@ _io_BytesIO___init__(PyObject *self, PyObject *args, PyObject *kwargs)
exit:
return return_value;
}
/*[clinic end generated code: output=4ec2506def9c8eb9 input=a9049054013a1b77]*/
/*[clinic end generated code: output=ba0f302f16397741 input=a9049054013a1b77]*/
Loading