summaryrefslogtreecommitdiff
path: root/fail2ban/helpers.py
diff options
context:
space:
mode:
authorsebres <serg.brester@sebres.de>2018-06-29 22:02:41 +0200
committersebres <serg.brester@sebres.de>2018-06-29 22:02:41 +0200
commit227550684ae75cee1f7f8aa3be8482a19052e671 (patch)
tree5fc9f6b8d7cb59cc0a5914cbdac4a72992db2918 /fail2ban/helpers.py
parent79350e916e79ec2d91d3430e6796f26da386ecf0 (diff)
downloadfail2ban-227550684ae75cee1f7f8aa3be8482a19052e671.tar.gz
**interim** try to fix several conversion errors
Diffstat (limited to 'fail2ban/helpers.py')
-rw-r--r--fail2ban/helpers.py24
1 files changed, 23 insertions, 1 deletions
diff --git a/fail2ban/helpers.py b/fail2ban/helpers.py
index f7b0f1eb..3c4e6ee6 100644
--- a/fail2ban/helpers.py
+++ b/fail2ban/helpers.py
@@ -36,9 +36,31 @@ from .server.mytime import MyTime
PREFER_ENC = locale.getpreferredencoding()
# correct preferred encoding if lang not set in environment:
if PREFER_ENC.startswith('ANSI_'): # pragma: no cover
- if all((os.getenv(v) in (None, "") for v in ('LANGUAGE', 'LC_ALL', 'LC_CTYPE', 'LANG'))):
+ if sys.stdout and not sys.stdout.encoding.startswith('ANSI_'):
+ PREFER_ENC = sys.stdout.encoding
+ elif all((os.getenv(v) in (None, "") for v in ('LANGUAGE', 'LC_ALL', 'LC_CTYPE', 'LANG'))):
PREFER_ENC = 'UTF-8';
+# py-2.x: try to minimize influence of sporadic conversion errors on python 2.x,
+# caused by implicit converting of string/unicode (e. g. `str(u"\uFFFD")` produces an error
+# if default encoding is 'ascii');
+if sys.version_info < (3,): # python >= 2.6
+ # correct default (global system) encoding (mostly UTF-8):
+ def __resetDefaultEncoding(encoding):
+ global PREFER_ENC
+ ode = sys.getdefaultencoding().upper()
+ if ode == 'ASCII' or ode != PREFER_ENC.upper():
+ # setdefaultencoding is normally deleted after site initialized, so hack-in using load of sys-module:
+ from imp import load_dynamic as __ldm
+ _sys = __ldm('_sys', 'sys')
+ _sys.setdefaultencoding(encoding)
+ # override to PREFER_ENC:
+ __resetDefaultEncoding(PREFER_ENC)
+ del __resetDefaultEncoding
+
+# todo: rewrite explicit (and implicit) str-conversions via encode/decode with IO-encoding (sys.stdout.encoding),
+# e. g. inside tags-replacement by command-actions, etc.
+
def formatExceptionInfo():
""" Consistently format exception information """