python / python/cpython

Windows os.stat and os.fstat report different st_ctime_ns values

Ouverte
#157,671 4 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

extension-modules OS-windows
Langage dominant
Python
Étoiles
77.2k
Forks
35.9k
Métriques de merge des PR
Métriques de PR en attente

Description

Bug description:

On Windows, os.stat(path).st_ctime_ns and os.fstat(fd).st_ctime_ns can differ for the same file.

I ran the example below on Windows 11 with NTFS. It fails with both official Python 3.14.7 Windows builds: ARM64 running natively and x64 running under emulation. The same-file assertion passes, but the timestamp assertion fails. Both st_birthtime_ns values equal os.stat(path).st_ctime_ns.

In the 3.14.7 code:

Commit a01fb672 added that compatibility step. The discussion in #102149 says st_ctime should continue returning creation time during the deprecation period. Should fstat() do the same?

In #117354, DirEntry.stat() was changed to return creation time during the same deprecation period, following #117267.

Example:

import os
import time
from pathlib import Path
from tempfile import TemporaryDirectory

with TemporaryDirectory() as directory:
    path = Path(directory) / "example.txt"
    path.write_text("before")
    time.sleep(0.1)
    path.write_text("after")

    with path.open("rb") as source:
        by_path = os.stat(path)
        by_fd = os.fstat(source.fileno())

    print("stat ctime:", by_path.st_ctime_ns)
    print("fstat ctime:", by_fd.st_ctime_ns)
    print("stat birthtime:", getattr(by_path, "st_birthtime_ns", None))
    print("fstat birthtime:", getattr(by_fd, "st_birthtime_ns", None))
    assert os.path.samestat(by_path, by_fd)
    assert by_path.st_ctime_ns == by_fd.st_ctime_ns

Output from the official Python 3.14.7 ARM64 build:

stat ctime: 1789643317486473600
fstat ctime: 1789643317591142500
stat birthtime: 1789643317486473600
fstat birthtime: 1789643317486473600

The final assertion raises AssertionError.

CPython versions tested on:
  • 3.10.20 (conda-forge build): timestamps match.
  • 3.14.6 (Anaconda build): timestamps differ.
  • 3.14.7: timestamps differ with both official Windows embeddable builds, ARM64 and x64.
Operating systems tested on:

Windows 11 ARM64, build 26200.9457, in Parallels Desktop. The test files were on NTFS. The ARM64 Python build ran natively and the x64 build ran under Windows emulation.

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Commencez par Python/fileutils.c à _Py_attribute_data_to_stat et _Py_fstat_noraise, puis examinez Modules/posixmodule.c à os_fstat_impl et win32_xstat. Exécutez la reproduction fournie sous Windows et vérifiez que os.stat() et os.fstat() indiquent des valeurs ctime_ns identiques tout en préservant le comportement d’heure de création montré.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
python
Domaine
operating-systems
Type d'issue
Bug
Difficulté
3/5
Temps estimé
1-2 jours
Activité
Active
Clarté
Plutôt claire
Accessibilité débutants
72/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.