/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-05 14:26:58 UTC
  • mto: This revision was merged to the branch mainline in revision 6468.
  • Revision ID: v.ladeuil+lp@free.fr-20120105142658-vek3v6pzlxb751s2
Tests passing for a first rough version of a cached branch config store. The changes here are too invasive and several parallel proposals have been made. 

@only_raises is evil and gave a hard time since any exception during
save_changes() was swallowed.

Possible improvements: 

- add some needs_write_lock decorators to crucial
  methods (_set_config_location ?) but keep locking the branch at higher levels

- decorate branch.unlock to call stack.save if last_lock() it True
  outside of @only_raises scope (evil decorator)

- add @needs_write_lock to stack.set and stack.remove (will probably get
  rid of most testing issues) we probably need a specialized decorator
  that can relay to the store and from there to the branch or whatever is
  needed. This will also helps bzr config to get it right. The
  get_mutable_section trick should not be needed anymore either.

- decorate branch.unlock to call stack.save if last_lock() it True outside
  of @only_raises scope (evil decorator)

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.