Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ee85339

Browse files
committed
in dict displays, evaluate the key before the value (closes #11205)
Patch partially by Steve Dougherty.
1 parent 44e6258 commit ee85339

6 files changed

Lines changed: 127 additions & 113 deletions

File tree

Lib/importlib/_bootstrap_external.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,12 +221,13 @@ def _write_atomic(path, data, mode=0o666):
221221
# Python 3.4rc2 3310 (alter __qualname__ computation)
222222
# Python 3.5a0 3320 (matrix multiplication operator)
223223
# Python 3.5b1 3330 (PEP 448: Additional Unpacking Generalizations)
224+
# Python 3.5b2 3340 (fix dictionary display evaluation order #11205)
224225
#
225226
# MAGIC must change whenever the bytecode emitted by the compiler may no
226227
# longer be understood by older implementations of the eval loop (usually
227228
# due to the addition of new opcodes).
228229

229-
MAGIC_NUMBER = (3330).to_bytes(2, 'little') + b'\r\n'
230+
MAGIC_NUMBER = (3340).to_bytes(2, 'little') + b'\r\n'
230231
_RAW_MAGIC_NUMBER = int.from_bytes(MAGIC_NUMBER, 'little') # For import.c
231232

232233
_PYCACHE = '__pycache__'

Lib/test/test_compile.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,17 @@ def test_compile_ast(self):
461461
ast.body = [_ast.BoolOp()]
462462
self.assertRaises(TypeError, compile, ast, '<ast>', 'exec')
463463

464+
def test_dict_evaluation_order(self):
465+
i = 0
466+
467+
def f():
468+
nonlocal i
469+
i += 1
470+
return i
471+
472+
d = {f(): f(), f(): f()}
473+
self.assertEqual(d, {1: 2, 3: 4})
474+
464475
@support.cpython_only
465476
def test_same_filename_used(self):
466477
s = """def f(): pass\ndef g(): pass"""

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ Release date: 2015-07-05
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #11205: In dictionary displays, evaluate the key before the value.
14+
1315
- Issue #24285: Fixed regression that prevented importing extension modules
1416
from inside packages. Patch by Petr Viktorin.
1517

Python/ceval.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2584,8 +2584,8 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
25842584
goto error;
25852585
while (--oparg >= 0) {
25862586
int err;
2587-
PyObject *key = TOP();
2588-
PyObject *value = SECOND();
2587+
PyObject *value = TOP();
2588+
PyObject *key = SECOND();
25892589
STACKADJ(-2);
25902590
err = PyDict_SetItem(map, key, value);
25912591
Py_DECREF(value);

Python/compile.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3138,8 +3138,8 @@ compiler_dict(struct compiler *c, expr_ty e)
31383138
containers++;
31393139
}
31403140
else {
3141-
VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
31423141
VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
3142+
VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
31433143
elements++;
31443144
}
31453145
}
@@ -3287,8 +3287,8 @@ compiler_call_helper(struct compiler *c,
32873287
}
32883288
else if (nsubkwargs) {
32893289
/* A keyword argument and we already have a dict. */
3290-
VISIT(c, expr, kw->value);
32913290
ADDOP_O(c, LOAD_CONST, kw->arg, consts);
3291+
VISIT(c, expr, kw->value);
32923292
nseen++;
32933293
}
32943294
else {

0 commit comments

Comments
 (0)