[py-svn] commit/pytest: 2 new changesets

Bitbucket commits-noreply at bitbucket.org
Tue Aug 30 16:34:31 CEST 2011


2 new changesets in pytest:

http://bitbucket.org/hpk42/pytest/changeset/fb6709054717/
changeset:   fb6709054717
user:        gutworth
date:        2011-08-30 06:24:57
summary:     clear instead of deleting temporary assertion variables
affected #:  1 file (482 bytes)

--- a/_pytest/assertion/rewrite.py	Tue Aug 30 00:12:07 2011 -0400
+++ b/_pytest/assertion/rewrite.py	Tue Aug 30 00:24:57 2011 -0400
@@ -374,7 +374,7 @@
         """Get a new variable."""
         # Use a character invalid in python identifiers to avoid clashing.
         name = "@py_assert" + str(next(self.variable_counter))
-        self.variables[self.cond_chain].add(name)
+        self.variables.append(name)
         return name
 
     def assign(self, expr):
@@ -437,7 +437,7 @@
             return [assert_]
         self.statements = []
         self.cond_chain = ()
-        self.variables = collections.defaultdict(set)
+        self.variables = []
         self.variable_counter = itertools.count()
         self.stack = []
         self.on_failure = []
@@ -459,22 +459,11 @@
         else:
             raise_ = ast.Raise(exc, None, None)
         body.append(raise_)
-        # Delete temporary variables. This requires a bit cleverness about the
-        # order, so we don't delete variables that are themselves conditions for
-        # later variables.
-        for chain in sorted(self.variables, key=len, reverse=True):
-            if chain:
-                where = []
-                if len(chain) > 1:
-                    cond = ast.BoolOp(ast.And(), list(chain))
-                else:
-                    cond = chain[0]
-                self.statements.append(ast.If(cond, where, []))
-            else:
-                where = self.statements
-            v = self.variables[chain]
-            names = [ast.Name(name, ast.Del()) for name in v]
-            where.append(ast.Delete(names))
+        # Clear temporary variables by setting them to None.
+        if self.variables:
+            variables = [ast.Name(name, ast.Store()) for name in self.variables]
+            clear = ast.Assign(variables, ast.Name("None", ast.Load()))
+            self.statements.append(clear)
         # Fix line numbers.
         for stmt in self.statements:
             set_location(stmt, assert_.lineno, assert_.col_offset)


http://bitbucket.org/hpk42/pytest/changeset/6fa4598c3bc4/
changeset:   6fa4598c3bc4
user:        gutworth
date:        2011-08-30 16:34:21
summary:     every boolop operand must have it's own format context (fixes #69)
affected #:  3 files (423 bytes)

--- a/CHANGELOG	Tue Aug 30 00:24:57 2011 -0400
+++ b/CHANGELOG	Tue Aug 30 10:34:21 2011 -0400
@@ -1,6 +1,7 @@
 Changes between 2.1.1 and [NEXT VERSION]
 ----------------------------------------
 
+- fix issue69 / assertion rewriting fixed on some boolean operations
 - fix issue68 / packages now work with assertion rewriting
 - fix issue66: use different assertion rewriting caches when the -O option is passed
 


--- a/_pytest/assertion/rewrite.py	Tue Aug 30 00:24:57 2011 -0400
+++ b/_pytest/assertion/rewrite.py	Tue Aug 30 10:34:21 2011 -0400
@@ -403,13 +403,6 @@
         self.explanation_specifiers[specifier] = expr
         return "%(" + specifier + ")s"
 
-    def enter_cond(self, cond, body):
-        self.statements.append(ast.If(cond, body, []))
-        self.cond_chain += cond,
-
-    def leave_cond(self, n=1):
-        self.cond_chain = self.cond_chain[:-n]
-
     def push_format_context(self):
         self.explanation_specifiers = {}
         self.stack.append(self.explanation_specifiers)
@@ -484,24 +477,30 @@
         app = ast.Attribute(expl_list, "append", ast.Load())
         is_or = isinstance(boolop.op, ast.Or)
         body = save = self.statements
+        fail_save = self.on_failure
         levels = len(boolop.values) - 1
         self.push_format_context()
         # Process each operand, short-circuting if needed.
         for i, v in enumerate(boolop.values):
+            self.push_format_context()
             res, expl = self.visit(v)
             body.append(ast.Assign([ast.Name(res_var, ast.Store())], res))
-            call = ast.Call(app, [ast.Str(expl)], [], None, None)
-            body.append(ast.Expr(call))
+            if i:
+                fail_inner = []
+                self.on_failure.append(ast.If(cond, fail_inner, []))
+                self.on_failure = fail_inner
+            expl_format = self.pop_format_context(ast.Str(expl))
+            call = ast.Call(app, [expl_format], [], None, None)
+            self.on_failure.append(ast.Expr(call))
             if i < levels:
-                inner = []
                 cond = res
                 if is_or:
                     cond = ast.UnaryOp(ast.Not(), cond)
-                self.enter_cond(cond, inner)
+                inner = []
+                self.statements.append(ast.If(cond, inner, []))
                 self.statements = body = inner
-        # Leave all conditions.
-        self.leave_cond(levels)
         self.statements = save
+        self.on_failure = fail_save
         expl_template = self.helper("format_boolop", expl_list, ast.Num(is_or))
         expl = self.pop_format_context(expl_template)
         return ast.Name(res_var, ast.Load()), self.explanation_param(expl)


--- a/testing/test_assertrewrite.py	Tue Aug 30 00:24:57 2011 -0400
+++ b/testing/test_assertrewrite.py	Tue Aug 30 10:34:21 2011 -0400
@@ -133,6 +133,14 @@
             f = g = False
             assert not f and not g
         getmsg(f, must_pass=True)
+        def x():
+            return False
+        def f():
+            assert x() and x()
+        assert getmsg(f, {"x" : x}) == "assert (x())"
+        def f():
+            assert False or x()
+        assert getmsg(f, {"x" : x}) == "assert (False or x())"
         def f():
             f = True
             g = False

Repository URL: https://bitbucket.org/hpk42/pytest/

--

This is a commit notification from bitbucket.org. You are receiving
this because you have the service enabled, addressing the recipient of
this email.



More information about the pytest-commit mailing list