forked from mirrors/gecko-dev
		
	Bug 1872666 - Remove OrderedDict code from YAML file loading. r=iain,profiler-reviewers,canaltinova
`mach` requires Python 3.7+ since bug 1734402, and Python 3.7 made preserving dictionary insertion order an official part of the language. Also use `safe_load` instead of `load` because it doesn't require a loader argument and is safer (although it doesn't really matter for this use case). Differential Revision: https://phabricator.services.mozilla.com/D197497
This commit is contained in:
		
							parent
							
								
									cdab453613
								
							
						
					
					
						commit
						4387a3b625
					
				
					 6 changed files with 11 additions and 101 deletions
				
			
		|  | @ -1,5 +1,3 @@ | ||||||
| from collections import OrderedDict |  | ||||||
| 
 |  | ||||||
| import buildconfig | import buildconfig | ||||||
| import six | import six | ||||||
| import yaml | import yaml | ||||||
|  | @ -40,20 +38,7 @@ def load_yaml(yaml_path): | ||||||
|     pp.do_filter("substitution") |     pp.do_filter("substitution") | ||||||
|     pp.do_include(yaml_path) |     pp.do_include(yaml_path) | ||||||
|     contents = pp.out.getvalue() |     contents = pp.out.getvalue() | ||||||
| 
 |     return yaml.safe_load(contents) | ||||||
|     # Load into an OrderedDict to ensure order is preserved. Note: Python 3.7+ |  | ||||||
|     # also preserves ordering for normal dictionaries. |  | ||||||
|     # Code based on https://stackoverflow.com/a/21912744. |  | ||||||
|     class OrderedLoader(yaml.Loader): |  | ||||||
|         pass |  | ||||||
| 
 |  | ||||||
|     def construct_mapping(loader, node): |  | ||||||
|         loader.flatten_mapping(node) |  | ||||||
|         return OrderedDict(loader.construct_pairs(node)) |  | ||||||
| 
 |  | ||||||
|     tag = yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG |  | ||||||
|     OrderedLoader.add_constructor(tag, construct_mapping) |  | ||||||
|     return yaml.load(contents, OrderedLoader) |  | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| def cpp_arg_type(arg_type): | def cpp_arg_type(arg_type): | ||||||
|  |  | ||||||
|  | @ -4,8 +4,6 @@ | ||||||
| 
 | 
 | ||||||
| # This script generates jit/CacheIROpsGenerated.h from CacheIROps.yaml | # This script generates jit/CacheIROpsGenerated.h from CacheIROps.yaml | ||||||
| 
 | 
 | ||||||
| from collections import OrderedDict |  | ||||||
| 
 |  | ||||||
| import buildconfig | import buildconfig | ||||||
| import six | import six | ||||||
| import yaml | import yaml | ||||||
|  | @ -46,20 +44,7 @@ def load_yaml(yaml_path): | ||||||
|     pp.do_filter("substitution") |     pp.do_filter("substitution") | ||||||
|     pp.do_include(yaml_path) |     pp.do_include(yaml_path) | ||||||
|     contents = pp.out.getvalue() |     contents = pp.out.getvalue() | ||||||
| 
 |     return yaml.safe_load(contents) | ||||||
|     # Load into an OrderedDict to ensure order is preserved. Note: Python 3.7+ |  | ||||||
|     # also preserves ordering for normal dictionaries. |  | ||||||
|     # Code based on https://stackoverflow.com/a/21912744. |  | ||||||
|     class OrderedLoader(yaml.Loader): |  | ||||||
|         pass |  | ||||||
| 
 |  | ||||||
|     def construct_mapping(loader, node): |  | ||||||
|         loader.flatten_mapping(node) |  | ||||||
|         return OrderedDict(loader.construct_pairs(node)) |  | ||||||
| 
 |  | ||||||
|     tag = yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG |  | ||||||
|     OrderedLoader.add_constructor(tag, construct_mapping) |  | ||||||
|     return yaml.load(contents, OrderedLoader) |  | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| # Information for generating CacheIRWriter code for a single argument. Tuple | # Information for generating CacheIRWriter code for a single argument. Tuple | ||||||
|  | @ -476,7 +461,7 @@ def generate_cacheirops_header(c_out, yaml_path): | ||||||
|         name = op["name"] |         name = op["name"] | ||||||
| 
 | 
 | ||||||
|         args = op["args"] |         args = op["args"] | ||||||
|         assert args is None or isinstance(args, OrderedDict) |         assert args is None or isinstance(args, dict) | ||||||
| 
 | 
 | ||||||
