python / python/cpython

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

Đang mở
#157,671 4 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

extension-modules OS-windows
Ngôn ngữ chính
Python
Star
77.2k
Fork
35.9k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

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.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu với Python/fileutils.c tại _Py_attribute_data_to_stat và _Py_fstat_noraise, sau đó kiểm tra Modules/posixmodule.c tại os_fstat_impl và win32_xstat. Chạy bản tái hiện được cung cấp trên Windows và xác minh rằng os.stat() và os.fstat() báo cáo các giá trị ctime_ns khớp nhau, đồng thời giữ nguyên hành vi thời gian tạo được hiển thị.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python
Lĩnh vực
operating-systems
Loại issue
Lỗi
Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức độ hoạt động
Sôi nổi
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
72/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.