def _set_reg_value(root, key_path, name, value, reg_type=winreg.REG_SZ, verbose=False):
    """Helper to create key and set a registry value using CreateKeyEx."""
    rootkey_name = "HKEY_CURRENT_USER" if root == winreg.HKEY_CURRENT_USER else "HKEY_LOCAL_MACHINE"
    if verbose:
        print(f"{rootkey_name}\\{key_path}\\{name if name  else ''}:{value}")
    try:
        # Use CreateKeyEx with context manager for automatic closing
        with winreg.CreateKeyEx(root, key_path, 0, winreg.KEY_WRITE) as key:
             winreg.SetValueEx(key, name, 0, reg_type, value)
    except OSError as e:
         print(f"Error creating/setting registry value {rootkey_name}\\{key_path}\\{name}: {e}", file=sys.stderr)
Helper to create key and set a registry value using CreateKeyEx.
winpython/associate.py