python / python/cpython

Inconsistent drive letter case on Windows

未关闭
#100,322 3 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

OS-windows type-bug
主要语言
Python
星标
77.2k
派生
36k
PR 合并指标
PR 指标待抓取

描述

Bug report + suggested fix

The os.path.abspath method on windows is inconsistent when pre-pending the drive letter as it can be either upper or lowercase.

It is not entirely clear from Microsoft's documentation (https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getfullpathnamew) when this happens exactly, but, a simple way of re-producing the issue is by changing directory and specifying the opposite case letter in the call

C:\Users\kieran>python -c "import os; print(os.path.abspath('.'))"
C:\Users\kieran

C:\Users\kieran>cd c:\Users\kieran\Projects

c:\Users\kieran\Projects>python -c "import os; print(os.path.abspath('.'))"
c:\Users\kieran\Projects

This is very frustrating when trying to compare paths, and, sometimes different tools in the same development environments will run in such a way that they change their drive letter case, leading to issues between those tools or differences inside tests.

I would like to suggest that we enforce the lowercase driver letters by making a small change to https://github.com/python/cpython/blob/0fe61d0838218b18bcff4bf434beba3e8dbcc42b/Modules/posixmodule.c#L4236-L4274

If we were to include an iteration (line 4266) of the buffered path that lowercased the chars up to the colon (guaranteed to be set), this would ensure consistent drive letters.

Your environment

Python 3.9.6
Microsoft Windows 10 Pro - 10.0.19044 Build 19044
x64-based PC

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

先从 Modules/posixmodule.c 中链接行附近的 Windows 路径处理代码开始,然后使用报告中的 os.path.abspath 命令重现该行为。确认如何获取驱动器号,并将 Windows 绝对路径中的驱动器号大小写一致且相关行为由测试覆盖定义为完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
c, python
领域
operating-systems
Issue 类型
缺陷
难度
3/5
预计耗时
1-2 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
45/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。