Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix XML Page template files in Python 3 #320

Merged
merged 2 commits into from
Oct 2, 2018
Merged

Conversation

ale-rt
Copy link
Member

@ale-rt ale-rt commented Sep 27, 2018

Fixes #319

@ale-rt
Copy link
Member Author

ale-rt commented Sep 27, 2018

Without the patch the ptest fail like:

Error in test testXMLPageTemplateFile (Products.PageTemplates.tests.testZopePageTemplate.ZopePageTemplateFileTests)
Traceback (most recent call last):
  File "/usr/lib/python3.6/unittest/case.py", line 59, in testPartExecutor
    yield
  File "/usr/lib/python3.6/unittest/case.py", line 605, in run
    testMethod()
  File "/home/ale/Code/python/Zope/src/Products/PageTemplates/tests/testZopePageTemplate.py", line 234, in testXMLPageTemplateFile
    PageTemplateFile(filename).pt_render(),
  File "/home/ale/Code/python/Zope/src/Products/PageTemplates/PageTemplate.py", line 83, in pt_render
    showtal=showtal)
  File "/home/ale/.buildout/eggs/zope.pagetemplate-4.3.0-py3.6.egg/zope/pagetemplate/pagetemplate.py", line 128, in pt_render
    raise PTRuntimeError(str(self._v_errors))
   - Warning: Compilation failed
   - Warning: builtins.TypeError: cannot use a string pattern on a bytes-like object
zope.pagetemplate.pagetemplate.PTRuntimeError: ['Compilation failed', 'builtins.TypeError: cannot use a string pattern on a bytes-like object']

The compilation fails when reaching this line:

text, count = re_match_pi.subn(sanitize, text)

@icemac
Copy link
Member

icemac commented Sep 28, 2018

Does this PR aim to fix #319?

Copy link
Member

@davisagli davisagli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This mostly looks great but I have one concern.

try:
text = text.decode(encodingFromXMLPreamble(text))
except UnicodeDecodeError:
pass
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it would be better to let the exception happen rather than swallowing it and returning bytes which will probably just fail later?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I remove the try/except the tests from test_sniffer_xml_utf16_be and test_sniffer_xml_utf16_le will fail because the regexp in encodingFromXMLPreamble method will fail to match the encoding.

Copy link
Member

@icemac icemac left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I share the concern of @davisagli

try:
text = text.decode(encodingFromXMLPreamble(text))
except UnicodeDecodeError:
pass
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

@ale-rt
Copy link
Member Author

ale-rt commented Oct 1, 2018

I removed the try/except but I had to add an encoding parameter to the PageTemplateFile constructor (as we have on the Python3 open function).
The reason is that otherwise there is no proper reliable way to detect the encoding a part from checking the first bytes of the file (which might also be an option).

@icemac icemac added the bug label Oct 2, 2018
@ale-rt ale-rt merged commit e64d75c into master Oct 2, 2018
@ale-rt ale-rt deleted the fix-xml-page-template branch October 2, 2018 07:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants