[Tutor] Regex ^$ not behaving as expected

Danny Yoo dyoo at hashcollision.org
Thu Dec 8 12:20:19 EST 2016


Following up: drats!  Detecting this conceptual TypeError is not
feasible under the current design, due to the choice of data
representation used in this API.

The reason is because the flags are being represented as integers, and
we're using bitwise operations to define the union of flags.  That is:

#######################################
>>> import re
>>> re.MULTILINE
8
>>> re.DOTALL
16
>>> re.MULTILINE | re.DOTALL
24
########################################

Flags are integers here.  Since re.sub is taking two optional
integer-based arguments, count and flags, Python's type system cannot
determine that we've passed flags in the wrong place, because it
legitimately looks like a valid count too.  Bits are bits.

This is a very rough design edge and quite unfortunate.  In an ideal
world, I can imagine that the representation of flags would be
different such that this mistake could be caught earlier.  But in
absence of this, we've just got to be careful, I suppose.  :(


Anyway, hope this helps!


More information about the Tutor mailing list