Skip to content

Commit

Permalink
Pass explicit encoding (of "utf-8") when generating XML.
Browse files Browse the repository at this point in the history
This way we don't need to encode strings for every node, which
breaks with Python 3.
  • Loading branch information
witsch committed Oct 21, 2017
1 parent 2bfac16 commit eb2c157
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Products/GenericSetup/OFSP/tests/test_exportimport.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from Products.GenericSetup.testing import ExportImportZCMLLayer

_FOLDER_BODY = """\
<?xml version="1.0"?>
<?xml version="1.0" encoding="utf-8"?>
<object name="foo_folder" meta_type="Folder">
<property name="title">Foo</property>
</object>
Expand Down
4 changes: 2 additions & 2 deletions Products/GenericSetup/ZCatalog/tests/test_exportimport.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class _extra:


_CATALOG_BODY = """\
<?xml version="1.0"?>
<?xml version="1.0" encoding="utf-8"?>
<object name="foo_catalog" meta_type="ZCatalog">
<property name="title"></property>
<object name="foo_plexicon" meta_type="ZCTextIndex Lexicon">
Expand Down Expand Up @@ -67,7 +67,7 @@ class _extra:
"""

_CATALOG_UPDATE_BODY = """\
<?xml version="1.0"?>
<?xml version="1.0" encoding="utf-8"?>
<object name="foo_catalog">
<object name="old_plexicon" remove="True"/>
<index name="foo_text" remove="True"/>
Expand Down
4 changes: 2 additions & 2 deletions Products/GenericSetup/tests/test_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def getExcludedInterfaces(self):


_COMPONENTS_BODY = """\
<?xml version="1.0"?>
<?xml version="1.0" encoding="utf-8"?>
<componentregistry>
<adapters>
<adapter factory="Products.GenericSetup.tests.test_components.DummyAdapter"
Expand Down Expand Up @@ -203,7 +203,7 @@ def getExcludedInterfaces(self):
"""

