~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/lazy_regex.py

(jameinel) Allow 'bzr serve' to interpret SIGHUP as a graceful shutdown.
 (bug #795025) (John A Meinel)

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
24
24
be used by existing Python modules that create regexs.
25
25
"""
26
26
 
27
 
from __future__ import absolute_import
28
 
 
29
27
import re
30
28
 
31
29
from bzrlib import errors
73
71
            # cleaner message to the user.
74
72
            raise errors.InvalidPattern('"' + args[0] + '" ' +str(e))
75
73
 
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
74
    def __getattr__(self, attr):
90
75
        """Return a member from the proxied regex object.
91
76
 
131
116
    raise AssertionError(
132
117
        "re.compile has already been overridden as lazy_compile, but this would" \
133
118
        " cause infinite recursion")
134
 
 
135
 
 
136
 
# Some libraries calls re.finditer which fails it if receives a LazyRegex.
137
 
if getattr(re, 'finditer', False):
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