[Python-ideas] IntFlags

Andrew Barnert abarnert at yahoo.com
Fri Mar 6 03:13:56 CET 2015


On Mar 5, 2015, at 9:11, Serhiy Storchaka <storchaka at gmail.com> wrote:

> On 05.03.15 01:42, Andrew Barnert wrote:
>> I think you're also right that using signed 1's complement is the best way to handle negated flags, despite the tradeoffs (not being able to compare to negated C values, having a confusing numerical value in the repr, having silly str for silly cases), especially since that's what ~ already does with IntEnum (except that the result is just plain int, of course).
> 
> Sorry, I don't understand your argument. Why you can't compare complemented IntFlags with complemented int?

You can, but what you can't do is compare it with the values that you'll likely get from C, because -0o200001 != 0o37775777777 in Python, whereas they are equal in C.

> ~(os.OpenMode.O_CLOEXEC) == int(~os.OpenMode.O_CLOEXEC) == ~int(os.OpenMode.O_CLOEXEC) == posix.O_CLOEXEC

I certainly hope that's not true, or ~ is a no-op... But even if you add the missing ~, none of these is going to be == the value of an unsigned int holding ~O_CLOEXEC in C.

Again, I already said I think you chose the best tradeoff, but there is a downside compared to treating the values as unsigned or to the other options people have proposed here; that's why it's a tradeoff rather than a one obvious choice.


More information about the Python-ideas mailing list