@cryptotaxi247 / netdata-1 / commits / 34a7b8ad0

Do not crash when logging UTF-8 data in Python 2 (#7376)

When the received data contained UTF-8 characters, the plugin would crash trying to log these UTF-8 characters (when run using Python 2). Use Python 2's `unicode` function to circumvent this, but fall back back to `str` if it doesn't exist e.g. in Python 3. Fixes #7190.

Vince Zarola committed Nov 28, 2019 at 09:02 UTC 34a7b8ad0a992f1f764ea38eacdb6edd44ce2885
3 files changed +24 -8
collectors/python.d.plugin/python_modules/bases/FrameworkServices/SocketService.py
+1 -1
@@ -247,7 +247,7 @@ class SocketService(SimpleService):
247 if self._check_raw_data(data):
248 break
249
250 - self.debug('final response: {0}'.format(data))
250 + self.debug(u'final response: {0}'.format(data))
251 return data
252
253 def _get_raw_data(self, raw=False, request=None):
collectors/python.d.plugin/python_modules/bases/collection.py
+16
@@ -82,3 +82,19 @@ def read_last_line(f):
82 break
83 result = opened.readline()
84 return result.decode()
85 +
86 +
87 +def unicode_str(arg):
88 + """Return the argument as a unicode string.
89 +
90 + The `unicode` function has been removed from Python3 and `str` takes its
91 + place. This function is a helper which will try using Python 2's `unicode`
92 + and if it doesn't exist, assume we're using Python 3 and use `str`.
93 +
94 + :param arg:
95 + :return: <str>
96 + """
97 + try:
98 + return unicode(arg)
99 + except NameError:
100 + return str(arg)
collectors/python.d.plugin/python_modules/bases/loggers.py
+7 -7
@@ -13,7 +13,7 @@ try:
13 except ImportError:
14 from time import time
15
16 -from bases.collection import on_try_except_finally
16 +from bases.collection import on_try_except_finally, unicode_str
17
18
19 LOGGING_LEVELS = {'CRITICAL': 50,
@@ -121,23 +121,23 @@ class BaseLogger(object):
121 self.logger.setLevel(LOGGING_LEVELS[level])
122
123 def debug(self, *msg, **kwargs):
124 - self.logger.debug(' '.join(map(str, msg)), **kwargs)
124 + self.logger.debug(' '.join(map(unicode_str, msg)), **kwargs)
125
126 def info(self, *msg, **kwargs):
127 - self.logger.info(' '.join(map(str, msg)), **kwargs)
127 + self.logger.info(' '.join(map(unicode_str, msg)), **kwargs)
128
129 def warning(self, *msg, **kwargs):
130 - self.logger.warning(' '.join(map(str, msg)), **kwargs)
130 + self.logger.warning(' '.join(map(unicode_str, msg)), **kwargs)
131
132 def error(self, *msg, **kwargs):
133 - self.logger.error(' '.join(map(str, msg)), **kwargs)
133 + self.logger.error(' '.join(map(unicode_str, msg)), **kwargs)
134
135 def alert(self, *msg, **kwargs):
136 - self.logger.critical(' '.join(map(str, msg)), **kwargs)
136 + self.logger.critical(' '.join(map(unicode_str, msg)), **kwargs)
137
138 @on_try_except_finally(on_finally=(exit, 1))
139 def fatal(self, *msg, **kwargs):
140 - self.logger.critical(' '.join(map(str, msg)), **kwargs)
140 + self.logger.critical(' '.join(map(unicode_str, msg)), **kwargs)
141
142
143 class PythonDLogger(object):