Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
59c795a2
Commit
59c795a2
authored
Feb 10, 2013
by
Benjamin Peterson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
evaluate positional defaults before keyword-only defaults (closes #16967)
parent
9d3041b0
Changes
7
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
161 additions
and
147 deletions
+161
-147
Doc/reference/compound_stmts.rst
Doc/reference/compound_stmts.rst
+9
-8
Lib/importlib/_bootstrap.py
Lib/importlib/_bootstrap.py
+3
-1
Lib/test/test_keywordonlyarg.py
Lib/test/test_keywordonlyarg.py
+8
-0
Misc/NEWS
Misc/NEWS
+3
-0
Python/ceval.c
Python/ceval.c
+17
-17
Python/compile.c
Python/compile.c
+2
-2
Python/importlib.h
Python/importlib.h
+119
-119
No files found.
Doc/reference/compound_stmts.rst
View file @
59c795a2
...
...
@@ -493,14 +493,15 @@ case the parameter's default value is substituted. If a parameter has a default
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:
...
...
Lib/importlib/_bootstrap.py
View file @
59c795a2
...
...
@@ -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)
Python 3.4a1 3240 (evaluate positional default arguments before
keyword-only defaults)
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
=
32
3
0
|
ord
(
'
\
r
'
)
<<
16
|
ord
(
'
\
n
'
)
<<
24
_RAW_MAGIC_NUMBER
=
32
4
0
|
ord
(
'
\
r
'
)
<<
16
|
ord
(
'
\
n
'
)
<<
24
_MAGIC_BYTES
=
bytes
(
_RAW_MAGIC_NUMBER
>>
n
&
0xff
for
n
in
range
(
0
,
25
,
8
))
_PYCACHE
=
'__pycache__'
...
...
Lib/test/test_keywordonlyarg.py
View file @
59c795a2
...
...
@@ -176,6 +176,14 @@ class KeywordOnlyArgTestCase(unittest.TestCase):
return
__a
self
.
assertEqual
(
X
().
f
(),
42
)
def
test_default_evaluation_order
(
self
):
# See issue 16967
a
=
42
with
self
.
assertRaises
(
NameError
)
as
err
:
def
f
(
v
=
a
,
x
=
b
,
*
,
y
=
c
,
z
=
d
):
pass
self
.
assertEqual
(
str
(
err
.
exception
),
"global name 'b' is not defined"
)
def
test_main
():
run_unittest
(
KeywordOnlyArgTestCase
)
...
...
Misc/NEWS
View file @
59c795a2
...
...
@@ -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
keyword-only defaults.
- Issue #17173: Remove uses of locale-dependent C functions (isalpha() etc.)
in the interpreter.
...
...
Python/ceval.c
View file @
59c795a2
...
...
@@ -2901,23 +2901,6 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
}
/* XXX Maybe this should be a separate opcode? */
if
(
posdefaults
>
0
)
{
PyObject
*
defs
=
PyTuple_New
(
posdefaults
);
if
(
defs
==
NULL
)
{
Py_DECREF
(
func
);
goto
error
;
}
while
(
--
posdefaults
>=
0
)
PyTuple_SET_ITEM
(
defs
,
posdefaults
,
POP
());
if
(
PyFunction_SetDefaults
(
func
,
defs
)
!=
0
)
{
/* Can't happen unless
PyFunction_SetDefaults changes. */
Py_DECREF
(
defs
);
Py_DECREF
(
func
);
goto
error
;
}
Py_DECREF
(
defs
);
}
if
(
kwdefaults
>
0
)
{
PyObject
*
defs
=
PyDict_New
();
if
(
defs
==
NULL
)
{
...
...
@@ -2945,6 +2928,23 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
}
Py_DECREF
(
defs
);
}
if
(
posdefaults
>
0
)
{
PyObject
*
defs
=
PyTuple_New
(
posdefaults
);
if
(
defs
==
NULL
)
{
Py_DECREF
(
func
);
goto
error
;
}
while
(
--
posdefaults
>=
0
)
PyTuple_SET_ITEM
(
defs
,
posdefaults
,
POP
());
if
(
PyFunction_SetDefaults
(
func
,
defs
)
!=
0
)
{
/* Can't happen unless
PyFunction_SetDefaults changes. */
Py_DECREF
(
defs
);
Py_DECREF
(
func
);
goto
error
;
}
Py_DECREF
(
defs
);
}
PUSH
(
func
);
DISPATCH
();
}
...
...
Python/compile.c
View file @
59c795a2
...
...
@@ -1565,6 +1565,8 @@ compiler_function(struct compiler *c, stmt_ty s)
if
(
!
compiler_decorators
(
c
,
decos
))
return
0
;
if
(
args
->
defaults
)
VISIT_SEQ
(
c
,
expr
,
args
->
defaults
);
if
(
args
->
kwonlyargs
)
{
int
res
=
compiler_visit_kwonlydefaults
(
c
,
args
->
kwonlyargs
,
args
->
kw_defaults
);
...
...
@@ -1572,8 +1574,6 @@ compiler_function(struct compiler *c, stmt_ty s)
return
0
;
kw_default_count
=
res
;
}
if
(
args
->
defaults
)
VISIT_SEQ
(
c
,
expr
,
args
->
defaults
);
num_annotations
=
compiler_visit_annotations
(
c
,
args
,
returns
);
if
(
num_annotations
<
0
)
return
0
;
...
...
Python/importlib.h
View file @
59c795a2
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment