From 3655ee98cbb524239f3beddac3f67928b058d617 Mon Sep 17 00:00:00 2001 From: Chris McDonough Date: Thu, 13 May 2010 03:19:53 +0000 Subject: [PATCH] - Get rid of circular reference in Invalid exceptions: Invalid exceptions now no longer have a ``parent`` attribute. Instead, they have a ``positional`` attribute, which signifies that the parent node type of the schema node to which they relate inherits from Positional. This attribute isn't an API; it's used only internally for reporting. --- CHANGES.txt | 7 +++++++ colander/__init__.py | 10 ++++------ colander/tests.py | 19 +++++++++++++------ docs/api.rst | 4 ---- 4 files changed, 24 insertions(+), 16 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index ed31877..83c2338 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -7,6 +7,13 @@ Next release - Allow ``colander.Regex`` validator to accept a pattern object instead of just a string. +- Get rid of circular reference in Invalid exceptions: Invalid + exceptions now no longer have a ``parent`` attribute. Instead, they + have a ``positional`` attribute, which signifies that the parent + node type of the schema node to which they relate inherits from + Positional. This attribute isn't an API; it's used only internally + for reporting. + 0.6.2 (2010-05-08) ------------------ diff --git a/colander/__init__.py b/colander/__init__.py index 3ad908c..28fbcdf 100644 --- a/colander/__init__.py +++ b/colander/__init__.py @@ -35,7 +35,7 @@ class Invalid(Exception): keyword, indicating the value related to the error. """ pos = None - parent = None + positional = False def __init__(self, node, msg=None, value=None): Exception.__init__(self, node, msg) @@ -57,11 +57,9 @@ class Invalid(Exception): If ``pos`` is provided, it will be assigned to the ``pos`` attribute of the provided ``exc`` object. - - The ``parent`` attribute of the provided ``exc`` will be set - as a reference to ``self``. """ - exc.parent = self + if self.node and isinstance(self.node.typ, Positional): + exc.positional = True if pos is not None: exc.pos = pos self.children.append(exc) @@ -112,7 +110,7 @@ class Invalid(Exception): return traverse(self, []) def _keyname(self): - if self.parent and isinstance(self.parent.node.typ, Positional): + if self.positional: return str(self.pos) return str(self.node.name) diff --git a/colander/tests.py b/colander/tests.py index 0e84141..a9151e4 100644 --- a/colander/tests.py +++ b/colander/tests.py @@ -26,7 +26,17 @@ class TestInvalid(unittest.TestCase): exc = self._makeOne(None, 'msg') other = Dummy() exc.add(other) - self.assertEqual(other.parent, exc) + self.failIf(hasattr(other, 'positional')) + self.assertEqual(exc.children, [other]) + + def test_add_positional(self): + from colander import Positional + p = Positional() + node = DummySchemaNode(p) + exc = self._makeOne(node, 'msg') + other = Dummy() + exc.add(other) + self.assertEqual(other.positional, True) self.assertEqual(exc.children, [other]) def test__keyname_no_parent(self): @@ -35,12 +45,9 @@ class TestInvalid(unittest.TestCase): exc.node = node self.assertEqual(exc._keyname(), 'name') - def test__keyname_positional_parent(self): - from colander import Positional - parent = Dummy() - parent.node = DummySchemaNode(Positional()) + def test__keyname_positional(self): exc = self._makeOne(None, '') - exc.parent = parent + exc.positional = True exc.pos = 2 self.assertEqual(exc._keyname(), '2') diff --git a/docs/api.rst b/docs/api.rst index 2fcc6b9..e0b8e3a 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -9,10 +9,6 @@ Exceptions .. autoclass:: Invalid :members: - .. attribute:: parent - - A reference to the parent exception. - .. attribute:: pos An integer representing the position of this exception's