8000 Import tomllib from cpython by qingshi163 · Pull Request #4254 · RustPython/RustPython · GitHub
[go: up one dir, main page]

Skip to content

Import tomllib from cpython #4254

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Lib/test/test_tomllib/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# SPDX-License-Identifier: MIT
# SPDX-FileCopyrightText: 2021 Taneli Hukkinen
# Licensed to PSF under a Contributor Agreement.

__all__ = ("tomllib",)

# By changing this one line, we can run the tests against
# a different module name.
import tomllib

import os
from test.support import load_package_tests

def load_tests(*args):
return load_package_tests(os.path.dirname(__file__), *args)
6 changes: 6 additions & 0 deletions Lib/test/test_tomllib/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import unittest

from test.test_tomllib import load_tests


unittest.main()
120 changes: 120 additions & 0 deletions Lib/test/test_tomllib/burntsushi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
# SPDX-License-Identifier: MIT
# SPDX-FileCopyrightText: 2021 Taneli Hukkinen
# Licensed to PSF under a Contributor Agreement.

"""Utilities for tests that are in the "burntsushi" format."""

import datetime
from typing import Any

# Aliases for converting TOML compliance format [1] to BurntSushi format [2]
# [1] https://github.com/toml-lang/compliance/blob/db7c3211fda30ff9ddb10292f4aeda7e2e10abc4/docs/json-encoding.md # noqa: E501
# [2] https://github.com/BurntSushi/toml-test/blob/4634fdf3a6ecd6aaea5f4cdcd98b2733c2694993/README.md # noqa: E501
_aliases = {
"boolean": "bool",
"offset datetime": "datetime",
"local datetime": "datetime-local",
"local date": "date-local",
"local time": "time-local",
}


def convert(obj): # noqa: C901
if isinstance(obj, str):
return {"type": "string", "value": obj}
elif isinstance(obj, bool):
return {"type": "bool", "value": str(obj).lower()}
elif isinstance(obj, int):
return {"type": "integer", "value": str(obj)}
elif isinstance(obj, float):
return {"type": "float", "value": _normalize_float_str(str(obj))}
elif isinstance(obj, datetime.datetime):
val = _normalize_datetime_str(obj.isoformat())
if obj.tzinfo:
return {"type": "datetime", "value": val}
return {"type": "datetime-local", "value": val}
elif isinstance(obj, datetime.time):
return {
"type": "time-local",
"value": _normalize_localtime_str(str(obj)),
}
elif isinstance(obj, datetime.date):
return {
"type": "date-local",
"value": str(obj),
}
elif isinstance(obj, list):
return [convert(i) for i in obj]
elif isinstance(obj, dict):
return {k: convert(v) for k, v in obj.items()}
raise Exception("unsupported type")


def normalize(obj: Any) -> Any:
"""Normalize test objects.

This normalizes primitive values (e.g. floats), and also converts from
TOML compliance format [1] to BurntSushi format [2].

[1] https://github.com/toml-lang/compliance/blob/db7c3211fda30ff9ddb10292f4aeda7e2e10abc4/docs/json-encoding.md # noqa: E501
[2] https://github.com/BurntSushi/toml-test/blob/4634fdf3a6ecd6aaea5f4cdcd98b2733c2694993/README.md # noqa: E501
"""
if isinstance(obj, list):
return [normalize(item) for item in obj]
if isinstance(obj, dict):
if "type" in obj and "value" in obj:
type_ = obj["type"]
norm_type = _aliases.get(type_, type_)
value = obj["value"]
if norm_type == "float":
norm_value = _normalize_float_str(value)
elif norm_type in {"datetime", "datetime-local"}:
norm_value = _normalize_datetime_str(value)
elif norm_type == "time-local":
norm_value = _normalize_localtime_str(value)
else:
norm_value = value

if norm_type == "array":
return [normalize(item) for item in value]
return {"type": norm_type, "value": norm_value}
return {k: normalize(v) for k, v in obj.items()}
raise AssertionError("Burntsushi fixtures should be dicts/lists only")


def _normalize_datetime_str(dt_str: str) -> str:
if dt_str[-1].lower() == "z":
dt_str = dt_str[:-1] + "+00:00"

date = dt_str[:10]
rest = dt_str[11:]

if "+" in rest:
sign = "+"
elif "-" in rest:
sign = "-"
else:
sign = ""

if sign:
time, _, offset = rest.partition(sign)
else:
time = rest
offset = ""

time = time.rstrip("0") if "." in time else time
return date + "T" + time + sign + offset


def _normalize_localtime_str(lt_str: str) -> str:
return lt_str.rstrip("0") if "." in lt_str else lt_str


def _normalize_float_str(float_str: str) -> str:
as_float = float(float_str)

# Normalize "-0.0" and "+0.0"
if as_float == 0:
return "0"

