From b4588f1798fc1a6e1230b5fe962986dc2e3e2f74 Mon Sep 17 00:00:00 2001 From: Benjamin Peterson Date: Thu, 3 May 2012 13:49:30 -0400 Subject: [PATCH] escape the % operator in string formatting --- CHANGELOG | 1 + _pytest/assertion/rewrite.py | 2 +- testing/test_assertrewrite.py | 4 ++++ 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index 82e218dba..8f1ae8bab 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,7 @@ Changese between 2.2.3 and ... ----------------------------------- +- fix error message for rewritten assertions involving the % operator - fix issue 126: correctly match all invalid xml characters for junitxml binary escape - fix issue with unittest: now @unittest.expectedFailure markers should diff --git a/_pytest/assertion/rewrite.py b/_pytest/assertion/rewrite.py index 97eeb6623..dc0317b85 100644 --- a/_pytest/assertion/rewrite.py +++ b/_pytest/assertion/rewrite.py @@ -297,7 +297,7 @@ binop_map = { ast.Mult : "*", ast.Div : "/", ast.FloorDiv : "//", - ast.Mod : "%", + ast.Mod : "%%", # escaped for string formatting ast.Eq : "==", ast.NotEq : "!=", ast.Lt : "<", diff --git a/testing/test_assertrewrite.py b/testing/test_assertrewrite.py index c07f10dae..648ab2100 100644 --- a/testing/test_assertrewrite.py +++ b/testing/test_assertrewrite.py @@ -195,6 +195,10 @@ class TestAssertionRewrite: y = -1 assert x + y assert getmsg(f) == "assert (1 + -1)" + def f(): + x = range(10) + assert not 5 % 4 + assert getmsg(f) == "assert not (5 % 4)" def test_call(self): def g(a=42, *args, **kwargs):