Commit 30523b2c authored by Gustavo Niemeyer's avatar Gustavo Niemeyer

Fixing bug

[#448679] Left to right

* Python/compile.c
  (com_dictmaker): Reordered evaluation of dictionaries to follow strict
  LTR evaluation.

* Lib/compiler/pycodegen.py
  (CodeGenerator.visitDict): Reordered evaluation of dictionaries to
  follow strict LTR evaluation.

* Doc/ref/ref5.tex
  Documented the general LTR evaluation order idea.

* Misc/NEWS
  Documented change in evaluation order of dictionaries.
parent b42d660f
...@@ -1026,6 +1026,24 @@ tuple, but rather yields the value of that expression. ...@@ -1026,6 +1026,24 @@ tuple, but rather yields the value of that expression.
\code{()}.) \code{()}.)
\indexii{trailing}{comma} \indexii{trailing}{comma}
\section{Evaluation order\label{evalorder}}
\indexii{evaluation}{order}
Python evaluates expressions from left to right. Notice that while
evaluating an assignment, the right-hand side is evaluated before
the left-hand side.
In the following lines, expressions will be evaluated in the
arithmetic order of their suffixes:
\begin{verbatim}
expr1, expr2, expr3, expr4
(expr1, expr2, expr3, expr4)
{expr1: expr2, expr3: expr4}
expr1 + expr2 * (expr3 - expr4)
func(expr1, expr2, *expr3, **expr4)
expr3, expr4 = expr1, expr2
\end{verbatim}
\section{Summary\label{summary}} \section{Summary\label{summary}}
......
...@@ -1129,9 +1129,9 @@ class CodeGenerator: ...@@ -1129,9 +1129,9 @@ class CodeGenerator:
self.emit('SET_LINENO', lineno2) self.emit('SET_LINENO', lineno2)
lineno = lineno2 lineno = lineno2
self.emit('DUP_TOP') self.emit('DUP_TOP')
self.visit(v)
self.emit('ROT_TWO')
self.visit(k) self.visit(k)
self.visit(v)
self.emit('ROT_THREE')
self.emit('STORE_SUBSCR') self.emit('STORE_SUBSCR')
class NestedScopeMixin: class NestedScopeMixin:
......
...@@ -331,6 +331,10 @@ Core and builtins ...@@ -331,6 +331,10 @@ Core and builtins
- sys.exit() inadvertently allowed more than one argument. - sys.exit() inadvertently allowed more than one argument.
An exception will now be raised if more than one argument is used. An exception will now be raised if more than one argument is used.
- Changed evaluation order of dictionaries to conform to the general
left to right evaluation order rule. Now {f1(): f2()} will evaluate
f1 first.
Extension modules Extension modules
----------------- -----------------
......
...@@ -1529,9 +1529,9 @@ com_dictmaker(struct compiling *c, node *n) ...@@ -1529,9 +1529,9 @@ com_dictmaker(struct compiling *c, node *n)
It wants the stack to look like (value) (dict) (key) */ It wants the stack to look like (value) (dict) (key) */
com_addbyte(c, DUP_TOP); com_addbyte(c, DUP_TOP);
com_push(c, 1); com_push(c, 1);
com_node(c, CHILD(n, i+2)); /* value */
com_addbyte(c, ROT_TWO);
com_node(c, CHILD(n, i)); /* key */ com_node(c, CHILD(n, i)); /* key */
com_node(c, CHILD(n, i+2)); /* value */
com_addbyte(c, ROT_THREE);
com_addbyte(c, STORE_SUBSCR); com_addbyte(c, STORE_SUBSCR);
com_pop(c, 3); com_pop(c, 3);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment