1010
1111# XXX: show string offset and offending character for all errors
1212
13- import sys
13+ # this module works under 1.5.2 and later. don't use string methods
14+ import string , sys
1415
1516from sre_constants import *
1617
17- __all__ = ["Pattern" ,"SubPattern" ,"Tokenizer" ,"parse" ,"parse_template" ,
18- "expand_template" ]
19-
2018SPECIAL_CHARS = ".\\ [{()*+?^$|"
2119REPEAT_CHARS = "*+?{"
2220
2826WHITESPACE = tuple (" \t \n \r \v \f " )
2927
3028ESCAPES = {
31- r"\a" : (LITERAL , 7 ),
32- r"\b" : (LITERAL , 8 ),
33- r"\f" : (LITERAL , 12 ),
34- r"\n" : (LITERAL , 10 ),
35- r"\r" : (LITERAL , 13 ),
36- r"\t" : (LITERAL , 9 ),
37- r"\v" : (LITERAL , 11 ),
29+ r"\a" : (LITERAL , ord ( " \a " ) ),
30+ r"\b" : (LITERAL , ord ( " \b " ) ),
31+ r"\f" : (LITERAL , ord ( " \f " ) ),
32+ r"\n" : (LITERAL , ord ( " \n " ) ),
33+ r"\r" : (LITERAL , ord ( " \r " ) ),
34+ r"\t" : (LITERAL , ord ( " \t " ) ),
35+ r"\v" : (LITERAL , ord ( " \v " ) ),
3836 r"\\" : (LITERAL , ord ("\\ " ))
3937}
4038
6361 "u" : SRE_FLAG_UNICODE ,
6462}
6563
64+ # figure out best way to convert hex/octal numbers to integers
65+ try :
66+ int ("10" , 8 )
67+ atoi = int # 2.0 and later
68+ except TypeError :
69+ atoi = string .atoi # 1.5.2
70+
6671class Pattern :
6772 # master pattern object. keeps track of global attributes
6873 def __init__ (self ):
@@ -219,7 +224,7 @@ def isname(name):
219224def _group (escape , groups ):
220225 # check if the escape string represents a valid group
221226 try :
222- gid = int (escape [1 :])
227+ gid = atoi (escape [1 :])
223228 if gid and gid < groups :
224229 return gid
225230 except ValueError :
@@ -242,13 +247,13 @@ def _class_escape(source, escape):
242247 escape = escape [2 :]
243248 if len (escape ) != 2 :
244249 raise error , "bogus escape: %s" % repr ("\\ " + escape )
245- return LITERAL , int (escape , 16 ) & 0xff
250+ return LITERAL , atoi (escape , 16 ) & 0xff
246251 elif str (escape [1 :2 ]) in OCTDIGITS :
247252 # octal escape (up to three digits)
248253 while source .next in OCTDIGITS and len (escape ) < 5 :
249254 escape = escape + source .get ()
250255 escape = escape [1 :]
251- return LITERAL , int (escape , 8 ) & 0xff
256+ return LITERAL , atoi (escape , 8 ) & 0xff
252257 if len (escape ) == 2 :
253258 return LITERAL , ord (escape [1 ])
254259 except ValueError :
@@ -270,12 +275,12 @@ def _escape(source, escape, state):
270275 escape = escape + source .get ()
271276 if len (escape ) != 4 :
272277 raise ValueError
273- return LITERAL , int (escape [2 :], 16 ) & 0xff
278+ return LITERAL , atoi (escape [2 :], 16 ) & 0xff
274279 elif escape [1 :2 ] == "0" :
275280 # octal escape
276281 while source .next in OCTDIGITS and len (escape ) < 4 :
277282 escape = escape + source .get ()
278- return LITERAL , int (escape [1 :], 8 ) & 0xff
283+ return LITERAL , atoi (escape [1 :], 8 ) & 0xff
279284 elif escape [1 :2 ] in DIGITS :
280285 # octal escape *or* decimal group reference (sigh)
281286 here = source .tell ()
@@ -285,7 +290,7 @@ def _escape(source, escape, state):
285290 source .next in OCTDIGITS ):
286291 # got three octal digits; this is an octal escape
287292 escape = escape + source .get ()
288- return LITERAL , int (escape [1 :], 8 ) & 0xff
293+ return LITERAL , atoi (escape [1 :], 8 ) & 0xff
289294 # got at least one decimal digit; this is a group reference
290295 group = _group (escape , state .groups )
291296 if group :
@@ -459,9 +464,9 @@ def _parse(source, state):
459464 source .seek (here )
460465 continue
461466 if lo :
462- min = int (lo )
467+ min = atoi (lo )
463468 if hi :
464- max = int (hi )
469+ max = atoi (hi )
465470 if max < min :
466471 raise error , "bad repeat interval"
467472 else :
@@ -649,7 +654,7 @@ def parse_template(source, pattern):
649654 if not name :
650655 raise error , "bad group name"
651656 try :
652- index = int (name )
657+ index = atoi (name )
653658 except ValueError :
654659 if not isname (name ):
655660 raise error , "bad character in group name"
@@ -673,7 +678,7 @@ def parse_template(source, pattern):
673678 break
674679 if not code :
675680 this = this [1 :]
676- code = LITERAL , int (this [- 6 :], 8 ) & 0xff
681+ code = LITERAL , atoi (this [- 6 :], 8 ) & 0xff
677682 a (code )
678683 else :
679684 try :
@@ -702,4 +707,4 @@ def expand_template(template, match):
702707 if s is None :
703708 raise error , "empty group"
704709 a (s )
705- return sep .join (p )
710+ return string .join (p , sep )
0 commit comments