#1 — Unicode error
| State | Tested and confirmed closed |
|---|---|
| Release: | 1.0.0 |
| Area | Bugs |
| Issue type | Bug |
| Severity | Medium |
| Submitted by | (anonymous) |
| Submitted on | 2007-03-07 |
| Responsible | Lukasz Lakomy |
| Target release: | 1.0.0 |
Last modified on
2007-03-09
by
Lukasz Lakomy
Hi, I§v been trying ZPTDebugger in my zope/plone environment and I have got Unicode error. This error were caused by str(value) conversion.
There is patch adding safe_unicode method from CMFPlone/utils.py extended with recursion call (due to lists and dicts) and is using safe_unicode in few places in DebuggerConsole.py
diff -u ZPTDebugger-old/DebuggerConsole.py ZPTDebugger/DebuggerConsole.py
--- ZPTDebugger-old/DebuggerConsole.py 2007-03-02 12:55:23.000000000 +0100
+++ ZPTDebugger/DebuggerConsole.py 2007-03-07 17:59:14.000000000 +0100
@@ -18,7 +18,7 @@
from ZPublisher.BaseRequest import BaseRequest
from DebuggerContainer import debugger_container
-from Utils import getObjectById
+from Utils import getObjectById, safe_unicode
from Constants import META_TYPE
import DebuggerPatch
from BeautifulSoup import BeautifulSoup
@@ -130,7 +130,7 @@
@return : Oryginal or truncated value
@rtype : String
"""
- value = str(value)
+ value = safe_unicode(value)
if len(value) > self.value_length:
value = value[:self.value_length]
return value
@@ -154,7 +154,7 @@
@return : Pretty HTML
@rtype : String
"""
- result = str(value)
+ result = safe_unicode(value)
soup = BeautifulSoup(result)
result = soup.prettify()
return result
@@ -197,7 +197,7 @@
"""
link = ""
value = entry['value']
- value = str(value)
+ value = safe_unicode(value)
if len(value) > self.value_length:
link = "<a class='more' href='manage_showDetails?value_id=%s'>%s</a>"%(entry['id'],self.more_link_body)
return link
@@ -429,4 +429,4 @@
except:
return None
-InitializeClass(DebuggerConsole)
\ No newline at end of file
+InitializeClass(DebuggerConsole)
diff -u ZPTDebugger-old/Utils.py ZPTDebugger/Utils.py
--- ZPTDebugger-old/Utils.py 2007-03-02 12:55:23.000000000 +0100
+++ ZPTDebugger/Utils.py 2007-03-07 17:17:26.000000000 +0100
@@ -32,4 +32,49 @@
icon.__roles__=None
if not hasattr(misc_images, pid):
setattr(misc_images, pid, MiscImage(pid, {}))
- getattr(misc_images, pid)[name]=icon
\ No newline at end of file
+ getattr(misc_images, pid)[name]=icon
+
+def safe_unicode(value, encoding='utf-8'):
+ """Converts a value to unicode, even it is already a unicode string.
+ This method is copied from CMFPlone.utils.py but returns always unicode
+
+ >>> from Products.CMFPlone.utils import safe_unicode
+
+ >>> safe_unicode('spam')
+ u'spam'
+ >>> safe_unicode(u'spam')
+ u'spam'
+ >>> safe_unicode(u'spam'.encode('utf-8'))
+ u'spam'
+ >>> safe_unicode('\xc6\xb5')
+ u'\u01b5'
+ >>> safe_unicode(u'\xc6\xb5'.encode('iso-8859-1'))
+ u'\u01b5'
+ >>> safe_unicode('\xc6\xb5', encoding='ascii')
+ u'\u01b5'
+ >>> safe_unicode(1)
+ u'1'
+ >>> print safe_unicode(None)
+ u'None'
+ """
+ if isinstance(value, unicode):
+ return value
+ elif isinstance(value, basestring):
+ try:
+ value = unicode(value, encoding)
+ except (UnicodeDecodeError):
+ value = value.decode('utf-8', 'replace')
+ elif isinstance(value, (list, tuple)):
+ res = []
+ for item in value:
+ res.append(safe_unicode(item))
+ return res
+ elif isinstance(value, dict):
+ res = {}
+ for k, v in value.items():
+ res[safe_unicode(k)] = safe_unicode(v)
+ return res
+
+ return unicode(value)
+
+
There is patch adding safe_unicode method from CMFPlone/utils.py extended with recursion call (due to lists and dicts) and is using safe_unicode in few places in DebuggerConsole.py
diff -u ZPTDebugger-old/DebuggerConsole.py ZPTDebugger/DebuggerConsole.py
--- ZPTDebugger-old/DebuggerConsole.py 2007-03-02 12:55:23.000000000 +0100
+++ ZPTDebugger/DebuggerConsole.py 2007-03-07 17:59:14.000000000 +0100
@@ -18,7 +18,7 @@
from ZPublisher.BaseRequest import BaseRequest
from DebuggerContainer import debugger_container
-from Utils import getObjectById
+from Utils import getObjectById, safe_unicode
from Constants import META_TYPE
import DebuggerPatch
from BeautifulSoup import BeautifulSoup
@@ -130,7 +130,7 @@
@return : Oryginal or truncated value
@rtype : String
"""
- value = str(value)
+ value = safe_unicode(value)
if len(value) > self.value_length:
value = value[:self.value_length]
return value
@@ -154,7 +154,7 @@
@return : Pretty HTML
@rtype : String
"""
- result = str(value)
+ result = safe_unicode(value)
soup = BeautifulSoup(result)
result = soup.prettify()
return result
@@ -197,7 +197,7 @@
"""
link = ""
value = entry['value']
- value = str(value)
+ value = safe_unicode(value)
if len(value) > self.value_length:
link = "<a class='more' href='manage_showDetails?value_id=%s'>%s</a>"%(entry['id'],self.more_link_body)
return link
@@ -429,4 +429,4 @@
except:
return None
-InitializeClass(DebuggerConsole)
\ No newline at end of file
+InitializeClass(DebuggerConsole)
diff -u ZPTDebugger-old/Utils.py ZPTDebugger/Utils.py
--- ZPTDebugger-old/Utils.py 2007-03-02 12:55:23.000000000 +0100
+++ ZPTDebugger/Utils.py 2007-03-07 17:17:26.000000000 +0100
@@ -32,4 +32,49 @@
icon.__roles__=None
if not hasattr(misc_images, pid):
setattr(misc_images, pid, MiscImage(pid, {}))
- getattr(misc_images, pid)[name]=icon
\ No newline at end of file
+ getattr(misc_images, pid)[name]=icon
+
+def safe_unicode(value, encoding='utf-8'):
+ """Converts a value to unicode, even it is already a unicode string.
+ This method is copied from CMFPlone.utils.py but returns always unicode
+
+ >>> from Products.CMFPlone.utils import safe_unicode
+
+ >>> safe_unicode('spam')
+ u'spam'
+ >>> safe_unicode(u'spam')
+ u'spam'
+ >>> safe_unicode(u'spam'.encode('utf-8'))
+ u'spam'
+ >>> safe_unicode('\xc6\xb5')
+ u'\u01b5'
+ >>> safe_unicode(u'\xc6\xb5'.encode('iso-8859-1'))
+ u'\u01b5'
+ >>> safe_unicode('\xc6\xb5', encoding='ascii')
+ u'\u01b5'
+ >>> safe_unicode(1)
+ u'1'
+ >>> print safe_unicode(None)
+ u'None'
+ """
+ if isinstance(value, unicode):
+ return value
+ elif isinstance(value, basestring):
+ try:
+ value = unicode(value, encoding)
+ except (UnicodeDecodeError):
+ value = value.decode('utf-8', 'replace')
+ elif isinstance(value, (list, tuple)):
+ res = []
+ for item in value:
+ res.append(safe_unicode(item))
+ return res
+ elif isinstance(value, dict):
+ res = {}
+ for k, v in value.items():
+ res[safe_unicode(k)] = safe_unicode(v)
+ return res
+
+ return unicode(value)
+
+
Added by
Lukasz Lakomy
on
2007-03-09 22:13
Target release:
None → 1.0.0
Responsible manager:
(UNASSIGNED) → lukasz
Fixed. Chsnges will be visible in the next release.
Added by
Lukasz Lakomy
on
2007-03-09 22:17
Issue state:
resolved → closed
In the meantime replace files with those from patch
ZPTDebuggerPatch1.zip
(