Fix Windows AttributeError: 'NoneType' object has no attribute 'encoding' (#9496)

Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
Co-authored-by: UltralyticsAssistant <web@ultralytics.com>
pull/9490/head
gs80140 10 months ago committed by GitHub
parent de2641fad7
commit e5f4f5c8b9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 31
      ultralytics/utils/__init__.py

@ -230,37 +230,42 @@ def plt_settings(rcparams=None, backend="Agg"):
return decorator return decorator
def set_logging(name=LOGGING_NAME, verbose=True): def set_logging(name="LOGGING_NAME", verbose=True):
"""Sets up logging for the given name with UTF-8 encoding support.""" """Sets up logging for the given name with UTF-8 encoding support, ensuring compatibility across different
environments.
"""
level = logging.INFO if verbose and RANK in {-1, 0} else logging.ERROR # rank in world for Multi-GPU trainings level = logging.INFO if verbose and RANK in {-1, 0} else logging.ERROR # rank in world for Multi-GPU trainings
# Configure the console (stdout) encoding to UTF-8 # Configure the console (stdout) encoding to UTF-8, with checks for compatibility
formatter = logging.Formatter("%(message)s") # Default formatter formatter = logging.Formatter("%(message)s") # Default formatter
if WINDOWS and sys.stdout.encoding != "utf-8": if WINDOWS and hasattr(sys.stdout, "encoding") and sys.stdout.encoding != "utf-8":
class CustomFormatter(logging.Formatter):
def format(self, record):
"""Sets up logging with UTF-8 encoding and configurable verbosity."""
return emojis(super().format(record))
try: try:
# Attempt to reconfigure stdout to use UTF-8 encoding if possible
if hasattr(sys.stdout, "reconfigure"): if hasattr(sys.stdout, "reconfigure"):
sys.stdout.reconfigure(encoding="utf-8") sys.stdout.reconfigure(encoding="utf-8")
# For environments where reconfigure is not available, wrap stdout in a TextIOWrapper
elif hasattr(sys.stdout, "buffer"): elif hasattr(sys.stdout, "buffer"):
import io import io
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8") sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8")
else: else:
sys.stdout.encoding = "utf-8" formatter = CustomFormatter("%(message)s")
except Exception as e: except Exception as e:
print(f"Creating custom formatter for non UTF-8 environments due to {e}") print(f"Creating custom formatter for non UTF-8 environments due to {e}")
formatter = CustomFormatter("%(message)s")
class CustomFormatter(logging.Formatter): # Create and configure the StreamHandler with the appropriate formatter and level
def format(self, record):
"""Sets up logging with UTF-8 encoding and configurable verbosity."""
return emojis(super().format(record))
formatter = CustomFormatter("%(message)s") # Use CustomFormatter to eliminate UTF-8 output as last recourse
# Create and configure the StreamHandler
stream_handler = logging.StreamHandler(sys.stdout) stream_handler = logging.StreamHandler(sys.stdout)
stream_handler.setFormatter(formatter) stream_handler.setFormatter(formatter)
stream_handler.setLevel(level) stream_handler.setLevel(level)
# Set up the logger
logger = logging.getLogger(name) logger = logging.getLogger(name)
logger.setLevel(level) logger.setLevel(level)
logger.addHandler(stream_handler) logger.addHandler(stream_handler)

Loading…
Cancel
Save