Skip to content

Commit

Permalink
Support Element truthiness on Python 3
Browse files Browse the repository at this point in the history
Python 3 changed `__nonzero__` for `__bool__`
(https://portingguide.readthedocs.io/en/latest/core-obj-misc.html#customizing-truthiness-bool)
-- this makes the standard class implementation Python 3 and
provides an alias for Python 2 compatibility.
  • Loading branch information
davidjb committed Dec 19, 2019
1 parent cd441fd commit 8e23f0d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Unreleased
- dropped support for Python 2.6, 3.3
- fixed support for Python 3.6 ([#57](https://github.com/stchris/untangle/pull/57))
- formatted code with black
- support Element truthiness on Python 3

1.1.1
- addded generic SAX feature toggle ([#26](https://github.com/stchris/untangle/pull/26))
Expand Down
7 changes: 7 additions & 0 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ def test_basic_with_decl(self):
self.assertTrue("c" in o.a)
self.assertTrue("d" not in o.a)

def test_truthiness(self):
o = untangle.parse("<a><b/><c/></a>")
self.assertTrue(o)
self.assertTrue(o.a)
self.assertTrue(o.a.b)
self.assertTrue(o.a.c)

def test_with_attributes(self):
o = untangle.parse(
"""
Expand Down
4 changes: 3 additions & 1 deletion untangle.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,11 @@ def __repr__(self):
self.cdata,
)

def __nonzero__(self):
def __bool__(self):
return self.is_root or self._name is not None

__nonzero__ = __bool__

def __eq__(self, val):
return self.cdata == val

Expand Down

0 comments on commit 8e23f0d

Please sign in to comment.