Commit 3efacd9a authored by Vincent Pelletier's avatar Vincent Pelletier

Make a monkeypatch out of LP Bug #706946.

Uses slightly modified version of Kazuhiko's patch, as attached on comment #1.
See https://bugs.launchpad.net/zope2/+bug/706946


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@45756 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 3be19db7
......@@ -71,6 +71,7 @@ from Products.ERP5Type.patches import ZODBConnection
# dropped support for older versions.
from Products.ERP5Type.patches import TransactionAddBeforeCommitHook
from Products.ERP5Type.patches import ZopePageTemplate
from Products.ERP5Type.patches import ZopePageTemplateUtils
# These symbols are required for backward compatibility
from Products.ERP5Type.patches.PropertyManager import ERP5PropertyManager
......
# -*- coding: utf-8 -*-
##############################################################################
#
# Copyright (c) 2002 Zope Foundation and Contributors.
# Copyright (c) 2011 Nexedi SA and Contributors. All Rights Reserved.
# Vincent Pelletier <vincent@nexedi.com>
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE
##############################################################################
NEED_PATCH = False
try:
from Products.PageTemplates.utils import convertToUnicode
except ImportError:
# Not present in Zope 2.8
pass
else:
try:
convertToUnicode(u'')
except TypeError:
NEED_PATCH = True
def patched_convertToUnicode(source, content_type, preferred_encodings):
""" Convert 'source' to unicode.
Returns (unicode_str, source_encoding).
"""
# PATCH BEGINING
# See https://bugs.launchpad.net/zope2/+bug/706946
if isinstance(source, unicode):
return source, 'utf-8'
# PATCH END
if content_type.startswith('text/xml'):
encoding = encodingFromXMLPreamble(source)
return unicode(source, encoding), encoding
elif content_type.startswith('text/html'):
encoding = charsetFromMetaEquiv(source)
if encoding:
return unicode(source, encoding), encoding
# Try to detect the encoding by converting it unicode without raising
# exceptions. There are some smarter Python-based sniffer methods
# available however we have to check their licenses first before
# including them into the Zope 2 core
for enc in preferred_encodings:
try:
return unicode(source, enc), enc
except UnicodeDecodeError:
continue
return unicode(source), None
if NEED_PATCH:
# We need to monkey patch in-place, as it is a top-level function and
# already imported in other places.
convertToUnicode.func_code = patched_convertToUnicode.func_code
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment