summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHelmut Grohne <helmut@subdivi.de>2025-05-20 10:03:14 +0200
committerHelmut Grohne <helmut@subdivi.de>2025-05-20 10:03:14 +0200
commit68d7e2a4478ed06a161f894262dbba0824261e30 (patch)
tree7347615a7f44b67366be25d1527f1b7ee607fb60
parentc27648e8ab2e0301a7ba5e519322fb5d3af01b4c (diff)
downloadpython-linuxnamespaces-68d7e2a4478ed06a161f894262dbba0824261e30.tar.gz
fix AtLocation.chdir for the mixed case
When both fd and location are given, it would previously ignore the fd component. To properly implement that, we must open and fchdir.
-rw-r--r--linuxnamespaces/atlocation.py8
1 files changed, 7 insertions, 1 deletions
diff --git a/linuxnamespaces/atlocation.py b/linuxnamespaces/atlocation.py
index 20d402a..a4255a6 100644
--- a/linuxnamespaces/atlocation.py
+++ b/linuxnamespaces/atlocation.py
@@ -7,6 +7,7 @@ to work with a location described in this way and this module provides support
code for doing so.
"""
+import contextlib
import enum
import errno
import os
@@ -218,7 +219,12 @@ class AtLocation:
"chdir on AtLocation only supports flag AT_EMPTY_PATH"
)
assert self.location
- return os.chdir(self.location)
+ if self.fd == AT_FDCWD:
+ return os.chdir(self.location)
+ with contextlib.closing(
+ FileDescriptor(self.open(flags=os.O_PATH | os.O_CLOEXEC))
+ ) as dirfd:
+ return os.fchdir(dirfd)
def chmod(self, mode: int) -> None:
"""Wrapper for os.chmod or os.fchmod."""