From 4dfa157161b1731d07c4e894806a99092ab2ff58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=97=E6=AA=8E?= Date: Mon, 6 Mar 2017 13:42:00 +0800 Subject: [PATCH] use condition rather than try/catch --- cps/helper.py | 6 +++--- cps/web.py | 6 ++++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/cps/helper.py b/cps/helper.py index b4d2ceed..3430de97 100755 --- a/cps/helper.py +++ b/cps/helper.py @@ -245,9 +245,9 @@ def get_valid_filename(value, replace_whitespace=True): value=value.replace(u'ß',u'ss') value = unicodedata.normalize('NFKD', value) re_slugify = re.compile('[\W\s-]', re.UNICODE) - try: - value = str(re_slugify.sub('', value).strip()) - except UnicodeEncodeError as e: #will exception on Python2.7 + if type(value) is str: #Python3 str, Python2 unicode + value = re_slugify.sub('', value).strip() + else: value = unicode(re_slugify.sub('', value).strip()) if replace_whitespace: #*+:\"/<>? werden durch _ ersetzt diff --git a/cps/web.py b/cps/web.py index 560ddd67..fc3e81ad 100755 --- a/cps/web.py +++ b/cps/web.py @@ -1052,8 +1052,10 @@ def stats(): stdin=subprocess.PIPE) p.wait() for lines in p.stdout.readlines(): - if re.search('Amazon kindlegen\(', str(lines)): - versions['KindlegenVersion'] = str(lines) + if type(lines) is bytes: + lines = lines.decode('utf-8') + if re.search('Amazon kindlegen\(', lines): + versions['KindlegenVersion'] = lines versions['PythonVersion'] = sys.version versions['babel'] = babelVersion versions['sqlalchemy'] = sqlalchemyVersion