1 # SPDX-License-Identifier: MIT
2 # SPDX-FileCopyrightText: 2021 Taneli Hukkinen
3 # Licensed to PSF under a Contributor Agreement.
4
5 import copy
6 import datetime
7 from decimal import Decimal as D
8 from pathlib import Path
9 import sys
10 import tempfile
11 import unittest
12 from test import support
13
14 from . import tomllib
15
16
17 class ESC[4;38;5;81mTestMiscellaneous(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
18 def test_load(self):
19 content = "one=1 \n two='two' \n arr=[]"
20 expected = {"one": 1, "two": "two", "arr": []}
21 with tempfile.TemporaryDirectory() as tmp_dir_path:
22 file_path = Path(tmp_dir_path) / "test.toml"
23 file_path.write_text(content)
24
25 with open(file_path, "rb") as bin_f:
26 actual = tomllib.load(bin_f)
27 self.assertEqual(actual, expected)
28
29 def test_incorrect_load(self):
30 content = "one=1"
31 with tempfile.TemporaryDirectory() as tmp_dir_path:
32 file_path = Path(tmp_dir_path) / "test.toml"
33 file_path.write_text(content)
34
35 with open(file_path, "r") as txt_f:
36 with self.assertRaises(TypeError):
37 tomllib.load(txt_f) # type: ignore[arg-type]
38
39 def test_parse_float(self):
40 doc = """
41 val=0.1
42 biggest1=inf
43 biggest2=+inf
44 smallest=-inf
45 notnum1=nan
46 notnum2=-nan
47 notnum3=+nan
48 """
49 obj = tomllib.loads(doc, parse_float=D)
50 expected = {
51 "val": D("0.1"),
52 "biggest1": D("inf"),
53 "biggest2": D("inf"),
54 "smallest": D("-inf"),
55 "notnum1": D("nan"),
56 "notnum2": D("-nan"),
57 "notnum3": D("nan"),
58 }
59 for k, expected_val in expected.items():
60 actual_val = obj[k]
61 self.assertIsInstance(actual_val, D)
62 if actual_val.is_nan():
63 self.assertTrue(expected_val.is_nan())
64 else:
65 self.assertEqual(actual_val, expected_val)
66
67 def test_deepcopy(self):
68 doc = """
69 [bliibaa.diibaa]
70 offsettime=[1979-05-27T00:32:00.999999-07:00]
71 """
72 obj = tomllib.loads(doc)
73 obj_copy = copy.deepcopy(obj)
74 self.assertEqual(obj_copy, obj)
75 expected_obj = {
76 "bliibaa": {
77 "diibaa": {
78 "offsettime": [
79 datetime.datetime(
80 1979,
81 5,
82 27,
83 0,
84 32,
85 0,
86 999999,
87 tzinfo=datetime.timezone(datetime.timedelta(hours=-7)),
88 )
89 ]
90 }
91 }
92 }
93 self.assertEqual(obj_copy, expected_obj)
94
95 def test_inline_array_recursion_limit(self):
96 with support.infinite_recursion(max_depth=100):
97 available = support.get_recursion_available()
98 nest_count = (available // 2) - 2
99 # Add details if the test fails
100 with self.subTest(limit=sys.getrecursionlimit(),
101 available=available,
102 nest_count=nest_count):
103 recursive_array_toml = "arr = " + nest_count * "[" + nest_count * "]"
104 tomllib.loads(recursive_array_toml)
105
106 def test_inline_table_recursion_limit(self):
107 with support.infinite_recursion(max_depth=100):
108 available = support.get_recursion_available()
109 nest_count = (available // 3) - 1
110 # Add details if the test fails
111 with self.subTest(limit=sys.getrecursionlimit(),
112 available=available,
113 nest_count=nest_count):
114 recursive_table_toml = nest_count * "key = {" + nest_count * "}"
115 tomllib.loads(recursive_table_toml)