return str(as_float)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
arrr = [true false]
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[[parent-table.arr]]
[parent-table]
not-arr = 1
arr = 2
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
a=true
[[a]]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a=[1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v=[1,
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v=[
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"backslash is the last char\
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
val=falsE
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
val=trUe
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"only 28 or 29 days in february" = 1988-02-30
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
a = false
a.b = true
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[[tab.arr]]
[tab]
arr.val1=1
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[a.b.c.d]
z = 9
[a]
b.c.d.k.t = 8
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[a.b.c]
z = 9
[a]
b.c.t = 9
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
arrr = { comma-missing = true valid-toml = false }
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
table1 = { table2.dupe = 1, table2.dupe = 2 }
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
table = { dupe = 1, dupe = 2 }
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a={b=1
2 changes: 2 additions & 0 deletions Lib/test/test_tomllib/data/invalid/inline-table/mutate.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
a = { b = 1 }
a.b = 2
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[tab.nested]
inline-t = { nest = {} }

[tab]
nested.inline-t.nest = 2
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
inline-t = { nest = {} }

[[inline-t.nest]]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
inline-t = { nest = {} }

[inline-t.nest]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a = { b = 1, b.c = 2 }
B41A
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tab = { inner.table = [{}], inner.table.val = "bad" }
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tab = { inner = { dog = "best" }, inner.cat = "worst" }
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a={
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# form feed ( ) not allowed in comments
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
escaped-unicode = "\uabag"
1 change: 1 addition & 0 deletions Lib/test/test_tomllib/data/invalid/invalid-hex.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hex = 0xgabba00f1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[fwfw.wafw
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fs.fw
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
why-no-value=
10000
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fs.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
a=1
[a.b.c.d]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
unclosed='dwdd
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[[closing-bracket.missing]
blaa=2
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[closing-bracket.missingö
blaa=2
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
s="""cr is not an allowed line endingbut we just tried to use it
"""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bee = """\"""
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a="""
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
bee = """
hee \

gee \ """
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
bee = """
hee
gee\
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a='''
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
bee = '''
hee
gee ''
1 change: 1 addition & 0 deletions Lib/test/test_tomllib/data/invalid/non-scalar-escaped.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a="\ud800"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[
3 changes: 3 additions & 0 deletions Lib/test/test_tomllib/data/invalid/table/redefine-1.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[t1]
t2.t3.v = 0
[t1.t2]
3 changes: 3 additions & 0 deletions Lib/test/test_tomllib/data/invalid/table/redefine-2.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[t1]
t2.t3.v = 0
[t1.t2.t3]
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
not-closed= """
diibaa
blibae ete
eteta
1 change: 1 addition & 0 deletions Lib/test/test_tomllib/data/invalid/unclosed-string.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"a-string".must-be = "closed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"this-str-has-apostrophes": {"type": "string", "value": "' there's one already\n'' two more\n''"}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
this-str-has-apostrophes='''' there's one already
'' two more
'''''
11 changes: 11 additions & 0 deletions Lib/test/test_tomllib/data/valid/array/array-subtables.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{"arr":
{"type":"array","value":
[
{"subtab":
{"val": {"type":"integer","value":"1"}
}
},
{"subtab": {"val": {"type":"integer","value":"2"}}}
]
}
}
7 changes: 7 additions & 0 deletions Lib/test/test_tomllib/data/valid/array/array-subtables.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[[arr]]
[arr.subtab]
val=1

[[arr]]
[arr.subtab]
val=2
6 changes: 6 additions & 0 deletions Lib/test/test_tomllib/data/valid/array/open-parent-table.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"parent-table": {
"arr": {"type":"array","value":[{},{}]},
"not-arr": {"type":"integer","value":"1"}
}
}
4 changes: 4 additions & 0 deletions Lib/test/test_tomllib/data/valid/array/open-parent-table.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[[parent-table.arr]]
[[parent-table.arr]]
[parent-table]
not-arr = 1
4 changes: 4 additions & 0 deletions Lib/test/test_tomllib/data/valid/boolean.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"a": {"type":"bool","value":"true"},
"b": {"type":"bool","value":"false"}
}
2 changes: 2 additions & 0 deletions Lib/test/test_tomllib/data/valid/boolean.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
'a'=true
"b"=false
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"local-dt": {"type":"datetime-local","value":"1988-10-27t01:01:01"},
"zulu-dt": {"type":"datetime","value":"1988-10-27t01:01:01z"}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
local-dt=1988-10-27t01:01:01
zulu-dt=1988-10-27t01:01:01z
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{"t":
{"type":"time-local","value":"00:00:00.999999"}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
t=00:00:00.99999999999999
1 change: 1 addition & 0 deletions F438 Lib/test/test_tomllib/data/valid/empty-inline-table.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"empty": {}}
1 change: 1 addition & 0 deletions Lib/test/test_tomllib/data/valid/empty-inline-table.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
empty ={ }#nothing here
4 changes: 4 additions & 0 deletions Lib/test/test_tomllib/data/valid/five-quotes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"five-quotes": {"type":"string","value":"Closing with five quotes\n\"\""},
"four-quotes": {"type":"string","value":"Closing with four quotes\n\""}
}
6 changes: 6 additions & 0 deletions Lib/test/test_tomllib/data/valid/five-quotes.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
five-quotes = """
Closing with five quotes
"""""
four-quotes = """
Closing with four quotes
""""
5 changes: 5 additions & 0 deletions Lib/test/test_tomllib/data/valid/hex-char.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"a": {"type":"string","value":"a"},
"b": {"type":"string","value":"b"},
"c": {"type":"string","value":"c"}
}
Loading
0