From 8e23f0d1fe2656c7361a5c2ba3eaca42ed59e576 Mon Sep 17 00:00:00 2001 From: David Beitey Date: Thu, 19 Dec 2019 00:43:55 +0000 Subject: [PATCH] Support Element truthiness on Python 3 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. --- CHANGELOG.md | 1 + tests/tests.py | 7 +++++++ untangle.py | 4 +++- 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2a8e0ef..57a9da7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)) diff --git a/tests/tests.py b/tests/tests.py index 83be504..767f9af 100755 --- a/tests/tests.py +++ b/tests/tests.py @@ -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("") + 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( """ diff --git a/untangle.py b/untangle.py index 7986c24..ffe2082 100755 --- a/untangle.py +++ b/untangle.py @@ -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