[PATCH 1/2] add to new function to encode the unicode passed to cherrpy.

From: ShaoHe Feng <shaohef@linux.vnet.ibm.com> Then others can make use of it. The cherrpy low version, cherrpy HTTPError accept unicode. But the high level version, cherrpy refuse to accept unicode. Signed-off-by: ShaoHe Feng <shaohef@linux.vnet.ibm.com> Signed-off-by: Zhou Zheng Sheng <zhshzhou@linux.vnet.ibm.com> --- src/kimchi/root.py | 11 +++-------- src/kimchi/utils.py | 8 ++++++++ 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/kimchi/root.py b/src/kimchi/root.py index 9bae34a..aabae68 100644 --- a/src/kimchi/root.py +++ b/src/kimchi/root.py @@ -31,6 +31,7 @@ from kimchi.control import sub_nodes from kimchi.control.base import Resource from kimchi.control.utils import parse_request from kimchi.exception import MissingParameter, OperationFailed +from kimchi.utils import safe_encode class Root(Resource): @@ -50,19 +51,13 @@ class Root(Resource): def error_production_handler(self, status, message, traceback, version): data = {'code': status, 'reason': message} res = template.render('error.html', data) - if (type(res) is unicode and - LooseVersion(cherrypy.__version__) < LooseVersion('3.2.5')): - res = res.encode("utf-8") - return res + return safe_encode(res) def error_development_handler(self, status, message, traceback, version): data = {'code': status, 'reason': message, 'call_stack': cherrypy._cperror.format_exc()} res = template.render('error.html', data) - if (type(res) is unicode and - LooseVersion(cherrypy.__version__) < LooseVersion('3.2.5')): - res = res.encode("utf-8") - return res + return safe_encode(res) def get(self): return self.default(self.default_page) diff --git a/src/kimchi/utils.py b/src/kimchi/utils.py index 6c29e0e..bfad775 100644 --- a/src/kimchi/utils.py +++ b/src/kimchi/utils.py @@ -31,6 +31,7 @@ from multiprocessing import Process, Queue from threading import Timer from cherrypy.lib.reprconf import Parser +from distutils.version import LooseVersion from kimchi.asynctask import AsyncTask from kimchi.config import paths, PluginPaths @@ -41,6 +42,13 @@ kimchi_log = cherrypy.log.error_log task_id = 0 +def safe_encode(string): + if (type(string) is unicode and + LooseVersion(cherrypy.__version__) < LooseVersion('3.2.5')): + return string.encode("utf-8") + return string + + def _uri_to_name(collection, uri): expr = '/%s/(.*?)/?$' % collection m = re.match(expr, uri) -- 1.8.5.3

From: ShaoHe Feng <shaohef@linux.vnet.ibm.com> ERROR:cherrypy.error:[01/Apr/2014:17:46:28] ENGINE Traceback (most recent call last): File "/usr/lib/python2.7/site-packages/cherrypy/wsgiserver/wsgiserver2.py", line 1302, in communicate req.respond() File "/usr/lib/python2.7/site-packages/cherrypy/wsgiserver/wsgiserver2.py", line 831, in respond self.server.gateway(self).respond() File "/usr/lib/python2.7/site-packages/cherrypy/wsgiserver/wsgiserver2.py", line 2126, in respond chunk = chunk.encode('ISO-8859-1') UnicodeEncodeError: 'latin-1' codec can't encode characters in position 529-536: ordinal not in range(256) Signed-off-by: ShaoHe Feng <shaohef@linux.vnet.ibm.com> --- src/kimchi/auth.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/kimchi/auth.py b/src/kimchi/auth.py index 3b33d7f..8fc3125 100644 --- a/src/kimchi/auth.py +++ b/src/kimchi/auth.py @@ -32,7 +32,7 @@ import time from kimchi import template from kimchi.exception import InvalidOperation, OperationFailed -from kimchi.utils import run_command +from kimchi.utils import run_command, safe_encode USER_ID = 'userid' @@ -238,4 +238,4 @@ def kimchiauth(admin_methods=None): cherrypy.response.headers['WWW-Authenticate'] = 'Basic realm=kimchi' e = InvalidOperation('KCHAUTH0002E') - raise cherrypy.HTTPError(401, e.message) + raise cherrypy.HTTPError(401, safe_encode(e.message)) -- 1.8.5.3

