[Python-ideas] IntFlags
Andrew Barnert
abarnert at yahoo.com
Thu Mar 5 00:19:00 CET 2015
On Mar 4, 2015, at 11:52, random832 at fastmail.us wrote:
> On Wed, Mar 4, 2015, at 10:17, Andrew Barnert wrote:
>> One of the big questions that (IIRC) derailed this last time and got it
>> dropped from the enum stdlib design was: what does ~ do? Does it give you
>> the 2's complement negative integer? What does that display as in the str
>> and repr? And, if you add in conversion from an IntFlags to/from a set of
>> separate values, as has been suggested again in this thread, how does
>> that work? All of this is trivial when you're dealing with C fixed-size
>> unsigned ints: ~READ means 15 of the 16 bits (all except the READ bit)
>> are set.
>
> str should be ~(READ) obviously. And more generally
> ~(all|bits|that|are|not|set).
Think about how that extends to the result of |. Of course the answer depends on how you store ~ in the first place, but for most choices, str is not obvious. For example, using fixed-size unsigned with automatic highest-bit detection, for an enum with READ, WRITE, EXEC, STICKY, ~(READ) is the same value as (WRITE|EXEC|STICKY), so how does str know which to print? The one with the fewest flags? Some other rule? (The signed 1's comp choice actually has a reasonable answer here, it just means that you get silly results for silly values, which is fine...)
>> Another issue that came up was that C flags often have "combined" names
>> that are ambiguous: RDWR = RDONLY | WRONLY),
>
> Nope. You've actually got a different, much worse, issue: traditionally,
> RDONLY is 0 (and should be printed if and only if 1 and 2 are not set),
> WRONLY is 1, RDWR is 2, and 3 is traditionally invalid and may have a
> platform-dependent meaning.
Yeah, open flags are especially screwy, where flags & 3 has a special non-bitmapped meaning but the rest of the bits are flags. There are other cases where multiple ints, only some of which are bitmaps, are packed as separate bitfields into the same int (e.g., TCP/IP headers), but I don't know of any others that pretend to be a single bitmap even though they aren't, so that's really a unique problem, which can be ignored. Just think about stat results or mmap prot flags or anything else where read and write are separate bits. (PS, IIRC, Linux treats 3 as "open for fstat only", and you have that access on most files even if you can't do anything else to them, which was an accident left in place because lilo used it, which made lilo a pain to port to FreeBSD as part of a Linux repair kit...)
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
More information about the Python-ideas
mailing list