Skip to content

Commit

Permalink
adding trailing underscore for python keywords
Browse files Browse the repository at this point in the history
  • Loading branch information
reverbc committed Jun 9, 2017
1 parent 2119763 commit e015c46
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
8 changes: 8 additions & 0 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("<class><return/><pass/><None/></class>")
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 """
Expand Down
6 changes: 6 additions & 0 deletions untangle.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e015c46

Please sign in to comment.