1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
# -*- coding: utf-8 -*-
# :Project: python-rapidjson -- Circular references tests
# :Author: John Anderson <sontek@gmail.com>
# :License: MIT License
# :Copyright: © 2015 John Anderson
# :Copyright: © 2017, 2018, 2020 Lele Gaifax
#
import sys
import pytest
if sys.version_info >= (3, 5):
expected_exception = RecursionError
else:
expected_exception = RuntimeError
def test_circular_dict(dumps):
dct = {}
dct['a'] = dct
with pytest.raises(expected_exception):
dumps(dct)
def test_circular_list(dumps):
lst = []
lst.append(lst)
with pytest.raises(expected_exception):
dumps(lst)
def test_circular_composite(dumps):
dct2 = {}
dct2['a'] = []
dct2['a'].append(dct2)
with pytest.raises(expected_exception):
dumps(dct2)
def test_max_recursion_depth(dumps):
import sys
root = child = {}
for i in range(500):
nephew = {'value': i}
child['child'] = nephew
child = nephew
dumps(root)
rl = sys.getrecursionlimit()
sys.setrecursionlimit(500)
try:
with pytest.raises(expected_exception):
dumps(root)
finally:
sys.setrecursionlimit(rl)
|