Skip to content

Commit

Permalink
Replace yaml.load with yaml.safe_load (#276)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fleker committed May 11, 2023
1 parent 49c0be9 commit 5a6e904
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
6 changes: 3 additions & 3 deletions cyanobyte/codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import json
import click

from yaml import load
from yaml import safe_load
try:
from yaml import CLoader as Loader
except ImportError:
Expand Down Expand Up @@ -129,7 +129,7 @@ def generate_source_file(template, peripheral, opts, template_ext,
"""
# Open peripheral file
with open(peripheral, "r") as peripheral_file:
peripheral_data = load(peripheral_file, Loader=Loader)
peripheral_data = safe_load(peripheral_file)
# Add additional metadata to the spec data
peripheral_data["version"] = _VERSION
peripheral_data["fileName"] = peripheral
Expand All @@ -139,7 +139,7 @@ def generate_source_file(template, peripheral, opts, template_ext,
options_file = open(opts, "r")
except:
options_file = pkg_resources.open_text('cyanobyte-templates', opts)
options_data = load(options_file, Loader=Loader)
options_data = safe_load(options_file)
peripheral_data["options"] = options_data

# Load imports
Expand Down
22 changes: 13 additions & 9 deletions cyanobyte/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

import sys
import json
import click
import os
import os.path as path
import click
import yaml
from yaml.constructor import ConstructorError
try:
Expand All @@ -31,13 +31,16 @@ def no_duplicates_constructor(loader, node, deep=False):
key = loader.construct_object(key_node, deep=deep)
value = loader.construct_object(value_node, deep=deep)
if key in mapping:
raise ConstructorError("while constructing a mapping", node.start_mark,
"found duplicate key (%s)" % key, key_node.start_mark)
raise ConstructorError("while constructing a mapping",
node.start_mark,
"found duplicate key (%s)" % key,
key_node.start_mark)
mapping[key] = value

return loader.construct_mapping(node, deep)

yaml.add_constructor(yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG, no_duplicates_constructor, Loader=Loader)
yaml.add_constructor(yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG,
no_duplicates_constructor, Loader=Loader)

def cyanobyte_validate(input_files):
"""
Expand All @@ -50,7 +53,8 @@ def cyanobyte_validate(input_files):
path = "cyanobyte-spec/cyanobyte.schema.json"
try:
import pkg_resources
path = pkg_resources.resource_filename('cyanobyte-spec', 'cyanobyte.schema.json')
path = pkg_resources.resource_filename('cyanobyte-spec',
'cyanobyte.schema.json')
except:pass

with open(path, "r") as schema_json:
Expand All @@ -61,7 +65,7 @@ def cyanobyte_validate(input_files):
for input_file in input_files:
with open(input_file, "r") as document_yaml:
try:
document_dict = yaml.load(document_yaml, Loader=Loader)
document_dict = yaml.safe_load(document_yaml)
validate(instance=document_dict, schema=schema)
print('✓ ' + input_file)
except (ConstructorError, ValidationError) as err:
Expand All @@ -70,9 +74,9 @@ def cyanobyte_validate(input_files):

# Dump all errors here
print('')
for e in errors:
print(e.input_file + ':')
print(e.err)
for err in errors:
print(err.input_file + ':')
print(err.err)


def unittest(input_files):
Expand Down

0 comments on commit 5a6e904

Please sign in to comment.