Skip to content

Commit

Permalink
pre-commit
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasDedieu committed Dec 16, 2024
1 parent bdfa408 commit 3ab4e4d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
12 changes: 8 additions & 4 deletions confit/utils/xjson.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ def __init__(self, value: str):
self.value = value
super().__init__(f"Malformed value: {value!r}")


def loads(s: str):
"""
Load an extended JSON string into a python object.
Expand All @@ -266,15 +267,18 @@ def loads(s: str):
return XJsonTransformer(s).transform(_json_parser.parse(s))
except Exception:
# Detect malformed strings with unmatched quotes
if (s.startswith("'") and not s.endswith("'")) or (s.startswith('"') and not s.endswith('"')):
if (s.startswith("'") and not s.endswith("'")) or (
s.startswith('"') and not s.endswith('"')
):
raise MalformedValueError(s)

# Detect suspicious malformed patterns
if any(char in s for char in ",{}[]") and not s.startswith(("'", '"')):
raise MalformedValueError(s)

return s



def dumps(o: Any):
"""
Dump a python object into an extended JSON string.
Expand Down
4 changes: 3 additions & 1 deletion tests/test_config_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,8 @@ def test_very_long_yaml_config():
config = Config.from_yaml_str(
"""\
a: {}
""".format("x" * 4200)
""".format(
"x" * 4200
)
)
assert config == {"a": "x" * 4200}

0 comments on commit 3ab4e4d

Please sign in to comment.