Repository navigation
Description
There are three main groups of conversion to C integer types:
- Conversion to a signed integer type with overflow checking
- Conversion to an unsigned integer type with overflow checking
- Conversion to an unsigned integer type without overflow checking
In conversion to a signed integer type, an OverflowError is raised if the value outside of the range of the signed integer type. It is OK, as the limits are usually an implementation detail.
In conversion to an unsigned integer type with overflow checking, an OverflowError is usually raised if the value outside of the range of the signed integer type. But in some cases a ValueError is raised for negative value, and OverflowError is raised for large value exceeding the implementation defined limit. I propose to always raise ValueError when negative value is converted to an unsigned integer type. This condition is usually not an implementation detail: a size cannot be negative, independently of a platform. I opened an issue about this in
2017, but @rhettinger was against this, so this froze. But a new PyLong_AsNativeBytes functions raises ValueError for negative values, as well as many private converters used in Argument Clinic, so I decided to revive this issue. Today arguments in favor of it are stronger.
The last group is the most dangerous. Silent ignoring an overflow can leads to bugs. Unfortunately this is the default behavior of PyArg_Parse format units for unsigned integers, and some of PyMemberDef types. There are use cases for this:
- when we do not know whether the destination integer type is signed or unsigned
- when the destination integer type is in general unsigned, but -1 or some other small negative values are special values. For example,
uid_tordev_t-- on some platforms they can have large positive values, but -1 is a special value.
To mitigate possible harm of such API, I propose to limit it too. For example, PyLong_AsUnsignedMask() should raise OverflowError if the value is larger than ULONG_MAX or less than some negative value (LONG_MIN or even smaller, like INT_MIN, or -256, or -1).
Here are my two ideas.
Activity
serhiy-storchaka commentedon Jun 28, 2024
See also https://discuss.python.org/t/conversion-between-python-and-c-integers/44117/2, I may repeat myself here.
encukou commentedon Jul 1, 2024
Which functions are you proposing to change?
I think it's too late to change the types of exceptions raised by existing functions. It's not worth breaking existing code.
But let's make new API is better. And perhaps also add documentation warnings to API that would work differently if added now.
serhiy-storchaka commentedon Jul 1, 2024
PyLong_AsUnsignedLong(),PyLong_AsUnsignedLongLong(),PyLong_AsSize_t()and the "b" format unit inPyArg_Parse(). See gh-74020: Raise ValueError for negative values converted to unsigned python/cpython#121114PyLong_AsUnsignedLongMask(),PyLong_AsUnsignedLongLongMask(), and format units "B", "H", "I", "k", "K" inPyArg_Parse().serhiy-storchaka commentedon Apr 17, 2025
It turned out, that it is difficult to emit a warning or raise an exception in
PyLong_AsUnsignedLongMask()andPyLong_AsUnsignedLongLongMask(). These functions never fail when the argument is a Python integer. There is a code that uses the afterPyLong_Check()without checking for error. Introducing an exception will break such valid code.But we can change unsigned integer formats in
PyArg_Parse(). I created a PR: python/cpython#132630.