~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/lazy_regex.py

  • Committer: Andrew Bennetts
  • Date: 2010-10-08 04:25:10 UTC
  • mto: This revision was merged to the branch mainline in revision 5472.
  • Revision ID: andrew.bennetts@canonical.com-20101008042510-sg9vdhmnggilzxsk
Fix stray TAB in source.

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
"""Lazily compiled regex objects.
18
18
 
22
22
 
23
23
import re
24
24
 
 
25
from bzrlib import errors
 
26
 
25
27
 
26
28
class LazyRegex(object):
27
29
    """A proxy around a real regex, which won't be compiled until accessed."""
58
60
 
59
61
    def _real_re_compile(self, *args, **kwargs):
60
62
        """Thunk over to the original re.compile"""
61
 
        return _real_re_compile(*args, **kwargs)
 
63
        try:
 
64
            return _real_re_compile(*args, **kwargs)
 
65
        except re.error, e:
 
66
            # raise InvalidPattern instead of re.error as this gives a
 
67
            # cleaner message to the user.
 
68
            raise errors.InvalidPattern('"' + args[0] + '" ' +str(e))
62
69
 
63
70
    def __getattr__(self, attr):
64
71
        """Return a member from the proxied regex object.
91
98
 
92
99
def reset_compile():
93
100
    """Restore the original function to re.compile().
94
 
    
 
101
 
95
102
    It is safe to call reset_compile() multiple times, it will always
96
103
    restore re.compile() to the value that existed at import time.
97
104
    Though the first call will reset back to the original (it doesn't
101
108
 
102
109
 
103
110
_real_re_compile = re.compile
104
 
assert _real_re_compile is not lazy_compile, \
105
 
    "re.compile has already been overridden as lazy_compile, but this would" \
106
 
    " cause infinite recursion"
 
111
if _real_re_compile is lazy_compile:
 
112
    raise AssertionError(
 
113
        "re.compile has already been overridden as lazy_compile, but this would" \
 
114
        " cause infinite recursion")