|         shared = op["shared"] |         shared = op["shared"] | ||||||
|         assert isinstance(shared, bool) |         assert isinstance(shared, bool) | ||||||
|  |  | ||||||
|  | @ -5,8 +5,6 @@ | ||||||
| # This script generates jit/LIROpsGenerated.h (list of LIR instructions) | # This script generates jit/LIROpsGenerated.h (list of LIR instructions) | ||||||
| # from LIROps.yaml. | # from LIROps.yaml. | ||||||
| 
 | 
 | ||||||
| from collections import OrderedDict |  | ||||||
| 
 |  | ||||||
| import buildconfig | import buildconfig | ||||||
| import six | import six | ||||||
| import yaml | import yaml | ||||||
|  | @ -37,20 +35,7 @@ def load_yaml(yaml_path): | ||||||
|     pp.do_filter("substitution") |     pp.do_filter("substitution") | ||||||
|     pp.do_include(yaml_path) |     pp.do_include(yaml_path) | ||||||
|     contents = pp.out.getvalue() |     contents = pp.out.getvalue() | ||||||
| 
 |     return yaml.safe_load(contents) | ||||||
|     # Load into an OrderedDict to ensure order is preserved. Note: Python 3.7 |  | ||||||
|     # also preserves ordering for normal dictionaries. |  | ||||||
|     # Code based on https://stackoverflow.com/a/21912744. |  | ||||||
|     class OrderedLoader(yaml.Loader): |  | ||||||
|         pass |  | ||||||
| 
 |  | ||||||
|     def construct_mapping(loader, node): |  | ||||||
|         loader.flatten_mapping(node) |  | ||||||
|         return OrderedDict(loader.construct_pairs(node)) |  | ||||||
| 
 |  | ||||||
|     tag = yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG |  | ||||||
|     OrderedLoader.add_constructor(tag, construct_mapping) |  | ||||||
|     return yaml.load(contents, OrderedLoader) |  | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| def generate_header(c_out, includeguard, contents): | def generate_header(c_out, includeguard, contents): | ||||||
|  | @ -256,10 +241,10 @@ def generate_lir_header(c_out, yaml_path): | ||||||
|                 assert result_types[result_type] |                 assert result_types[result_type] | ||||||
| 
 | 
 | ||||||
|             operands = op.get("operands", None) |             operands = op.get("operands", None) | ||||||
|             assert operands is None or OrderedDict |             assert operands is None or isinstance(operands, dict) | ||||||
| 
 | 
 | ||||||
|             arguments = op.get("arguments", None) |             arguments = op.get("arguments", None) | ||||||
|             assert arguments is None or isinstance(arguments, OrderedDict) |             assert arguments is None or isinstance(arguments, dict) | ||||||
| 
 | 
 | ||||||
|             num_temps = op.get("num_temps", 0) |             num_temps = op.get("num_temps", 0) | ||||||
|             assert num_temps is None or int |             assert num_temps is None or int | ||||||
|  |  | ||||||
|  | @ -5,8 +5,6 @@ | ||||||
| # This script generates jit/MIROpsGenerated.h (list of MIR instructions) | # This script generates jit/MIROpsGenerated.h (list of MIR instructions) | ||||||
| # from MIROps.yaml, as well as MIR op definitions. | # from MIROps.yaml, as well as MIR op definitions. | ||||||
| 
 | 
 | ||||||
| from collections import OrderedDict |  | ||||||
| 
 |  | ||||||
| import buildconfig | import buildconfig | ||||||
| import six | import six | ||||||
| import yaml | import yaml | ||||||
|  | @ -47,20 +45,7 @@ def load_yaml(yaml_path): | ||||||
|     pp.do_filter("substitution") |     pp.do_filter("substitution") | ||||||
|     pp.do_include(yaml_path) |     pp.do_include(yaml_path) | ||||||
|     contents = pp.out.getvalue() |     contents = pp.out.getvalue() | ||||||
| 
 |     return yaml.safe_load(contents) | ||||||
|     # Load into an OrderedDict to ensure order is preserved. Note: Python 3.7+ |  | ||||||
|     # also preserves ordering for normal dictionaries. |  | ||||||
|     # Code based on https://stackoverflow.com/a/21912744. |  | ||||||
|     class OrderedLoader(yaml.Loader): |  | ||||||
|         pass |  | ||||||
| 
 |  | ||||||
|     def construct_mapping(loader, node): |  | ||||||
|         loader.flatten_mapping(node) |  | ||||||
|         return OrderedDict(loader.construct_pairs(node)) |  | ||||||
| 
 |  | ||||||
