8000 import tomllib from cpython3.11 · RustPython/RustPython@92e027a · GitHub
[go: up one dir, main page]

Skip to content

Commit 92e027a

Browse files
committed
import tomllib from cpython3.11
1 parent 865ad00 commit 92e027a

File tree

84 files changed

+1358
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+1358
-0
lines changed

‎Lib/test/test_tomllib/__init__.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# SPDX-License-Identifier: MIT
2+
# SPDX-FileCopyrightText: 2021 Taneli Hukkinen
3+
# Licensed to PSF under a Contributor Agreement.
4+
5+
__all__ = ("tomllib",)
6+
7+
# By changing this one line, we can run the tests against
8+
# a different module name.
9+
import tomllib
10+
11+
import os
12+
from test.support import load_package_tests
13+
14+
def load_tests(*args):
15+
return load_package_tests(os.path.dirname(__file__), *args)

‎Lib/test/test_tomllib/__main__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import unittest
2+
3+
from test.test_tomllib import load_tests
4+
5+
6+
unittest.main()
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# SPDX-License-Identifier: MIT
2+
# SPDX-FileCopyrightText: 2021 Taneli Hukkinen
3+
# Licensed to PSF under a Contributo 1241 r Agreement.
4+
5+
"""Utilities for tests that are in the "burntsushi" format."""
6+
7+
import datetime
8+
from typing import Any
9+
10+
# Aliases for converting TOML compliance format [1] to BurntSushi format [2]
11+
# [1] https://github.com/toml-lang/compliance/blob/db7c3211fda30ff9ddb10292f4aeda7e2e10abc4/docs/json-encoding.md # noqa: E501
12+
# [2] https://github.com/BurntSushi/toml-test/blob/4634fdf3a6ecd6aaea5f4cdcd98b2733c2694993/README.md # noqa: E501
13+
_aliases = {
14+
"boolean": "bool",
15+
"offset datetime": "datetime",
16+
"local datetime": "datetime-local",
17+
"local date": "date-local",
18+
"local time": "time-local",
19+
}
20+
21+
22+
def convert(obj): # noqa: C901
23+
if isinstance(obj, str):
24+
return {"type": "string", "value": obj}
25+
elif isinstance(obj, bool):
26+
return {"type": "bool", "value": str(obj).lower()}
27+
elif isinstance(obj, int):
28+
return {"type": "integer", "value": str(obj)}
29+
elif isinstance(obj, float):
30+
return {"type": "float", "value": _normalize_float_str(str(obj))}
31+
elif isinstance(obj, datetime.datetime):
32+
val = _normalize_datetime_str(obj.isoformat())
33+
if obj.tzinfo:
34+
return {"type": "datetime", "value": val}
35+
return {"type": "datetime-local", "value": val}
36+
elif isinstance(obj, datetime.time):
37+
return {
38+
"type": "time-local",
39+
"value": _normalize_localtime_str(str(obj)),
40+
}
41+
elif isinstance(obj, datetime.date):
42+
return {
43+
"type": "date-local",
44+
"value": str(obj),
45+
}
46+
elif isinstance(obj, list):
47+
return [convert(i) for i in obj]
48+
elif isinstance(obj, dict):
49+
return {k: convert(v) for k, v in obj.items()}
50+
raise Exception("unsupported type")
51+
52+
53+
def normalize(obj: Any) -> Any:
54+
"""Normalize test objects.
55+
56+
This normalizes primitive values (e.g. floats), and also converts from
57+
TOML compliance format [1] to BurntSushi format [2].
58+
59+
[1] https://github.com/toml-lang/compliance/blob/db7c3211fda30ff9ddb10292f4aeda7e2e10abc4/docs/json-encoding.md # noqa: E501
60+
[2] https://github.com/BurntSushi/toml-test/blob/4634fdf3a6ecd6aaea5f4cdcd98b2733c2694993/README.md # noqa: E501
61+
"""
62+
if isinstance(obj, list):
63+
return [normalize(item) for item in obj]
64+
if isinstance(obj, dict):
65+
if "type" in obj and "value" in obj:
66+
type_ = obj["type"]
67+
norm_type = _aliases.get(type_, type_)
68+
value = obj["value"]
69+
if norm_type == "float":
70+
norm_value = _normalize_float_str(value)
71+
elif norm_type in {"datetime", "datetime-local"}:
72+
norm_value = _normalize_datetime_str(value)
73+
elif norm_type == "time-local":
74+
norm_value = _normalize_localtime_str(value)
75+
else:
76+
norm_value = value
77+
78+
if norm_type == "array":
79+
return [normalize(item) for item in value]
80+
return {"type": norm_type, "value": norm_value}
81+
return {k: normalize(v) for k, v in obj.items()}
82+
raise AssertionError("Burntsushi fixtures should be dicts/lists only")
83+
84+
85+
def _normalize_datetime_str(dt_str: str) -> str:
86+
if dt_str[-1].lower() == "z":
87+
dt_str = dt_str[:-1] + "+00:00"
88+
89+
date = dt_str[:10]
90+
rest = dt_str[11:]
91+
92+
if "+" in rest:
93+
sign = "+"
94+
elif "-" in rest:
95+
sign = "-"
96+
else:
97+
sign = ""
98+
99+
if sign:
100+
time, _, offset = rest.partition(sign)
101+
else:
102+
time = rest
103+
offset = ""
104+
105+
time = time.rstrip("0") if "." in time else time
106+
return date + "T" + time + sign + offset
107+
108+
109+
def _normalize_localtime_str(lt_str: str) -> str:
110+
return lt_str.rstrip("0") if "." in lt_str else lt_str
111+
112+
113+
def _normalize_float_str(float_str: str) -> str:
114+
as_float = float(float_str)
115+
116+
# Normalize "-0.0" and "+0.0"
117+
if as_float == 0:
118+
return "0"
119+
120+
return str(as_float)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
arrr = [true false]
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[[parent-table.arr]]
2+
[parent-table]
3+
not-arr = 1
4+
arr = 2
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
a=true
2+
[[a]]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
a=[1
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
v=[1,
Lines changed: 1 addition & 0 deletions
O C2EE riginal file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
v=[
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"backslash is the last char\
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
val=falsE
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
val=trUe
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"only 28 or 29 days in february" = 1988-02-30
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
a = false
2+
a.b = true
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[[tab.arr]]
2+
[tab]
3+
arr.val1=1
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[a.b.c.d]
2+
z = 9
3+
[a]
4+
b.c.d.k.t = 8
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[a.b.c]
2+
z = 9
3+
[a]
4+
b.c.t = 9
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
arrr = { comma-missing = true valid-toml = false }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
table1 = { table2.dupe = 1, table2.dupe = 2 }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
table = { dupe = 1, dupe = 2 }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
a={b=1
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
a = { b = 1 }
2+
a.b = 2
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[tab.nested]
2+
inline-t = { nest = {} }
3+
4+
[tab]
5+
nested.inline-t.nest = 2
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
inline-t = { nest = {} }
2+
3+
[[inline-t.nest]]
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
inline-t = { nest = {} }
2+
3+
[inline-t.nest]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
a = { b = 1, b.c = 2 }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
tab = { inner.table = [{}], inner.table.val = "bad" }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
tab = { inner = { dog = "best" }, inner.cat = "worst" }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
a={
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# form feed ( ) not allowed in comments
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
escaped-unicode = "\uabag"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hex = 0xgabba00f1
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[fwfw.wafw
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fs.fw
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
why-no-value=
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fs.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
a=1
2+
[a.b.c.d]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
unclosed='dwdd
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[[closing-bracket.missing]
2+
blaa=2
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[closing-bracket.missingö
2+
blaa=2

‎Lib/test/test_tomllib/data/invalid/multiline-basic-str/carriage-return.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
s="""cr is not an allowed line endingbut we just tried to use it
2+
"""
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bee = """\"""
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
a="""
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
bee = """
2+
hee \
3+
4+
gee \ """
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
bee = """
2+
hee
3+
gee\
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
a='''
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
bee = '''
2+
hee
3+
gee ''
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
a="\ud800"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[t1]
2+
t2.t3.v = 0
3+
[t1.t2]
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[t1]
2+
t2.t3.v = 0
3+
[t1.t2.t3]
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
not-closed= """
2+
diibaa
3+
blibae ete
4+
eteta
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"a-string".must-be = "closed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"this-str-has-apostrophes": {"type": "string", "value": "' there's one already\n'' two more\n''"}}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
this-str-has-apostrophes='''' there's one already
2+
'' two more
3+
'''''
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{"arr":
2+
{"type":"array","value":
3+
[
4+
{"subtab":
5+
{"val": {"type":"integer","value":"1"}
6+
}
7+
},
8+
{"subtab": {"val": {"type":"integer","value":"2"}}}
9+
]
10+
}
11+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[[arr]]
2+
[arr.subtab]
3+
val=1
4+
5+
[[arr]]
6+
[arr.subtab]
7+
val=2
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"parent-table": {
3+
"arr": {"type":"array","value":[{},{}]},
4+
"not-arr": {"type":"integer","value":"1"}
5+
}
6+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[[parent-table.arr]]
2+
[[parent-table.arr]]
3+
[parent-table]
4+
not-arr = 1
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"a": {"type":"bool","value":"true"},
3+
"b": {"type":"bool","value":"false"}
4+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
'a'=true
2+
"b"=false
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"local-dt": {"type":"datetime-local","value":"1988-10-27t01:01:01"},
3+
"zulu-dt": {"type":"datetime","value":"1988-10-27t01:01:01z"}
4+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
local-dt=1988-10-27t01:01:01
2+
zulu-dt=1988-10-27t01:01:01z
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{"t":
2+
{"type":"time-local","value":"00:00:00.999999"}}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
t=00:00:00.99999999999999
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"empty": {}}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
empty ={ }#nothing here
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"five-quotes": {"type":"string","value":"Closing with five quotes\n\"\""},
3+
"four-quotes": {"type":"string","value":"Closing with four quotes\n\""}
4+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
five-quotes = """
2+
Closing with five quotes
3+
"""""
4+
four-quotes = """
5+
Closing with four quotes
6+
""""
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"a": {"type":"string","value":"a"},
3+
"b": {"type":"string","value":"b"},
4+
"c": {"type":"string","value":"c"}
5+
}

0 commit comments

Comments
 (0)
0