/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/lazy_regex.py

  • Committer: Vincent Ladeuil
  • Date: 2012-01-18 14:09:19 UTC
  • mto: This revision was merged to the branch mainline in revision 6468.
  • Revision ID: v.ladeuil+lp@free.fr-20120118140919-rlvdrhpc0nq1lbwi
Change set/remove to require a lock for the branch config files.

This means that tests (or any plugin for that matter) do not requires an
explicit lock on the branch anymore to change a single option. This also
means the optimisation becomes "opt-in" and as such won't be as
spectacular as it may be and/or harder to get right (nothing fails
anymore).

This reduces the diff by ~300 lines.

Code/tests that were updating more than one config option is still taking
a lock to at least avoid some IOs and demonstrate the benefits through
the decreased number of hpss calls.

The duplication between BranchStack and BranchOnlyStack will be removed
once the same sharing is in place for local config files, at which point
the Stack class itself may be able to host the changes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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.