[pypy-commit] pypy py3k: make None, True, and False keywords
gutworth
noreply at buildbot.pypy.org
Wed Mar 14 23:00:38 CET 2012
Author: Benjamin Peterson <benjamin at python.org>
Branch: py3k
Changeset: r53611:b75666e56661
Date: 2012-03-14 17:00 -0500
http://bitbucket.org/pypy/pypy/changeset/b75666e56661/
Log: make None, True, and False keywords
diff --git a/pypy/interpreter/astcompiler/optimize.py b/pypy/interpreter/astcompiler/optimize.py
--- a/pypy/interpreter/astcompiler/optimize.py
+++ b/pypy/interpreter/astcompiler/optimize.py
@@ -261,11 +261,18 @@
return rep
def visit_Name(self, name):
- # Turn loading None into a constant lookup. Eventaully, we can do this
- # for True and False, too.
- if name.id == "None":
- assert name.ctx == ast.Load
- return ast.Const(self.space.w_None, name.lineno, name.col_offset)
+ """Turn loading None, True, and False into a constant lookup."""
+ space = self.space
+ iden = name.id
+ w_const = None
+ if iden == "None":
+ w_const = space.w_None
+ elif iden == "True":
+ w_const = space.w_True
+ elif iden == "False":
+ w_const = space.w_False
+ if w_const is not None:
+ return ast.Const(w_const, name.lineno, name.col_offset)
return name
def visit_Tuple(self, tup):
diff --git a/pypy/interpreter/astcompiler/test/test_astbuilder.py b/pypy/interpreter/astcompiler/test/test_astbuilder.py
--- a/pypy/interpreter/astcompiler/test/test_astbuilder.py
+++ b/pypy/interpreter/astcompiler/test/test_astbuilder.py
@@ -795,15 +795,11 @@
"from x import y as %s",
"for %s in x: pass",
)
- for name in ("None", "__debug__"):
+ for name in "__debug__",:
for template in invalid:
input = template % (name,)
exc = py.test.raises(SyntaxError, self.get_ast, input).value
assert exc.msg == "cannot assign to %s" % (name,)
- # This is ok.
- self.get_ast("from None import x")
- self.get_ast("from x import None as y")
- self.get_ast("import None as x")
def test_lambda(self):
lam = self.get_first_expr("lambda x: expr")
diff --git a/pypy/interpreter/pyparser/data/Grammar3.2 b/pypy/interpreter/pyparser/data/Grammar3.2
--- a/pypy/interpreter/pyparser/data/Grammar3.2
+++ b/pypy/interpreter/pyparser/data/Grammar3.2
@@ -107,7 +107,7 @@
atom: ('(' [yield_expr|testlist_comp] ')' |
'[' [testlist_comp] ']' |
'{' [dictorsetmaker] '}' |
- NAME | NUMBER | STRING+ | '...')
+ NAME | NUMBER | STRING+ | '...' | 'None' | 'True' | 'False')
testlist_comp: (test|star_expr) ( comp_for | (',' (test|star_expr))* [','] )
lambdef: 'lambda' [varargslist] ':' test
trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
diff --git a/pypy/interpreter/test/test_compiler.py b/pypy/interpreter/test/test_compiler.py
--- a/pypy/interpreter/test/test_compiler.py
+++ b/pypy/interpreter/test/test_compiler.py
@@ -788,13 +788,15 @@
output = s.getvalue()
assert output.count('LOAD_CONST') == 1
- def test_none_constant(self):
+ def test_constant_name(self):
import opcode
- co = compile("def f(): return None", "<test>", "exec").co_consts[0]
- assert "None" not in co.co_names
- co = co.co_code
- op = co[0] + (co[1] << 8)
- assert op == opcode.opmap["LOAD_CONST"]
+ for name in "None", "True", "False":
+ snip = "def f(): return " + name
+ co = compile(snip, "<test>", "exec").co_consts[0]
+ assert name not in co.co_names
+ co = co.co_code
+ op = co[0] + (co[1] << 8)
+ assert op == opcode.opmap["LOAD_CONST"]
def test_tuple_constants(self):
ns = {}
More information about the pypy-commit
mailing list