New submission from Bruno Dupuis:
We found some strange behaviour of the compiler in this discussion on
python-list:
http://mail.python.org/pipermail/python-list/2012-December/636104.html
The fact is, `return` and `return None` result in inconsistent bytecode
depending on the context.
Consider :
>>> import dis
>>> def f(x):
... return None
...
>>> dis.dis(f)
2 0 LOAD_CONST 0 (None)
3 RETURN_VALUE
>>> def g(x):
... return None
... print(x)
...
>>> dis.dis(g)
2 0 LOAD_GLOBAL 0 (None)
3 RETURN_VALUE
3 4 LOAD_GLOBAL 1 (print)
7 LOAD_FAST 0 (x)
10 CALL_FUNCTION 1 (1 positional, 0 keyword pair)
13 POP_TOP
`return None` statement results in LOAD_GLOBAL 0 if there is some unreachable
code after it. I first saw that as an optimization issue, but Ian Kelly's
message http://mail.python.org/pipermail/python-list/2012-December/636117.html
gives an extensive analysis and some examples:
"""
I think this should even be considered a bug, not just a missing
optimization. Consider:
>>> globals()['None'] = 42
>>> def f(x):
... return None
... print(x)
...
>>> f('test')
42
The use of the LOAD_GLOBAL allows None to effectively be reassigned.
"""
Ian also points out in this message that `return` and `return None` don't
result in the same bytecode when followed by trash code.
----------
components: Interpreter Core
messages: 176999
nosy: Horpner, bruno.dupuis, ikelly, python-dev, stevenjd
priority: normal
severity: normal
status: open
title: LOAD_GLOBAL used to load `None` under certain circumstances
versions: Python 2.7, Python 3.2, Python 3.3, Python 3.4
_______________________________________
Python tracker <[email protected]>
<http://bugs.python.org/issue16619>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com