_REMOVE_IMPORT = """\
<?xml version="1.0"?>
<?xml version="1.0" encoding="utf-8"?>
<componentregistry>
<adapters>
<adapter factory="Products.GenericSetup.tests.test_components.DummyAdapter"
Expand Down
28 changes: 14 additions & 14 deletions Products/GenericSetup/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
)

_EMPTY_PROPERTY_EXPORT = """\
<?xml version="1.0"?>
<?xml version="1.0" encoding="utf-8"?>
<dummy>
<property name="foo_boolean" type="boolean">False</property>
<property name="foo_date" type="date">1970/01/01 00:00:00 UTC</property>
Expand All @@ -70,7 +70,7 @@
"""

_NORMAL_PROPERTY_EXPORT = u"""\
<?xml version="1.0"?>
<?xml version="1.0" encoding="utf-8"?>
<dummy>
<property name="foo_boolean" type="boolean">True</property>
<property name="foo_date" type="date">2000/01/01 00:00:00 UTC</property>
Expand Down Expand Up @@ -106,7 +106,7 @@
""".encode('utf-8')

_FIXED_PROPERTY_EXPORT = u"""\
<?xml version="1.0"?>
<?xml version="1.0" encoding="utf-8"?>
<dummy>
<property name="foo_boolean">True</property>
<property name="foo_date">2000/01/01 00:00:00 UTC</property>
Expand Down Expand Up @@ -140,23 +140,23 @@
""".encode('utf-8')

_SPECIAL_IMPORT = """\
<?xml version="1.0"?>
<?xml version="1.0" encoding="utf-8"?>
<dummy>
<!-- ignore comment, import 0 as False -->
<property name="foo_boolean0" type="boolean">0</property>
</dummy>
"""

_I18N_IMPORT = """\
<?xml version="1.0"?>
<?xml version="1.0" encoding="utf-8"?>
<dummy xmlns:i18n="http://xml.zope.org/namespaces/i18n"
i18n:domain="dummy_domain">
<property name="foo_string" i18n:translate="">Foo String</property>
</dummy>
"""

_NOPURGE_IMPORT = """\
<?xml version="1.0"?>
<?xml version="1.0" encoding="utf-8"?>
<dummy>
<property name="lines1">
<element value="Foo"/>
Expand All @@ -173,7 +173,7 @@
</dummy>
"""
_REMOVE_ELEMENT_IMPORT = """\
<?xml version="1.0"?>
<?xml version="1.0" encoding="utf-8"?>
<dummy>
<property name="lines1" purge="False">
<element value="Foo" remove="True" />
Expand All @@ -186,37 +186,37 @@
"""

_NORMAL_MARKER_EXPORT = """\
<?xml version="1.0"?>
<?xml version="1.0" encoding="utf-8"?>
<dummy>
<marker name="Products.GenericSetup.testing.IDummyMarker"/>
</dummy>
"""

_ADD_IMPORT = """\
<?xml version="1.0"?>
<?xml version="1.0" encoding="utf-8"?>
<dummy>
<object name="history" meta_type="Generic Setup Tool"/>
<object name="future" meta_type="Generic Setup Tool"/>
</dummy>
"""
_REMOVE_IMPORT = """\
<?xml version="1.0"?>
<?xml version="1.0" encoding="utf-8"?>
<dummy>
<object name="history" remove="True"/>
<object name="future" remove="False"/>
</dummy>
"""

_ADD_PROPERTY_IMPORT = """\
<?xml version="1.0"?>
<?xml version="1.0" encoding="utf-8"?>
<dummy>
<property name="line1" type="string">Line 1</property>
<property name="line2" type="string">Line 2</property>
</dummy>
"""

_REMOVE_PROPERTY_IMPORT = """\
<?xml version="1.0"?>
<?xml version="1.0" encoding="utf-8"?>
<dummy>
<property name="line1" remove="True"/>
<property name="line2" type="string" remove="False"/>
Expand Down Expand Up @@ -730,7 +730,7 @@ class PrettyDocumentTests(unittest.TestCase):
def test_attr_quoting(self):
from Products.GenericSetup.utils import PrettyDocument
original = 'baz &nbsp;<bar>&"\''
expected = ('<?xml version="1.0"?>\n'
expected = ('<?xml version="1.0" encoding="utf-8"?>\n'
'<doc bar="" foo="baz '
'&amp;nbsp;&lt;bar&gt;&amp;&quot;\'"/>\n')

Expand All @@ -747,7 +747,7 @@ def test_attr_quoting(self):
def test_text_quoting(self):
from Products.GenericSetup.utils import PrettyDocument
original = 'goo &nbsp;<hmm>&"\''
expected = ('<?xml version="1.0"?>\n'
expected = ('<?xml version="1.0" encoding="utf-8"?>\n'
'<doc>goo &amp;nbsp;&lt;hmm&gt;&amp;"\'</doc>\n')

doc = PrettyDocument()
Expand Down
8 changes: 6 additions & 2 deletions Products/GenericSetup/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,15 +361,15 @@ def writexml(self, writer, indent="", addindent="", newl=""):
if a_value is None:
a_value = ""
else:
a_value = escape(a_value.encode('utf-8'), quote=True)
a_value = escape(a_value, quote=True)

wrapper.queue(' %s="%s"' % (a_name, a_value))

if self.childNodes:
wrapper.queue('>')
for node in self.childNodes:
if node.nodeType == Node.TEXT_NODE:
data = escape(node.data.encode('utf-8'))
data = escape(node.data)
textlines = data.splitlines()
if textlines:
wrapper.queue(textlines.pop(0))
Expand Down Expand Up @@ -410,6 +410,10 @@ def writexml(self, writer, indent="", addindent="", newl="",
for node in self.childNodes:
node.writexml(writer, indent, addindent, newl)

def toprettyxml(self, indent='\t', newl='\n', encoding='utf-8'):
# `super` does not work here in python 2.7, yuck!
return Document.toprettyxml(self, indent, newl, encoding)


@implementer(INode)
class NodeAdapterBase(object):
Expand Down

0 comments on commit eb2c157

Please sign in to comment.