please ignore this patch. On 04/01/2014 05:57 PM, shaohef@linux.vnet.ibm.com wrote:
From: ShaoHe Feng <shaohef@linux.vnet.ibm.com>
ERROR:cherrypy.error:[01/Apr/2014:17:46:28] ENGINE Traceback (most recent call last): File "/usr/lib/python2.7/site-packages/cherrypy/wsgiserver/wsgiserver2.py", line 1302, in communicate req.respond() File "/usr/lib/python2.7/site-packages/cherrypy/wsgiserver/wsgiserver2.py", line 831, in respond self.server.gateway(self).respond() File "/usr/lib/python2.7/site-packages/cherrypy/wsgiserver/wsgiserver2.py", line 2126, in respond chunk = chunk.encode('ISO-8859-1') UnicodeEncodeError: 'latin-1' codec can't encode characters in position 529-536: ordinal not in range(256)
Signed-off-by: ShaoHe Feng <shaohef@linux.vnet.ibm.com> --- src/kimchi/auth.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/src/kimchi/auth.py b/src/kimchi/auth.py index 3b33d7f..8fc3125 100644 --- a/src/kimchi/auth.py +++ b/src/kimchi/auth.py @@ -32,7 +32,7 @@ import time
from kimchi import template from kimchi.exception import InvalidOperation, OperationFailed -from kimchi.utils import run_command +from kimchi.utils import run_command, safe_encode
USER_ID = 'userid' @@ -238,4 +238,4 @@ def kimchiauth(admin_methods=None): cherrypy.response.headers['WWW-Authenticate'] = 'Basic realm=kimchi'
e = InvalidOperation('KCHAUTH0002E') - raise cherrypy.HTTPError(401, e.message) + raise cherrypy.HTTPError(401, safe_encode(e.message))
-- Thanks and best regards! Sheldon Feng(冯少合)<shaohef@linux.vnet.ibm.com> IBM Linux Technology Center

On 04/01/2014 06:57 AM, shaohef@linux.vnet.ibm.com wrote:
From: ShaoHe Feng <shaohef@linux.vnet.ibm.com>
Then others can make use of it.
The cherrpy low version, cherrpy HTTPError accept unicode. But the high level version, cherrpy refuse to accept unicode.
Signed-off-by: ShaoHe Feng <shaohef@linux.vnet.ibm.com> Signed-off-by: Zhou Zheng Sheng <zhshzhou@linux.vnet.ibm.com> --- src/kimchi/root.py | 11 +++-------- src/kimchi/utils.py | 8 ++++++++ 2 files changed, 11 insertions(+), 8 deletions(-)
diff --git a/src/kimchi/root.py b/src/kimchi/root.py index 9bae34a..aabae68 100644 --- a/src/kimchi/root.py +++ b/src/kimchi/root.py @@ -31,6 +31,7 @@ from kimchi.control import sub_nodes from kimchi.control.base import Resource from kimchi.control.utils import parse_request from kimchi.exception import MissingParameter, OperationFailed +from kimchi.utils import safe_encode
class Root(Resource): @@ -50,19 +51,13 @@ class Root(Resource): def error_production_handler(self, status, message, traceback, version): data = {'code': status, 'reason': message} res = template.render('error.html', data) - if (type(res) is unicode and - LooseVersion(cherrypy.__version__) < LooseVersion('3.2.5')): - res = res.encode("utf-8") - return res + return safe_encode(res)
def error_development_handler(self, status, message, traceback, version): data = {'code': status, 'reason': message, 'call_stack': cherrypy._cperror.format_exc()} res = template.render('error.html', data) - if (type(res) is unicode and - LooseVersion(cherrypy.__version__) < LooseVersion('3.2.5')): - res = res.encode("utf-8") - return res + return safe_encode(res)
def get(self): return self.default(self.default_page) diff --git a/src/kimchi/utils.py b/src/kimchi/utils.py index 6c29e0e..bfad775 100644 --- a/src/kimchi/utils.py +++ b/src/kimchi/utils.py @@ -31,6 +31,7 @@ from multiprocessing import Process, Queue from threading import Timer
from cherrypy.lib.reprconf import Parser +from distutils.version import LooseVersion
from kimchi.asynctask import AsyncTask from kimchi.config import paths, PluginPaths @@ -41,6 +42,13 @@ kimchi_log = cherrypy.log.error_log task_id = 0
+def safe_encode(string): + if (type(string) is unicode and + LooseVersion(cherrypy.__version__) < LooseVersion('3.2.5')): + return string.encode("utf-8") + return string + +
As it is related to cherrypy, ie, controller, I suggest put it into src/kimchi/control/utils.py
def _uri_to_name(collection, uri): expr = '/%s/(.*?)/?$' % collection m = re.match(expr, uri)
participants (3)
-
Aline Manera
-
shaohef@linux.vnet.ibm.com
-
Sheldon