cpython: d296cf1600a8 (original) (raw)

--- a/Doc/reference/compound_stmts.rst +++ b/Doc/reference/compound_stmts.rst @@ -493,14 +493,15 @@ case the parameter's default value is su value, all following parameters up until the "*" must also have a default value --- this is a syntactic restriction that is not expressed by the grammar. -Default parameter values are evaluated when the function definition is -executed. This means that the expression is evaluated once, when the function -is defined, and that the same "pre-computed" value is used for each call. This -is especially important to understand when a default parameter is a mutable -object, such as a list or a dictionary: if the function modifies the object -(e.g. by appending an item to a list), the default value is in effect modified. -This is generally not what was intended. A way around this is to use None -as the default, and explicitly test for it in the body of the function, e.g.:: +Default parameter values are evaluated from left to right when the function +definition is executed. This means that the expression is evaluated once, when +the function is defined, and that the same "pre-computed" value is used for each +call. This is especially important to understand when a default parameter is a +mutable object, such as a list or a dictionary: if the function modifies the +object (e.g. by appending an item to a list), the default value is in effect +modified. This is generally not what was intended. A way around this is to use +None as the default, and explicitly test for it in the body of the function, +e.g.:: def whats_on_the_telly(penguin=None): if penguin is None:

--- a/Lib/importlib/_bootstrap.py +++ b/Lib/importlib/_bootstrap.py @@ -396,13 +396,15 @@ Known values: 3210 (added size modulo 2**32 to the pyc header) Python 3.3a1 3220 (changed PEP 380 implementation) Python 3.3a4 3230 (revert changes to implicit class closure)

MAGIC must change whenever the bytecode emitted by the compiler may no longer be understood by older implementations of the eval loop (usually due to the addition of new opcodes). """ -_RAW_MAGIC_NUMBER = 3230 | ord('\r') << 16 | ord('\n') << 24 +_RAW_MAGIC_NUMBER = 3240 | ord('\r') << 16 | ord('\n') << 24 _MAGIC_BYTES = bytes(_RAW_MAGIC_NUMBER >> n & 0xff for n in range(0, 25, 8)) _PYCACHE = 'pycache'

--- a/Lib/test/test_keywordonlyarg.py +++ b/Lib/test/test_keywordonlyarg.py @@ -176,6 +176,14 @@ class KeywordOnlyArgTestCase(unittest.Te return __a self.assertEqual(X().f(), 42)

+ def test_main(): run_unittest(KeywordOnlyArgTestCase)

--- a/Misc/NEWS +++ b/Misc/NEWS @@ -10,6 +10,9 @@ What's New in Python 3.4.0 Alpha 1? Core and Builtins ----------------- +- Issue #16967: In function definition, evaluate positional defaults before

--- a/Python/ceval.c +++ b/Python/ceval.c @@ -2901,23 +2901,6 @@ PyEval_EvalFrameEx(PyFrameObject f, int } / XXX Maybe this should be a separate opcode? */

@@ -2945,6 +2928,23 @@ PyEval_EvalFrameEx(PyFrameObject *f, int } Py_DECREF(defs); }

--- a/Python/compile.c +++ b/Python/compile.c @@ -1565,6 +1565,8 @@ compiler_function(struct compiler *c, st if (!compiler_decorators(c, decos)) return 0;

@@ -1572,8 +1574,6 @@ compiler_function(struct compiler *c, st return 0; kw_default_count = res; }

--- a/Python/importlib.h +++ b/Python/importlib.h @@ -756,7 +756,7 @@ unsigned char _Py_M__importlib[] = { 114,101,109,111,118,101,100,49,1,0,0,115,2,0,0,0, 0,8,117,25,0,0,0,95,99,97,108,108,95,119,105,116, 104,95,102,114,97,109,101,115,95,114,101,109,111,118,101,100,