[Python-checkins] peps: Updated given statement torture test to show why renaming strategies are flawed
nick.coghlan
python-checkins at python.org
Sun Apr 10 14:05:54 CEST 2011
http://hg.python.org/peps/rev/fc2aa3ef6d34
changeset: 3863:fc2aa3ef6d34
user: Nick Coghlan <ncoghlan at gmail.com>
date: Sun Apr 10 22:05:30 2011 +1000
summary:
Updated given statement torture test to show why renaming strategies are flawed
files:
pep-3150.txt | 29 ++++++++++++++++++++++++++---
1 files changed, 26 insertions(+), 3 deletions(-)
diff --git a/pep-3150.txt b/pep-3150.txt
--- a/pep-3150.txt
+++ b/pep-3150.txt
@@ -314,16 +314,25 @@
assert d[42] == 42 given:
d = b
assert "d" not in locals()
+ y = y given:
+ x = 42
+ def f(): pass
+ y = locals("x"), f.__name__
+ assert "x" not in locals()
+ assert "f" not in locals()
+ assert y == (42, "f")
Most naive implementations will choke on the first complex assignment,
while less naive but still broken implementations will fail when
-the torture test is executed at class scope.
+the torture test is executed at class scope. Renaming based strategies
+struggle to support ``locals()`` correctly and also have problems with
+class and function ``__name__`` attributes.
And yes, that's a perfectly well-defined assignment statement. Insane,
you might rightly say, but legal::
>>> def f(x): return x
- ...
+ ...
>>> x = 42
>>> b = {}
>>> a = b[f(a)] = x
@@ -349,6 +358,10 @@
* Return-based semantics struggle with complex assignment statements
like the one in the torture test
+The second thought is generally some kind of hidden renaming strategy. This
+also creates problems, as Python exposes variables names via the ``locals()``
+dictionary and class and function ``__name__`` attributes.
+
The most promising approach is one based on symtable analysis and
copy-in-copy-out referencing semantics to move any required name
bindings between the inner and outer scopes. The torture test above
@@ -371,6 +384,16 @@
# Nothing to copy out (not an assignment)
_anon2()
assert "d" not in locals()
+ def _anon3() # Nothing to copy in (no references to other variables)
+ x = 42
+ def f(): pass
+ y = locals("x"), f.__name__
+ y = y # Assuming no optimisation of special cases
+ return y # 'y' reference copied out
+ y = _anon3()
+ assert "x" not in locals()
+ assert "f" not in locals()
+ assert y == (42, "f")
However, as noted in the abstract, an actual implementation of
this idea has never been tried.
@@ -417,7 +440,7 @@
This document has been placed in the public domain.
-
+
..
Local Variables:
mode: indented-text
--
Repository URL: http://hg.python.org/peps
More information about the Python-checkins
mailing list