Debian Codegraph @

Lib/test/test_tomllib/test_misc.py

raw
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 import importlib
9 from pathlib import Path
10 import sys
11 import tempfile
12 import unittest
13 from test import support
14
15 from . import tomllib
16
17
18 class TestMiscellaneous(unittest.TestCase):
19 def test_load(self):
20 content = "one=1 \n two='two' \n arr=[]"
21 expected = {"one": 1, "two": "two", "arr": []}
22 with tempfile.TemporaryDirectory() as tmp_dir_path:
23 file_path = Path(tmp_dir_path) / "test.toml"
24 file_path.write_text(content)
25
26 with open(file_path, "rb") as bin_f:
27 actual = tomllib.load(bin_f)
28 self.assertEqual(actual, expected)
29
30 def test_incorrect_load(self):
31 content = "one=1"
32 with tempfile.TemporaryDirectory() as tmp_dir_path:
33 file_path = Path(tmp_dir_path) / "test.toml"
34 file_path.write_text(content)
35
36 with open(file_path, "r") as txt_f:
37 with self.assertRaises(TypeError):
38 tomllib.load(txt_f) # type: ignore[arg-type]
39
40 def test_parse_float(self):
41 doc = """
42 val=0.1
43 biggest1=inf
44 biggest2=+inf
45 smallest=-inf
46 notnum1=nan
47 notnum2=-nan
48 notnum3=+nan
49 """
50 obj = tomllib.loads(doc, parse_float=D)
51 expected = {
52 "val": D("0.1"),
53 "biggest1": D("inf"),
54 "biggest2": D("inf"),
55 "smallest": D("-inf"),
56 "notnum1": D("nan"),
57 "notnum2": D("-nan"),
58 "notnum3": D("nan"),
59 }
60 for k, expected_val in expected.items():
61 actual_val = obj[k]
62 self.assertIsInstance(actual_val, D)
63 if actual_val.is_nan():
64 self.assertTrue(expected_val.is_nan())
65 else:
66 self.assertEqual(actual_val, expected_val)
67
68 def test_deepcopy(self):
69 doc = """
70 [bliibaa.diibaa]
71 offsettime=[1979-05-27T00:32:00.999999-07:00]
72 """
73 obj = tomllib.loads(doc)
74 obj_copy = copy.deepcopy(obj)
75 self.assertEqual(obj_copy, obj)
76 expected_obj = {
77 "bliibaa": {
78 "diibaa": {
79 "offsettime": [
80 datetime.datetime(
81 1979,
82 5,
83 27,
84 0,
85 32,
86 0,
87 999999,
88 tzinfo=datetime.timezone(datetime.timedelta(hours=-7)),
89 )
90 ]
91 }
92 }
93 }
94 self.assertEqual(obj_copy, expected_obj)
95
96 @support.skip_if_unlimited_stack_size
97 def test_inline_array_recursion_limit(self):
98 with support.infinite_recursion(max_depth=100):
99 available = support.get_recursion_available()
100 nest_count = (available // 2) - 2
101 # Add details if the test fails
102 with self.subTest(limit=sys.getrecursionlimit(),
103 available=available,
104 nest_count=nest_count):
105 recursive_array_toml = "arr = " + nest_count * "[" + nest_count * "]"
106 tomllib.loads(recursive_array_toml)
107
108 @support.skip_if_unlimited_stack_size
109 def test_inline_table_recursion_limit(self):
110 with support.infinite_recursion(max_depth=100):
111 available = support.get_recursion_available()
112 nest_count = (available // 3) - 1
113 # Add details if the test fails
114 with self.subTest(limit=sys.getrecursionlimit(),
115 available=available,
116 nest_count=nest_count):
117 recursive_table_toml = nest_count * "key = {" + nest_count * "}"
118 tomllib.loads(recursive_table_toml)
119
120 def test_types_import(self):
121 """Test that `_types` module runs.
122
123 The module is for type annotations only, so it is otherwise
124 never imported by tests.
125 """
126 importlib.import_module(f"{tomllib.__name__}._types")