From 7cf96a4b4bd80396252bac5e054666e3942262aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Hajdan=2C=20Jr?= Date: Fri, 26 May 2017 20:28:35 +0200 Subject: [PATCH] gclient_eval: use ordered dict MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is a step towards implementing conditions. Bug: 570091 Change-Id: I99467033082d96021854c23dcff3fc2b56f995b4 Reviewed-on: https://chromium-review.googlesource.com/517107 Reviewed-by: Andrii Shyshkalov Commit-Queue: Paweł Hajdan Jr. --- gclient_eval.py | 6 ++++-- tests/gclient_eval_unittest.py | 8 ++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/gclient_eval.py b/gclient_eval.py index 1652eb7d30..b8ed31c707 100644 --- a/gclient_eval.py +++ b/gclient_eval.py @@ -3,6 +3,7 @@ # found in the LICENSE file. import ast +import collections from third_party import schema @@ -115,8 +116,9 @@ def _gclient_eval(node_or_string, global_scope, filename=''): elif isinstance(node, ast.List): return list(map(_convert, node.elts)) elif isinstance(node, ast.Dict): - return dict((_convert(k), _convert(v)) - for k, v in zip(node.keys, node.values)) + return collections.OrderedDict( + (_convert(k), _convert(v)) + for k, v in zip(node.keys, node.values)) elif isinstance(node, ast.Name): if node.id not in _allowed_names: raise ValueError( diff --git a/tests/gclient_eval_unittest.py b/tests/gclient_eval_unittest.py index 817f1c0072..7c6787b862 100755 --- a/tests/gclient_eval_unittest.py +++ b/tests/gclient_eval_unittest.py @@ -3,6 +3,7 @@ # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. +import itertools import logging import os import sys @@ -58,6 +59,13 @@ class GClientEvalTest(unittest.TestCase): self.assertIn( 'unexpected AST node: <_ast.ListComp object', str(cm.exception)) + def test_dict_ordered(self): + for test_case in itertools.permutations(range(4)): + input_data = ['{'] + ['"%s": "%s",' % (n, n) for n in test_case] + ['}'] + expected = [(str(n), str(n)) for n in test_case] + result = gclient_eval._gclient_eval(''.join(input_data), {}) + self.assertEqual(expected, result.items()) + class GClientExecTest(unittest.TestCase): def test_basic(self):