~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/lazy_regex.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2010-09-01 08:02:42 UTC
  • mfrom: (5390.3.3 faster-revert-593560)
  • Revision ID: pqm@pqm.ubuntu.com-20100901080242-esg62ody4frwmy66
(spiv) Avoid repeatedly calling self.target.all_file_ids() in
 InterTree.iter_changes. (Andrew Bennetts)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006, 2008-2011, 2017 Canonical Ltd
 
1
# Copyright (C) 2006 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
16
16
 
17
17
"""Lazily compiled regex objects.
18
18
 
19
 
This module defines a class which creates proxy objects for regex
20
 
compilation.  This allows overriding re.compile() to return lazily compiled
21
 
objects.  
22
 
 
23
 
We do this rather than just providing a new interface so that it will also
24
 
be used by existing Python modules that create regexs.
 
19
This module defines a class which creates proxy objects for regex compilation.
 
20
This allows overriding re.compile() to return lazily compiled objects.
25
21
"""
26
22
 
27
 
from __future__ import absolute_import
28
 
 
29
23
import re
30
24
 
31
25
from bzrlib import errors
50
44
    def __init__(self, args=(), kwargs={}):
51
45
        """Create a new proxy object, passing in the args to pass to re.compile
52
46
 
53
 
        :param args: The `*args` to pass to re.compile
54
 
        :param kwargs: The `**kwargs` to pass to re.compile
 
47
        :param args: The *args to pass to re.compile
 
48
        :param kwargs: The **kwargs to pass to re.compile
55
49
        """
56
50
        self._real_regex = None
57
51
        self._regex_args = args
73
67
            # cleaner message to the user.
74
68
            raise errors.InvalidPattern('"' + args[0] + '" ' +str(e))
75
69
 
76
 
    def __getstate__(self):
77
 
        """Return the state to use when pickling."""
78
 
        return {
79
 
            "args": self._regex_args,
80
 
            "kwargs": self._regex_kwargs,
81
 
            }
82
 
 
83
 
    def __setstate__(self, dict):
84
 
        """Restore from a pickled state."""
85
 
        self._real_regex = None
86
 
        setattr(self, "_regex_args", dict["args"])
87
 
        setattr(self, "_regex_kwargs", dict["kwargs"])
88
 
 
89
70
    def __getattr__(self, attr):
90
71
        """Return a member from the proxied regex object.
91
72
 
131
112
    raise AssertionError(
132
113
        "re.compile has already been overridden as lazy_compile, but this would" \
133
114
        " cause infinite recursion")
134
 
 
135
 
 
136
 
# re.finditer get confused if it receives a LazyRegex
137
 
if getattr(re, 'finditer', None is not None):
138
 
    def finditer_public(pattern, string, flags=0):
139
 
        if isinstance(pattern, LazyRegex):
140
 
            return pattern.finditer(string)
141
 
        else:
142
 
            return _real_re_compile(pattern, flags).finditer(string)
143
 
re.finditer = finditer_public