Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f2989b2

Browse files
author
Fredrik Lundh
committed
- restored 1.5.2 compatibility (sorry, eric)
- removed __all__ cruft from internal modules (sorry, skip) - don't assume ASCII for string escapes (sorry, per)
1 parent ae76367 commit f2989b2

5 files changed

Lines changed: 39 additions & 31 deletions

File tree

Lib/sre.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,14 @@
1717
import sre_compile
1818
import sre_parse
1919

20-
__all__ = ["match","search","sub","subn","split","findall","compile",
21-
"purge","template","escape","I","L","M","S","X","U","IGNORECASE",
22-
"LOCALE","MULTILINE","DOTALL","VERBOSE","UNICODE","error"]
20+
# public symbols
21+
__all__ = [ "match", "search", "sub", "subn", "split", "findall",
22+
"compile", "purge", "template", "escape", "I", "L", "M", "S", "X",
23+
"U", "IGNORECASE", "LOCALE", "MULTILINE", "DOTALL", "VERBOSE",
24+
"UNICODE", "error" ]
25+
26+
# this module works under 1.5.2 and later. don't use string methods
27+
import string
2328

2429
# flags
2530
I = IGNORECASE = sre_compile.SRE_FLAG_IGNORECASE # ignore case
@@ -88,7 +93,6 @@ def purge():
8893

8994
def template(pattern, flags=0):
9095
"Compile a template pattern, returning a pattern object"
91-
9296
return _compile(pattern, flags|T)
9397

9498
def escape(pattern):
@@ -111,7 +115,7 @@ def escape(pattern):
111115

112116
def _join(seq, sep):
113117
# internal: join into string having the same type as sep
114-
return sep[:0].join(seq)
118+
return string.join(seq, sep[:0])
115119

116120
def _compile(*key):
117121
# internal: compile pattern

Lib/sre_compile.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212

1313
from sre_constants import *
1414

15-
__all__ = ["compile"]
16-
1715
assert _sre.MAGIC == MAGIC, "SRE module mismatch"
1816

1917
MAXCODE = 65535

Lib/sre_constants.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,11 +195,12 @@ def makedict(list):
195195
SRE_INFO_CHARSET = 4 # pattern starts with character from given set
196196

197197
if __name__ == "__main__":
198+
import string
198199
def dump(f, d, prefix):
199200
items = d.items()
200201
items.sort(lambda a, b: cmp(a[1], b[1]))
201202
for k, v in items:
202-
f.write("#define %s_%s %s\n" % (prefix, k.upper(), v))
203+
f.write("#define %s_%s %s\n" % (prefix, string.upper(k), v))
203204
f = open("sre_constants.h", "w")
204205
f.write("""\
205206
/*

Lib/sre_parse.py

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,11 @@
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

1516
from sre_constants import *
1617

17-
__all__ = ["Pattern","SubPattern","Tokenizer","parse","parse_template",
18-
"expand_template"]
19-
2018
SPECIAL_CHARS = ".\\[{()*+?^$|"
2119
REPEAT_CHARS = "*+?{"
2220

@@ -28,13 +26,13 @@
2826
WHITESPACE = tuple(" \t\n\r\v\f")
2927

3028
ESCAPES = {
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

@@ -63,6 +61,13 @@
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+
6671
class Pattern:
6772
# master pattern object. keeps track of global attributes
6873
def __init__(self):
@@ -219,7 +224,7 @@ def isname(name):
219224
def _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)

Lib/test/test_sre.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from test_support import verbose, TestFailed
1010
import sre
11-
import sys, os, traceback
11+
import sys, os, string, traceback
1212

1313
#
1414
# test support

0 commit comments

Comments
 (0)