Skip to content

Commit

Permalink
Merge pull request #78 from zopefoundation/jens_html_escape
Browse files Browse the repository at this point in the history
Fix deprecation warnings for ``cgi.escape`` (fixes #76)
  • Loading branch information
dataflake committed Dec 3, 2018
2 parents fe1292f + 53ce1f7 commit 056dec8
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 14 deletions.
3 changes: 2 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ Changelog
2.0b5 (unreleased)
------------------

- Nothing changed yet.
- Fix deprecation warnings for ``cgi.escape`` by using ``html.escape``
(`#76 <https://github.com/zopefoundation/Products.GenericSetup/issues/76>`_)


2.0b4 (2018-11-22)
Expand Down
60 changes: 49 additions & 11 deletions Products/GenericSetup/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
""" GenericSetup.utils unit tests
"""

try:
from html import escape # noqa
HTML_ESCAPE = True
except ImportError:
HTML_ESCAPE = False
import six
import unittest

Expand Down Expand Up @@ -891,34 +896,67 @@ class PrettyDocumentTests(unittest.TestCase):
def test_attr_quoting(self):
from Products.GenericSetup.utils import PrettyDocument
original = 'baz &nbsp;<bar>&"\''
expected = (b'<?xml version="1.0" encoding="utf-8"?>\n'
b'<doc bar="" foo="baz '
b'&amp;nbsp;&lt;bar&gt;&amp;&quot;\'"/>\n')
html_escaped = (b'<?xml version="1.0" encoding="utf-8"?>\n'
b'<doc bar="" foo="baz '
b'&amp;nbsp;&lt;bar&gt;&amp;&quot;&#x27;"/>\n')
cgi_escaped = (b'<?xml version="1.0" encoding="utf-8"?>\n'
b'<doc bar="" foo="baz '
b'&amp;nbsp;&lt;bar&gt;&amp;&quot;\'"/>\n')

doc = PrettyDocument()
node = doc.createElement('doc')
node.setAttribute('foo', original)
node.setAttribute('bar', None)
doc.appendChild(node)
self.assertEqual(doc.toprettyxml(' '), expected)
# Reparse
e = _getDocumentElement(expected)

# Output depends on the presence of html.escape
xml_output = doc.toprettyxml(' ')
if HTML_ESCAPE:
self.assertEqual(xml_output, html_escaped)
else:
self.assertEqual(xml_output, cgi_escaped)

# Reparse from cgi.escape representation (Python 2 only)
# should always work
e = _getDocumentElement(cgi_escaped)
self.assertEqual(e.getAttribute('foo'), original)

# Reparse from html.escape representation (Python 3 only)
# should always work, even without html.escape
e = _getDocumentElement(html_escaped)
self.assertEqual(e.getAttribute('foo'), original)

def test_text_quoting(self):
from Products.GenericSetup.utils import PrettyDocument
original = 'goo &nbsp;<hmm>&"\''
expected = (b'<?xml version="1.0" encoding="utf-8"?>\n'
b'<doc>goo &amp;nbsp;&lt;hmm&gt;&amp;"\'</doc>\n')
html_escaped = (b'<?xml version="1.0" encoding="utf-8"?>\n'
b'<doc>goo &amp;nbsp;&lt;hmm&gt;'
b'&amp;&quot;&#x27;</doc>\n')
cgi_escaped = (b'<?xml version="1.0" encoding="utf-8"?>\n'
b'<doc>goo &amp;nbsp;&lt;hmm&gt;'
b'&amp;"\'</doc>\n')

doc = PrettyDocument()
node = doc.createElement('doc')
child = doc.createTextNode(original)
node.appendChild(child)
doc.appendChild(node)
self.assertEqual(doc.toprettyxml(' '), expected)
# Reparse
e = _getDocumentElement(expected)

# Output depends on the presence of html.escape
xml_output = doc.toprettyxml(' ')
if HTML_ESCAPE:
self.assertEqual(xml_output, html_escaped)
else:
self.assertEqual(xml_output, cgi_escaped)

# Reparse from cgi.escape representation (Python 2 only)
# should always work
e = _getDocumentElement(cgi_escaped)
self.assertEqual(e.childNodes[0].nodeValue, original)

# Reparse from html.escape representation (Python 3 only)
# should always work, even without html.escape
e = _getDocumentElement(html_escaped)
self.assertEqual(e.childNodes[0].nodeValue, original)


Expand Down
2 changes: 1 addition & 1 deletion Products/GenericSetup/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

try:
from html import escape
except ImportError:
except ImportError: # BBB Python 2
from cgi import escape
import logging
import os
Expand Down
5 changes: 4 additions & 1 deletion Products/GenericSetup/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
import os
import six
import sys
from cgi import escape
try:
from html import escape
except ImportError: # BBB Python 2
from cgi import escape
from inspect import getdoc
from logging import getLogger
from xml.dom.minidom import _nssplit
Expand Down

0 comments on commit 056dec8

Please sign in to comment.