~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/lazy_regex.py

  • Committer: Vincent Ladeuil
  • Date: 2017-01-17 13:48:10 UTC
  • mfrom: (6615.3.6 merges)
  • mto: This revision was merged to the branch mainline in revision 6620.
  • Revision ID: v.ladeuil+lp@free.fr-20170117134810-j9p3lidfy6pfyfsc
Merge 2.7, resolving conflicts

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006 Canonical Ltd
 
1
# Copyright (C) 2006, 2008-2011, 2017 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
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
 
25
31
from bzrlib import errors
44
50
    def __init__(self, args=(), kwargs={}):
45
51
        """Create a new proxy object, passing in the args to pass to re.compile
46
52
 
47
 
        :param args: The *args to pass to re.compile
48
 
        :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
49
55
        """
50
56
        self._real_regex = None
51
57
        self._regex_args = args
67
73
            # cleaner message to the user.
68
74
            raise errors.InvalidPattern('"' + args[0] + '" ' +str(e))
69
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"])
 
88
 
70
89
    def __getattr__(self, attr):
71
90
        """Return a member from the proxied regex object.
72
91
 
112
131
    raise AssertionError(
113
132
        "re.compile has already been overridden as lazy_compile, but this would" \
114
133
        " cause infinite recursion")
 
134
 
 
135
 
 
136
# re.finditer get confused if it receives a LazyRegex
 
137
if getattr(re, 'finditer', None is not None):
 
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