55import string
66import reop
77
8- error = 're error'
8+ # reop.error and re.error should be the same, since exceptions can be
9+ # raised from either module.
10+ error = reop .error # 're error'
11+
12+ from reop import NORMAL , CHARCLASS , REPLACEMENT
13+ from reop import CHAR , MEMORY_REFERENCE , SYNTAX , NOT_SYNTAX , SET
14+ from reop import WORD_BOUNDARY , NOT_WORD_BOUNDARY , BEGINNING_OF_BUFFER , END_OF_BUFFER
915
1016# compilation flags
1117
@@ -622,9 +628,9 @@ def build_fastmap(code, pos=0):
622628#
623629#
624630
625- [NORMAL , CHARCLASS , REPLACEMENT ] = range (3 )
626- [CHAR , MEMORY_REFERENCE , SYNTAX , NOT_SYNTAX , SET , WORD_BOUNDARY ,
627- NOT_WORD_BOUNDARY , BEGINNING_OF_BUFFER , END_OF_BUFFER ] = range (9 )
631+ # [NORMAL, CHARCLASS, REPLACEMENT] = range(3)
632+ # [CHAR, MEMORY_REFERENCE, SYNTAX, NOT_SYNTAX, SET, WORD_BOUNDARY,
633+ # NOT_WORD_BOUNDARY, BEGINNING_OF_BUFFER, END_OF_BUFFER] = range(9)
628634
629635def expand_escape (pattern , index , context = NORMAL ):
630636 if index >= len (pattern ):
@@ -636,6 +642,9 @@ def expand_escape(pattern, index, context=NORMAL):
636642 elif pattern [index ] == 'n' :
637643 return CHAR , chr (10 ), index + 1
638644
645+ elif pattern [index ] == 'v' :
646+ return CHAR , chr (11 ), index + 1
647+
639648 elif pattern [index ] == 'r' :
640649 return CHAR , chr (13 ), index + 1
641650
@@ -645,28 +654,17 @@ def expand_escape(pattern, index, context=NORMAL):
645654 elif pattern [index ] == 'a' :
646655 return CHAR , chr (7 ), index + 1
647656
648- elif pattern [index ] == 'e' :
649- return CHAR , chr (27 ), index + 1
650-
651- elif pattern [index ] == 'c' :
652- if index + 1 >= len (pattern ):
653- raise error , '\\ c must be followed by another character'
654- elif pattern [index + 1 ] in 'abcdefghijklmnopqrstuvwxyz' :
655- return CHAR , chr (ord (pattern [index + 1 ]) - ord ('a' ) + 1 ), index + 2
656- else :
657- return CHAR , chr (ord (pattern [index + 1 ]) ^ 64 ), index + 2
658-
659657 elif pattern [index ] == 'x' :
660658 # CAUTION: this is the Python rule, not the Perl rule!
661- end = index
659+ end = index + 1 # Skip over the 'x' character
662660 while (end < len (pattern )) and (pattern [end ] in string .hexdigits ):
663661 end = end + 1
664662 if end == index :
665663 raise error , "\\ x must be followed by hex digit(s)"
666664 # let Python evaluate it, so we don't incorrectly 2nd-guess
667665 # what it's doing (and Python in turn passes it on to sscanf,
668666 # so that *it* doesn't incorrectly 2nd-guess what C does!)
669- char = eval ('"' + pattern [index - 2 :end ] + '"' )
667+ char = eval ('"' + pattern [index - 1 :end ] + '"' )
670668 assert len (char ) == 1
671669 return CHAR , char , end
672670
@@ -690,12 +688,12 @@ def expand_escape(pattern, index, context=NORMAL):
690688
691689 elif pattern [index ] == 'Z' :
692690 if context != NORMAL :
693- return 'Z' , index + 1
691+ return CHAR , 'Z' , index + 1
694692 else :
695693 return END_OF_BUFFER , '' , index + 1
696694
697695 elif pattern [index ] in 'GluLUQE' :
698- raise error , ('\\ ' + ch + ' is not allowed' )
696+ raise error , ('\\ ' + pattern [ index ] + ' is not allowed' )
699697
700698 elif pattern [index ] == 'w' :
701699 if context == NORMAL :
@@ -1488,3 +1486,8 @@ def compile(pattern, flags=0):
14881486 label = label + 1
14891487 code .append (End ())
14901488 return RegexObject (pattern , flags , code , register , groupindex )
1489+
1490+ # Replace expand_escape and _expand functions with their C equivalents.
1491+ # If you suspect bugs in the C versions, comment out the next two lines
1492+ expand_escape = reop .expand_escape
1493+ _expand = reop ._expand
0 commit comments