Author serhiy.storchaka
Recipients mark.dickinson, serhiy.storchaka, vstinner
Date 2019-02-20.06:43:05
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
Currently, C API functions that convert a Python number to a C integer like PyLong_AsLong() and argument parsing functions like PyArg_ParseTuple() with integer converting format units like 'i' use the __int__() special method for converting objects which are not instances of int or int subclasses. This leads to dropping the fractional part if the object is not integral number (e.g. float, Decimal or Fraction). In some cases, there is a special check for float, but it does not prevent truncation for Decimal, Fraction and other numeric types.

For example:

>>> chr(65.5)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: integer argument expected, got float
>>> chr(Decimal('65.5'))

The proposed PR makes all these functions using __index__() instead of __int__() if available and emit a deprecation warning when __int__() is used for implicit conversion to a C integer.

>>> chr(Decimal('65.5'))
<stdin>:1: DeprecationWarning: an integer is required (got type decimal.Decimal)

In future versions only __index__() will be used for the implicit conversion, and __int__() will be used only in the int constructor.
Date User Action Args
2019-02-20 06:43:05serhiy.storchakasetrecipients: + serhiy.storchaka, mark.dickinson, vstinner
2019-02-20 06:43:05serhiy.storchakasetmessageid: <>
2019-02-20 06:43:05serhiy.storchakalinkissue36048 messages
2019-02-20 06:43:05serhiy.storchakacreate