Skip to content

gh-153400: Add a syscall fallback for os.statx - #159043

Open
jjhelmus wants to merge 3 commits into
python:mainfrom
jjhelmus:jjh/statx_syscall_fallback
Open

jjhelmus wants to merge 3 commits into
python:mainfrom
jjhelmus:jjh/statx_syscall_fallback

Conversation

@jjhelmus

@jjhelmus jjhelmus commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Following the pattern in #155520, this adds a syscall fallback for statx.

Expose os.statx when built with Linux headers that provide the syscall number and statx struct, even if the build time libc lacks a statx wrapper.

Call the libc wrapper when available, to keep the symbol versioning and fortification checks. Fall back to the syscall if the wrapper is absent, either at build or run time. If the run time kernel lacks the syscall an OSError with errno.ENOSYS will be raised.

Detect the optional statx struct fields from the available libc or kernel headers.

os.statx is no longer exposed when the syscall is unavailable at build time, even if libc provides a wrapper. This is a rare edge case where glibc would emulate the behavior of statx at runtime.

Comment thread Doc/library/os.rst

.. versionchanged:: 3.16
The function is also available when Python is built against a libc
that lacks ``statx()``, such as glibc older than 2.28.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect this will be backportable to 3.15 once that is released (although will need a core developer to confirm)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given 3.15 has now released, it isn't backportable anymore unfortunately

@KaiErikNiermann

Copy link
Copy Markdown

I believe this would now need the same try-catch on OSError as the other syscall fallbacks which rely on hasattr (otherwise I believe logging can crash on pre-statx-syscall kernels or the alrdy mentioned seccomp case before it has the chance to fallback to os.stat)

if hasattr(os, 'statx'):
statx_result = os.statx(filename,
os.STATX_BTIME|os.STATX_CTIME|os.STATX_MTIME)

e.g.

if not hasattr(os, 'pidfd_open'):
return False
try:
pid = os.getpid()
os.close(os.pidfd_open(pid, 0))
except OSError:
# blocked by security policy like SECCOMP
return False
return True

jjhelmus and others added 2 commits October 20, 2026 23:27
Expose os.statx when build with Linux headers that provide the syscall
number and struct statx, even if the build-time libc lacks a statx
wrapper.

Call the libc wrapper when available, to keep the symbol versioing and
fortification checks. Fall back to the syscall if the wrapper is absent,
either at build or run time.

Detect optional struct statx fields from the available libc or kernel headers.

os.statx is not longer exposed when the syscall is unavailable at build
time, even is libc provides a wrapper. This is a rare edge case where
glibc would emulate the behavior at runtime.
@ambv
ambv force-pushed the jjh/statx_syscall_fallback branch from 411cbb0 to 372ca48 Compare October 20, 2026 21:29
@read-the-docs-community

read-the-docs-community Bot commented Oct 20, 2026 •

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #35285605 | 📁 Comparing 943aa85 against main (80040b6)

  🔍 Preview build  

3 files changed
± library/ast.html
± library/os.html
± whatsnew/changelog.html

@python-cla-bot

python-cla-bot Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@ambv
ambv force-pushed the jjh/statx_syscall_fallback branch from db0e4e4 to 943aa85 Compare October 21, 2026 01:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants