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
()
| 240 | |
| 241 | |
| 242 | def _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 | |
| 269 | def setup( |