Merge pull request #1424 from ryanpetrello/more-yaml-unsafe

add more edge case handling for yaml unsafe marking
This commit is contained in:
Ryan Petrello 2018-04-19 09:32:25 -04:00 committed by GitHub
commit 3c7a0b5505
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 1 deletions

View File

@ -20,6 +20,18 @@ def test_raw_string():
assert safe_dump('foo') == "!unsafe 'foo'\n"
def test_kv_null():
assert safe_dump({'a': None}) == "!unsafe 'a': null\n"
def test_kv_null_safe():
assert safe_dump({'a': None}, {'a': None}) == "a: null\n"
def test_kv_null_unsafe():
assert safe_dump({'a': ''}, {'a': None}) == "!unsafe 'a': !unsafe ''\n"
def test_kv_int():
assert safe_dump({'a': 1}) == "!unsafe 'a': 1\n"

View File

@ -58,7 +58,7 @@ def safe_dump(x, safe_dict=None):
# equality matches (and consider those branches safe)
for k, v in x.items():
dumper = yaml.SafeDumper
if safe_dict.get(k) != v:
if k not in safe_dict or safe_dict.get(k) != v:
dumper = SafeStringDumper
yamls.append(yaml.dump_all(
[{k: v}],