[Python-checkins] r64296 - in python/branches/tlee-ast-optimize: Lib/test/test_optimizer.py Python/optimize.c
thomas.lee
python-checkins at python.org
Sun Jun 15 17:08:51 CEST 2008
Author: thomas.lee
Date: Sun Jun 15 17:08:51 2008
New Revision: 64296
Log:
Remove optimizations for True/False named pseudo-constants, since it breaks a bunch of 2.x semantics that may be relied upon. Added a test for enforcing the 2.6 semantics for now. Finally, added an error check for OOM to optimize_name.
Modified:
python/branches/tlee-ast-optimize/Lib/test/test_optimizer.py
python/branches/tlee-ast-optimize/Python/optimize.c
Modified: python/branches/tlee-ast-optimize/Lib/test/test_optimizer.py
==============================================================================
--- python/branches/tlee-ast-optimize/Lib/test/test_optimizer.py (original)
+++ python/branches/tlee-ast-optimize/Lib/test/test_optimizer.py Sun Jun 15 17:08:51 2008
@@ -139,7 +139,8 @@
self.assertNum(-5, "-5")
self.assertNum(True, "not 0")
self.assertNum(-3, "-+3")
- self.assertNum(False, "not True")
+ # XXX: cannot be enforced in 2.6
+ #self.assertNum(False, "not True")
self.assertNum(True, "not None")
def test_unary_failure_left_until_runtime(self):
@@ -249,7 +250,7 @@
self.assertEqual((1, 2, 3), ast.body[0].iter.value)
def test_named_constants(self):
- tests = [None, True, False]
+ tests = [None]
for obj in tests:
code = repr(obj)
@@ -273,6 +274,19 @@
self.assertEqual(_ast.Return, ast.body[0].body[0].body[1].__class__)
self.assertEqual('y', ast.body[0].body[0].body[1].value.id)
+ def test_assignment_to_true_works(self):
+ # ---------------------------------------------------------------------
+ # Please note that this test is for <2.6 to ensure that
+ # assignment to True and False are possible despite any
+ # optimizations.
+ # ---------------------------------------------------------------------
+
+ True = 0
+ assert not True
+
+ False = 1
+ assert False
+
def test_main():
test_support.run_unittest(AstOptimizerTest)
Modified: python/branches/tlee-ast-optimize/Python/optimize.c
==============================================================================
--- python/branches/tlee-ast-optimize/Python/optimize.c (original)
+++ python/branches/tlee-ast-optimize/Python/optimize.c Sun Jun 15 17:08:51 2008
@@ -34,15 +34,6 @@
else if (expr->kind == Num_kind) {
return expr->v.Num.n;
}
- else if (expr->kind == Name_kind) {
- const char* name = PyString_AS_STRING(expr->v.Name.id);
- if (strcmp(name, "True") == 0)
- return Py_True;
- else if (strcmp(name, "False") == 0)
- return Py_False;
- else if (strcmp(name, "None") == 0)
- return Py_None;
- }
else if (expr->kind == Const_kind) {
return expr->v.Const.value;
}
@@ -916,10 +907,16 @@
const char* id = PyString_AS_STRING(expr->v.Name.id);
PyObject* constvalue = NULL;
+ /* allow "assignment to none" error to naturally occur */
+ if (expr->v.Name.ctx != Load)
+ return 1;
+
if (strcmp(id, "None") == 0) {
Py_INCREF(Py_None);
constvalue = Py_None;
}
+/* this is not doable in 2.x ... */
+#if 0
else if (strcmp(id, "True") == 0) {
Py_INCREF(Py_True);
constvalue = Py_True;
@@ -928,10 +925,14 @@
Py_INCREF(Py_False);
constvalue = Py_False;
}
+#endif
- if (constvalue != NULL)
+ if (constvalue != NULL) {
*expr_ptr = Const(constvalue, expr->lineno,
expr->col_offset, arena);
+ if (*expr_ptr == NULL)
+ return 0;
+ }
return 1;
}
@@ -1007,12 +1008,7 @@
}
case Name_kind:
{
- /* we probably only want to optimize loads ... storing values
- * in a Const makes no sense!
- */
- if (expr->v.Name.ctx == Load)
- return optimize_name(expr_ptr, ste, arena);
- /* fall through */
+ return optimize_name(expr_ptr, ste, arena);
}
case Num_kind:
case Str_kind:
More information about the Python-checkins
mailing list