From e015c4637b1eaa31bed5077c4eb751d68017a979 Mon Sep 17 00:00:00 2001 From: Reverb Chu Date: Fri, 9 Jun 2017 11:54:04 -0700 Subject: [PATCH] adding trailing underscore for python keywords --- tests/tests.py | 8 ++++++++ untangle.py | 6 ++++++ 2 files changed, 14 insertions(+) diff --git a/tests/tests.py b/tests/tests.py index 76a1ef4..33558e3 100755 --- a/tests/tests.py +++ b/tests/tests.py @@ -93,6 +93,14 @@ def test_attribute_protocol(self): self.assertEqual('child1', getattr(o.root, 'child')[0]['name']) + def test_python_keyword(self): + o = untangle.parse("") + self.assert_(o is not None) + self.assert_(o.class_ is not None) + self.assert_(o.class_.return_ is not None) + self.assert_(o.class_.pass_ is not None) + self.assert_(o.class_.None_ is not None) + class InvalidTestCase(unittest.TestCase): """ Test corner cases """ diff --git a/untangle.py b/untangle.py index 1263056..6d1e966 100755 --- a/untangle.py +++ b/untangle.py @@ -14,6 +14,7 @@ License: MIT License - http://www.opensource.org/licenses/mit-license.php """ import os +import keyword from xml.sax import make_parser, handler try: from StringIO import StringIO @@ -133,6 +134,11 @@ def startElement(self, name, attributes): name = name.replace('-', '_') name = name.replace('.', '_') name = name.replace(':', '_') + + # adding trailing _ for keywords + if keyword.iskeyword(name): + name += '_' + attrs = dict() for k, v in attributes.items(): attrs[k] = v