|     tag = yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG |  | ||||||
|     OrderedLoader.add_constructor(tag, construct_mapping) |  | ||||||
|     return yaml.load(contents, OrderedLoader) |  | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| type_policies = { | type_policies = { | ||||||
|  | @ -322,10 +307,10 @@ def generate_mir_header(c_out, yaml_path): | ||||||
| 
 | 
 | ||||||
|         if gen_boilerplate: |         if gen_boilerplate: | ||||||
|             operands = op.get("operands", None) |             operands = op.get("operands", None) | ||||||
|             assert operands is None or isinstance(operands, OrderedDict) |             assert operands is None or isinstance(operands, dict) | ||||||
| 
 | 
 | ||||||
|             arguments = op.get("arguments", None) |             arguments = op.get("arguments", None) | ||||||
|             assert arguments is None or isinstance(arguments, OrderedDict) |             assert arguments is None or isinstance(arguments, dict) | ||||||
| 
 | 
 | ||||||
|             no_type_policy = op.get("type_policy", None) |             no_type_policy = op.get("type_policy", None) | ||||||
|             assert no_type_policy in (None, "none") |             assert no_type_policy in (None, "none") | ||||||
|  |  | ||||||
|  | @ -1,5 +1,3 @@ | ||||||
| from collections import OrderedDict |  | ||||||
| 
 |  | ||||||
| import buildconfig | import buildconfig | ||||||
| import six | import six | ||||||
| import yaml | import yaml | ||||||
|  | @ -40,20 +38,7 @@ def load_yaml(yaml_path): | ||||||
|     pp.do_filter("substitution") |     pp.do_filter("substitution") | ||||||
|     pp.do_include(yaml_path) |     pp.do_include(yaml_path) | ||||||
|     contents = pp.out.getvalue() |     contents = pp.out.getvalue() | ||||||
| 
 |     return yaml.safe_load(contents) | ||||||
|     # Load into an OrderedDict to ensure order is preserved. Note: Python 3.7+ |  | ||||||
|     # also preserves ordering for normal dictionaries. |  | ||||||
|     # Code based on https://stackoverflow.com/a/21912744. |  | ||||||
|     class OrderedLoader(yaml.Loader): |  | ||||||
|         pass |  | ||||||
| 
 |  | ||||||
|     def construct_mapping(loader, node): |  | ||||||
|         loader.flatten_mapping(node) |  | ||||||
|         return OrderedDict(loader.construct_pairs(node)) |  | ||||||
| 
 |  | ||||||
|     tag = yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG |  | ||||||
|     OrderedLoader.add_constructor(tag, construct_mapping) |  | ||||||
|     return yaml.load(contents, OrderedLoader) |  | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| def cppBool(v): | def cppBool(v): | ||||||
|  |  | ||||||
|  | @ -5,8 +5,6 @@ | ||||||
| # This script generates ProfilingCategoryList.h and profiling_categories.rs | # This script generates ProfilingCategoryList.h and profiling_categories.rs | ||||||
| # files from profiling_categories.yaml. | # files from profiling_categories.yaml. | ||||||
| 
 | 
 | ||||||
| from collections import OrderedDict |  | ||||||
| 
 |  | ||||||
| import yaml | import yaml | ||||||
| 
 | 
 | ||||||
| CPP_HEADER_TEMPLATE = """\ | CPP_HEADER_TEMPLATE = """\ | ||||||
|  | @ -104,21 +102,8 @@ def generate_rust_file(c_out, contents): | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| def load_yaml(yaml_path): | def load_yaml(yaml_path): | ||||||
|     # Load into an OrderedDict to ensure order is preserved. Note: Python 3.7+ |  | ||||||
|     # also preserves ordering for normal dictionaries. |  | ||||||
|     # Code based on https://stackoverflow.com/a/21912744. |  | ||||||
|     class OrderedLoader(yaml.Loader): |  | ||||||
|         pass |  | ||||||
| 
 |  | ||||||
|     def construct_mapping(loader, node): |  | ||||||
|         loader.flatten_mapping(node) |  | ||||||
|         return OrderedDict(loader.construct_pairs(node)) |  | ||||||
| 
 |  | ||||||
|     tag = yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG |  | ||||||
|     OrderedLoader.add_constructor(tag, construct_mapping) |  | ||||||
| 
 |  | ||||||
|     file_handler = open(yaml_path) |     file_handler = open(yaml_path) | ||||||
|     return yaml.load(file_handler, OrderedLoader) |     return yaml.safe_load(file_handler) | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| def generate_category_macro(name, label, color, subcategories): | def generate_category_macro(name, label, color, subcategories): | ||||||
|  |  | ||||||
		Loading…
	
		Reference in a new issue
	
	 Jan de Mooij
						Jan de Mooij