MCPcopy Index your code
hub / github.com/RustPython/RustPython / loads

Function loads

Lib/tomllib/_parser.py:136–204  ·  view source on GitHub ↗

Parse TOML from a string.

(s: str, /, *, parse_float: ParseFloat = float)

Source from the content-addressed store, hash-verified

134
135
136def loads(s: str, /, *, parse_float: ParseFloat = float) -> dict[str, Any]: # noqa: C901
137 """Parse TOML from a string."""
138
139 # The spec allows converting "\r\n" to "\n", even in string
140 # literals. Let's do so to simplify parsing.
141 try:
142 src = s.replace("\r\n", "\n")
143 except (AttributeError, TypeError):
144 raise TypeError(
145 f"Expected str object, not '{type(s).__qualname__}'"
146 ) from None
147 pos = 0
148 out = Output()
149 header: Key = ()
150 parse_float = make_safe_parse_float(parse_float)
151
152 # Parse one statement at a time
153 # (typically means one line in TOML source)
154 while True:
155 # 1. Skip line leading whitespace
156 pos = skip_chars(src, pos, TOML_WS)
157
158 # 2. Parse rules. Expect one of the following:
159 # - end of file
160 # - end of line
161 # - comment
162 # - key/value pair
163 # - append dict to list (and move to its namespace)
164 # - create dict (and move to its namespace)
165 # Skip trailing whitespace when applicable.
166 try:
167 char = src[pos]
168 except IndexError:
169 break
170 if char == "\n":
171 pos += 1
172 continue
173 if char in KEY_INITIAL_CHARS:
174 pos = key_value_rule(src, pos, out, header, parse_float)
175 pos = skip_chars(src, pos, TOML_WS)
176 elif char == "[":
177 try:
178 second_char: str | None = src[pos + 1]
179 except IndexError:
180 second_char = None
181 out.flags.finalize_pending()
182 if second_char == "[":
183 pos, header = create_list_rule(src, pos, out)
184 else:
185 pos, header = create_dict_rule(src, pos, out)
186 pos = skip_chars(src, pos, TOML_WS)
187 elif char != "#":
188 raise TOMLDecodeError("Invalid statement", src, pos)
189
190 # 3. Skip comment
191 pos = skip_comment(src, pos)
192
193 # 4. Expect end of line or end of file

Callers 13

loadFunction · 0.70
test_pickleMethod · 0.50
test_compat_unpickleMethod · 0.50
test_compat_unpickleMethod · 0.50
test_compat_unpickleMethod · 0.50
test_compat_unpickleMethod · 0.50
test_compat_unpickleMethod · 0.50
test_pickle_dump_loadFunction · 0.50
test_pickleMethod · 0.50

Calls 10

make_safe_parse_floatFunction · 0.85
skip_charsFunction · 0.85
key_value_ruleFunction · 0.85
create_list_ruleFunction · 0.85
create_dict_ruleFunction · 0.85
TOMLDecodeErrorClass · 0.85
skip_commentFunction · 0.85
finalize_pendingMethod · 0.80
OutputClass · 0.70
replaceMethod · 0.45

Tested by 12

test_pickleMethod · 0.40
test_compat_unpickleMethod · 0.40
test_compat_unpickleMethod · 0.40
test_compat_unpickleMethod · 0.40
test_compat_unpickleMethod · 0.40
test_compat_unpickleMethod · 0.40
test_pickle_dump_loadFunction · 0.40
test_pickleMethod · 0.40
check_keys_reuseMethod · 0.40