MCPcopy Create free account
hub / github.com/StackStorm/st2 / _patch_stdout

Function _patch_stdout

st2common/st2common/log.py:242–266  ·  view source on GitHub ↗

This function re-opens sys.stdout using utf-8 encoding. It's to be used in situations where process encoding / locale is not set to utf-8. In such situations when unicode sequence is logged, it would cause logging formatter to go in an infite loop on formatting a record. This

()

Source from the content-addressed store, hash-verified

240
241
242def _patch_stdout():
243 """
244 This function re-opens sys.stdout using utf-8 encoding.
245
246 It's to be used in situations where process encoding / locale is not set to utf-8. In such
247 situations when unicode sequence is logged, it would cause logging formatter to go in an infite
248 loop on formatting a record.
249
250 This function works around that by ensuring sys.stdout is always opened in utf-8 mode.
251 """
252
253 stdout_encoding = str(getattr(sys.stdout, "encoding", "none")).lower()
254
255 if stdout_encoding not in ["utf8", "utf-8"] and PATCH_STDOUT:
256 LOG.info(
257 "Patching sys.stdout and re-opening it with utf-8 encoding (originally opened "
258 "with %s encoding)..." % (stdout_encoding)
259 )
260 sys.stdout = open(
261 sys.stdout.fileno(),
262 mode="w",
263 encoding="utf-8",
264 errors="replace",
265 buffering=1,
266 )
267
268
269def setup(

Callers 1

setupFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected