- 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.
This commit is contained in:
Chris McDonough
2010-05-13 03:19:53 +00:00
parent 12abbf7f8a
commit 3655ee98cb
4 changed files with 24 additions and 16 deletions

View File

@@ -7,6 +7,13 @@ Next release
- Allow ``colander.Regex`` validator to accept a pattern object - Allow ``colander.Regex`` validator to accept a pattern object
instead of just a string. 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) 0.6.2 (2010-05-08)
------------------ ------------------

View File

@@ -35,7 +35,7 @@ class Invalid(Exception):
keyword, indicating the value related to the error. keyword, indicating the value related to the error.
""" """
pos = None pos = None
parent = None positional = False
def __init__(self, node, msg=None, value=None): def __init__(self, node, msg=None, value=None):
Exception.__init__(self, node, msg) Exception.__init__(self, node, msg)
@@ -57,11 +57,9 @@ class Invalid(Exception):
If ``pos`` is provided, it will be assigned to the ``pos`` If ``pos`` is provided, it will be assigned to the ``pos``
attribute of the provided ``exc`` object. 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: if pos is not None:
exc.pos = pos exc.pos = pos
self.children.append(exc) self.children.append(exc)
@@ -112,7 +110,7 @@ class Invalid(Exception):
return traverse(self, []) return traverse(self, [])
def _keyname(self): def _keyname(self):
if self.parent and isinstance(self.parent.node.typ, Positional): if self.positional:
return str(self.pos) return str(self.pos)
return str(self.node.name) return str(self.node.name)

View File

@@ -26,7 +26,17 @@ class TestInvalid(unittest.TestCase):
exc = self._makeOne(None, 'msg') exc = self._makeOne(None, 'msg')
other = Dummy() other = Dummy()
exc.add(other) 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]) self.assertEqual(exc.children, [other])
def test__keyname_no_parent(self): def test__keyname_no_parent(self):
@@ -35,12 +45,9 @@ class TestInvalid(unittest.TestCase):
exc.node = node exc.node = node
self.assertEqual(exc._keyname(), 'name') self.assertEqual(exc._keyname(), 'name')
def test__keyname_positional_parent(self): def test__keyname_positional(self):
from colander import Positional
parent = Dummy()
parent.node = DummySchemaNode(Positional())
exc = self._makeOne(None, '') exc = self._makeOne(None, '')
exc.parent = parent exc.positional = True
exc.pos = 2 exc.pos = 2
self.assertEqual(exc._keyname(), '2') self.assertEqual(exc._keyname(), '2')

View File

@@ -9,10 +9,6 @@ Exceptions
.. autoclass:: Invalid .. autoclass:: Invalid
:members: :members:
.. attribute:: parent
A reference to the parent exception.
.. attribute:: pos .. attribute:: pos
An integer representing the position of this exception's An integer representing the position of this exception's