python / python/cpython

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

オープン
#157,671 コメント 4 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

extension-modules OS-windows
主要言語
Python
スター
77.2k
フォーク
35.9k
PR マージ指標
PR 指標を取得中

説明

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.

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

Python/fileutils.c の _Py_attribute_data_to_stat と _Py_fstat_noraise から始め、次に Modules/posixmodule.c の os_fstat_impl と win32_xstat を調べます。Windows で提供された再現手順を実行し、os.stat() と os.fstat() が一致する ctime_ns 値を報告すると同時に、示されている作成時刻の動作が維持されることを確認します。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
operating-systems
issue の種類
バグ
難易度
3/5
見積もり時間
1〜2日
活発さ
活発
明瞭さ
おおむね明確
初心者へのやさしさ
72/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。