~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/lazy_regex.py

MergeĀ lp:~gz/bzr/path_from_environ_832028

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
 
23
27
import re
44
48
    def __init__(self, args=(), kwargs={}):
45
49
        """Create a new proxy object, passing in the args to pass to re.compile
46
50
 
47
 
        :param args: The *args to pass to re.compile
48
 
        :param kwargs: The **kwargs to pass to re.compile
 
51
        :param args: The `*args` to pass to re.compile
 
52
        :param kwargs: The `**kwargs` to pass to re.compile
49
53
        """
50
54
        self._real_regex = None
51
55
        self._regex_args = args
67
71
            # cleaner message to the user.
68
72
            raise errors.InvalidPattern('"' + args[0] + '" ' +str(e))
69
73
 
 
74
    def __getstate__(self):
 
75
        """Return the state to use when pickling."""
 
76
        return {
 
77
            "args": self._regex_args,
 
78
            "kwargs": self._regex_kwargs,
 
79
            }
 
80
 
 
81
    def __setstate__(self, dict):
 
82
        """Restore from a pickled state."""
 
83
        self._real_regex = None
 
84
        setattr(self, "_regex_args", dict["args"])
 
85
        setattr(self, "_regex_kwargs", dict["kwargs"])
 
86
 
70
87
    def __getattr__(self, attr):
71
88
        """Return a member from the proxied regex object.
72
89