/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 breezy/tests/test__bencode.py

  • Committer: Jelmer Vernooij
  • Date: 2018-08-21 02:25:27 UTC
  • mfrom: (7077 work)
  • mto: This revision was merged to the branch mainline in revision 7078.
  • Revision ID: jelmer@jelmer.uk-20180821022527-6mxzeeqet5orl0n3
Merge trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
    return suite
27
27
 
28
28
 
 
29
class RecursionLimit(object):
 
30
    """Context manager that lowers recursion limit for testing."""
 
31
 
 
32
    def __init__(self, limit=100):
 
33
        self._new_limit = limit
 
34
        self._old_limit = sys.getrecursionlimit()
 
35
 
 
36
    def __enter__(self):
 
37
        sys.setrecursionlimit(self._new_limit)
 
38
        return self
 
39
 
 
40
    def __exit__(self, *exc_info):
 
41
        sys.setrecursionlimit(self._old_limit)
 
42
 
 
43
 
29
44
class TestBencodeDecode(tests.TestCase):
30
45
 
31
46
    module = None
90
105
        self._check([[b'Alice', b'Bob'], [2, 3]], b'll5:Alice3:Bobeli2ei3eee')
91
106
 
92
107
    def test_list_deepnested(self):
93
 
        self._run_check_error(RuntimeError, (b"l" * 10000) + (b"e" * 10000))
 
108
        with RecursionLimit():
 
109
            self._run_check_error(RuntimeError, (b"l" * 100) + (b"e" * 100))
94
110
 
95
111
    def test_malformed_list(self):
96
112
        self._run_check_error(ValueError, b'l')
107
123
            b'd8:spam.mp3d6:author5:Alice6:lengthi100000eee')
108
124
 
109
125
    def test_dict_deepnested(self):
110
 
        # The recursion here provokes CPython into emitting a warning on
111
 
        # stderr, "maximum recursion depth exceeded in __subclasscheck__", due
112
 
        # to running out of stack space while evaluating "except (...):" in
113
 
        # _bencode_py.  This is harmless, so we temporarily override stderr to
114
 
        # avoid distracting noise in the test output.
115
 
        self.overrideAttr(sys, 'stderr', self._log_file)
116
 
        self._run_check_error(
117
 
            RuntimeError, (b"d0:" * 10000) + b'i1e' + (b"e" * 10000))
 
126
        with RecursionLimit():
 
127
            self._run_check_error(
 
128
                RuntimeError, (b"d0:" * 1000) + b'i1e' + (b"e" * 1000))
118
129
 
119
130
    def test_malformed_dict(self):
120
131
        self._run_check_error(ValueError, b'd')
184
195
    def test_list_deep_nested(self):
185
196
        top = []
186
197
        l = top
187
 
        for i in range(10000):
 
198
        for i in range(1000):
188
199
            l.append([])
189
200
            l = l[0]
190
 
        self.assertRaises(RuntimeError, self.module.bencode, top)
 
201
        with RecursionLimit():
 
202
            self.assertRaises(RuntimeError, self.module.bencode, top)
191
203
 
192
204
    def test_dict(self):
193
205
        self._check(b'de', {})
197
209
 
198
210
    def test_dict_deep_nested(self):
199
211
        d = top = {}
200
 
        for i in range(10000):
 
212
        for i in range(1000):
201
213
            d[b''] = {}
202
214
            d = d[b'']
203
 
        self.assertRaises(RuntimeError, self.module.bencode, top)
 
215
        with RecursionLimit():
 
216
            self.assertRaises(RuntimeError, self.module.bencode, top)
204
217
 
205
218
    def test_bencached(self):
206
219
        self._check(b'i3e', self.module.Bencached(self.module.bencode(3)))