1 new commit in pytest:

https://bitbucket.org/hpk42/pytest/commits/a79829a4061e/
Changeset:   a79829a4061e
User:        gutworth
Date:        2014-05-31 23:37:02
Summary:     assert reinterpretation: try mangling attributes that look like 
private class vars (fixes #514)
Affected #:  4 files

diff -r eac38fd28362f3f5d81ce2e39bf2f56189153782 -r 
a79829a4061e5ff521cd94925910e8ad56e0112b CHANGELOG
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,6 +1,8 @@
 NEXT (2.6)
 -----------------------------------
 
+- fix issue514: teach assertion reinterpretation about private class attributes
+
 - change -v output to include full node IDs of tests.  Users can copy
   a node ID from a test run, including line number, and use it as a
   positional argument in order to run only a single test.

diff -r eac38fd28362f3f5d81ce2e39bf2f56189153782 -r 
a79829a4061e5ff521cd94925910e8ad56e0112b _pytest/assertion/newinterpret.py
--- a/_pytest/assertion/newinterpret.py
+++ b/_pytest/assertion/newinterpret.py
@@ -286,7 +286,19 @@
         source = "__exprinfo_expr.%s" % (attr.attr,)
         co = self._compile(source)
         try:
-            result = self.frame.eval(co, __exprinfo_expr=source_result)
+            try:
+                result = self.frame.eval(co, __exprinfo_expr=source_result)
+            except AttributeError:
+                # Maybe the attribute name needs to be mangled?
+                if not attr.attr.startswith("__") or attr.attr.endswith("__"):
+                    raise
+                source = "getattr(__exprinfo_expr.__class__, '__name__', '')"
+                co = self._compile(source)
+                class_name = self.frame.eval(co, __exprinfo_expr=source_result)
+                mangled_attr = "_" + class_name +  attr.attr
+                source = "__exprinfo_expr.%s" % (mangled_attr,)
+                co = self._compile(source)
+                result = self.frame.eval(co, __exprinfo_expr=source_result)
         except Exception:
             raise Failure(explanation)
         explanation = "%s\n{%s = %s.%s\n}" % (self.frame.repr(result),

diff -r eac38fd28362f3f5d81ce2e39bf2f56189153782 -r 
a79829a4061e5ff521cd94925910e8ad56e0112b _pytest/assertion/oldinterpret.py
--- a/_pytest/assertion/oldinterpret.py
+++ b/_pytest/assertion/oldinterpret.py
@@ -355,7 +355,18 @@
         expr.eval(frame)
         source = '__exprinfo_expr.%s' % self.attrname
         try:
-            self.result = frame.eval(source, __exprinfo_expr=expr.result)
+            try:
+                self.result = frame.eval(source, __exprinfo_expr=expr.result)
+            except AttributeError:
+                # Maybe the attribute name needs to be mangled?
+                if (not self.attrname.startswith("__") or
+                    self.attrname.endswith("__")):
+                    raise
+                source = "getattr(__exprinfo_expr.__class__, '__name__', '')"
+                class_name = frame.eval(source, __exprinfo_expr=expr.result)
+                mangled_attr = "_" + class_name +  self.attrname
+                source = "__exprinfo_expr.%s" % (mangled_attr,)
+                self.result = frame.eval(source, __exprinfo_expr=expr.result)
         except passthroughex:
             raise
         except:

diff -r eac38fd28362f3f5d81ce2e39bf2f56189153782 -r 
a79829a4061e5ff521cd94925910e8ad56e0112b testing/test_assertinterpret.py
--- a/testing/test_assertinterpret.py
+++ b/testing/test_assertinterpret.py
@@ -131,6 +131,18 @@
         e = exvalue()
         assert "x=5" in e.msg
 
+def test_private_class_variable():
+    class X:
+        def __init__(self):
+            self.__v = 41
+        def m(self):
+            assert self.__v == 42
+    try:
+        X().m()
+    except AssertionError:
+        e = exvalue()
+        assert "== 42" in e.msg
+
 # These tests should both fail, but should fail nicely...
 class WeirdRepr:
     def __repr__(self):

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.
_______________________________________________
pytest-commit mailing list
pytest-commit@python.org
https://mail.python.org/mailman/listinfo/pytest-commit

Reply via email to