~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/lazy_regex.py

  • Committer: Vincent Ladeuil
  • Date: 2013-10-04 09:56:23 UTC
  • mto: (6588.1.1 trunk)
  • mto: This revision was merged to the branch mainline in revision 6589.
  • Revision ID: v.ladeuil+lp@free.fr-20131004095623-xlan34vg0y51gdb5
Stricter checks on configuration option names

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
 
19
 
This module defines a class which creates proxy objects for regex compilation.
20
 
This allows overriding re.compile() to return lazily compiled objects.
 
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.
21
25
"""
22
26
 
 
27
from __future__ import absolute_import
 
28
 
23
29
import re
24
30
 
 
31
from bzrlib import errors
 
32
 
25
33
 
26
34
class LazyRegex(object):
27
35
    """A proxy around a real regex, which won't be compiled until accessed."""
42
50
    def __init__(self, args=(), kwargs={}):
43
51
        """Create a new proxy object, passing in the args to pass to re.compile
44
52
 
45
 
        :param args: The *args to pass to re.compile
46
 
        :param kwargs: The **kwargs to pass to re.compile
 
53
        :param args: The `*args` to pass to re.compile
 
54
        :param kwargs: The `**kwargs` to pass to re.compile
47
55
        """
48
56
        self._real_regex = None
49
57
        self._regex_args = args
58
66
 
59
67
    def _real_re_compile(self, *args, **kwargs):
60
68
        """Thunk over to the original re.compile"""
61
 
        return _real_re_compile(*args, **kwargs)
 
69
        try:
 
70
            return _real_re_compile(*args, **kwargs)
 
71
        except re.error, e:
 
72
            # raise InvalidPattern instead of re.error as this gives a
 
73
            # cleaner message to the user.
 
74
            raise errors.InvalidPattern('"' + args[0] + '" ' +str(e))
 
75
 
 
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"])
62
88
 
63
89
    def __getattr__(self, attr):
64
90
        """Return a member from the proxied regex object.
91
117
 
92
118
def reset_compile():
93
119
    """Restore the original function to re.compile().
94
 
    
 
120
 
95
121
    It is safe to call reset_compile() multiple times, it will always
96
122
    restore re.compile() to the value that existed at import time.
97
123
    Though the first call will reset back to the original (it doesn't
101
127
 
102
128
 
103
129
_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"
 
130
if _real_re_compile is lazy_compile:
 
131
    raise AssertionError(
 
132
        "re.compile has already been overridden as lazy_compile, but this would" \
 
133
        " cause infinite recursion")