From 52967ec1034ed02f1a12f7bb0eefd1b8b5660f3d Mon Sep 17 00:00:00 2001 From: Eric Schneider <16943959+tailoric@users.noreply.github.com> Date: Sun, 9 Feb 2025 03:08:45 +0100 Subject: [PATCH] Fix path sanitation for absolute Windows paths When using an absolute Windows path (e.g. `C:\Users\USER\Documents\`) for the `newbot` command the translation table replaced the valid `:` character in the drive causing it to create the directory at the wrong place. Fixes #10096 --- discord/__main__.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/discord/__main__.py b/discord/__main__.py index 843274b53..f8556fcdc 100644 --- a/discord/__main__.py +++ b/discord/__main__.py @@ -28,7 +28,7 @@ from typing import Optional, Tuple, Dict import argparse import sys -from pathlib import Path +from pathlib import Path, PurePath, PureWindowsPath import discord import importlib.metadata @@ -225,8 +225,14 @@ def to_path(parser: argparse.ArgumentParser, name: str, *, replace_spaces: bool ) if len(name) <= 4 and name.upper() in forbidden: parser.error('invalid directory name given, use a different one') + path = PurePath(name) + if isinstance(path, PureWindowsPath) and path.drive: + drive, rest = path.parts[0], path.parts[1:] + transformed = tuple(map(lambda p: p.translate(_translation_table), rest)) + name = drive + '\\'.join(transformed) - name = name.translate(_translation_table) + else: + name = name.translate(_translation_table) if replace_spaces: name = name.replace(' ', '-') return Path(name)