python / python/cpython

Confusing error message when rename target is an open file

Aberta
#129,230 4 comentários 0 reações 0 responsáveis Ver no GitHub

Ninguém assumiu esta issue ainda.

stdlib topic-pathlib type-bug
Linguagem predominante
Python
Estrelas
77.2k
Forks
36k
Métricas de merge de PRs
Métricas de PR pendentes

Descrição

Bug report

Bug description:

Background

Writing a tool to update and rename some files, I made a mistake and tried to rename a file while it was still open. This works on Linux, but raises an error when writing to a Windows network shares. This is known and documented - see Path.rename docs.

The issue

The problem I have is that the error message is incorrect. It says "FileNotFound", which is confusing because the files do exist. If I had got a more useful message, I would have worked out what the problem was with my tool much sooner.

Repro case

    file1 = Path("test1.txt")
    file2 = Path("test2.txt")
    file1.open("w").write("file 1 text\n")
    file2.open("w").write("file 2 text\n")
    with file1.open():
        file2.rename(file1)

Outcomes;
On linux, writing to pwd or a linux network share, file is rewritten without error.
On linux, writing to a Windows network share, an incorrect error message is given.

$ python test.py
Traceback (most recent call last):
  File "/home/jontwo/test.py", line 17, in <module>
    main()
  File "/home/jontwo/test.py", line 13, in main
    file2.rename(file1)
  File "/usr/lib/python3.12/pathlib.py", line 1363, in rename
    os.rename(self, target)
FileNotFoundError: [Errno 2] No such file or directory: 'test2.txt' -> 'test1.txt'

On Windows, writing to pwd, a useful error message is given.

c:\temp>python test.py
Traceback (most recent call last):
  File "c:\temp\test.py", line 16, in <module>
    main()
  File "c:\temp\test.py", line 12, in main
    file2.rename(file1)
  File "C:\OSGeo4W\apps\Python312\Lib\pathlib.py", line 1363, in rename
    os.rename(self, target)
FileExistsError: [WinError 183] Cannot create a file when that file already exists: 'test2.txt' -> 'test1.txt'
CPython versions tested on:

3.12

Operating systems tested on:

Linux, Windows

Guia de contribuição

Abrir o guia de contribuição

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Direção de pesquisa

Comece reproduzindo o caso de pathlib.Path.rename mostrado na issue no Linux e em condições de compartilhamento de rede do Windows. Inspecione pathlib.py ao redor do ponto de entrada de rename relatado e compare os erros resultantes com o comportamento documentado de Path.rename. O trabalho estará concluído quando o caso em que o destino está aberto não apresentar mais uma mensagem FileNotFound enganosa e o comportamento específico da plataforma estiver coberto por testes apropriados.

Escrita pelo modelo de indexação a partir do texto da issue.

Avaliação

Stack de tecnologia
python
Domínio
operating-systems
Tipo de issue
Bug
Dificuldade
4/5
Tempo estimado
3-5 dias
Status de atividade
Estagnada
Clareza
Razoavelmente clara
Facilidade para iniciantes
45/100

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.