annotate Lib/regexp.py @ 4566:6403c94de50d legacy-trunk

Added safeguard against failure in __del__.
author Guido van Rossum <guido@python.org>
date Wed, 27 Nov 1996 19:39:58 +0000
parents 1940892d2fe3
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
540
48eaf4fc53cb Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 # Provide backward compatibility for module "regexp" using "regex".
48eaf4fc53cb Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
48eaf4fc53cb Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 import regex
48eaf4fc53cb Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 from regex_syntax import *
48eaf4fc53cb Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
5
48eaf4fc53cb Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 class Prog:
1435
560ec7030817 * Mass change: get rid of all init() methods, in favor of __init__()
Guido van Rossum <guido@python.org>
parents: 1178
diff changeset
7 def __init__(self, pat):
540
48eaf4fc53cb Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 save_syntax = regex.set_syntax(RE_SYNTAX_AWK)
48eaf4fc53cb Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 try:
48eaf4fc53cb Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 self.prog = regex.compile(pat)
48eaf4fc53cb Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 finally:
48eaf4fc53cb Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 xxx = regex.set_syntax(save_syntax)
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
13 def match(self, str, offset = 0):
540
48eaf4fc53cb Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 if self.prog.search(str, offset) < 0:
48eaf4fc53cb Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 return ()
557
1ef63adf4fc5 New == syntax; regs is no longer a method.
Guido van Rossum <guido@python.org>
parents: 540
diff changeset
16 regs = self.prog.regs
540
48eaf4fc53cb Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 i = len(regs)
557
1ef63adf4fc5 New == syntax; regs is no longer a method.
Guido van Rossum <guido@python.org>
parents: 540
diff changeset
18 while i > 0 and regs[i-1] == (-1, -1):
540
48eaf4fc53cb Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 i = i-1
48eaf4fc53cb Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 return regs[:i]
48eaf4fc53cb Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
21
48eaf4fc53cb Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 def compile(pat):
1435
560ec7030817 * Mass change: get rid of all init() methods, in favor of __init__()
Guido van Rossum <guido@python.org>
parents: 1178
diff changeset
23 return Prog(pat)
540
48eaf4fc53cb Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
24
48eaf4fc53cb Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 cache_pat = None
48eaf4fc53cb Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 cache_prog = None
48eaf4fc53cb Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
27
48eaf4fc53cb Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 def match(pat, str):
48eaf4fc53cb Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 global cache_pat, cache_prog
48eaf4fc53cb Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 if pat <> cache_pat:
48eaf4fc53cb Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 cache_pat, cache_prog = pat, compile(pat)
48eaf4fc53cb Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 return cache_prog.match(str)