os._win32

Standard library module View source

Windows backend for os; native little-endian Windows x64/ARM64 only.

All foreign declarations are local to functions and nothing calls Windows at module initialization. The public os module supplies compile-time dispatch and the environ mapping. This module does not import os or port the compiler/runtime. Called functions require Kernel32 and a compatible Microsoft UCRT; no new C++ runtime helpers are needed.

Paths, commands and environment use UTF-16LE with surrogatepass, never ANSI. getcwdb() uses UTF-8 with surrogatepass, as modern Python on Windows does. Extended-length paths are accepted as supplied; no automatic long-path prefix/manifest is added. mkdir() ignores mode and inherits the parent's Windows security descriptor (even for 0o700); chmod() changes only the read-only attribute. access() is an attribute probe, not an ACL check, and X_OK means existence. Descriptor I/O retains the descriptor's CRT text/binary mode and can return partial transfers.

Environment updates are not atomic across the CRT and OS and must be externally synchronized with other environment writers. The CRT treats an empty value as deletion; putenv() restores an actual empty OS value afterwards, but cannot make CRT getenv() distinguish empty from missing. Other CRT instances/ANSI mirrors cannot be kept fully Unicode-consistent. No snapshot is updated here.

OSError carries errno, strerror, applicable filenames and the original winerror for Windows API failures. CRT failures supply errno without inventing winerror. Toolhelp reports the recorded parent PID, which can have exited or been reused. This backend has not been executed on Windows as part of its implementation.

function stat
stat(path: str, follow_symlinks: bool = True)
function fstat
fstat(fd: int)
function open
open(path: str, flags: int, mode: int = 0o777)
function listdir
listdir(path: str = ".")
function realpath
realpath(path: str, strict: bool = False)
function make_env_map
make_env_map()

Return an uppercase-key snapshot of the actual Windows environment.

function putenv
putenv(key: str, value: str)

Update CRT and OS environment, but not the public environ snapshot.

function unsetenv
unsetenv(key: str)
function process_getenv
process_getenv(key: str)

Query the true OS environment, distinguishing empty from missing.

function system
system(cmd: str)

Run the Windows command processor; return its native status, not wait bits.

function mkdir
mkdir(path: str, mode: int = 0o777)
function rmdir
rmdir(path: str)
function chdir
chdir(path: str)
function getcwd
getcwd()
function getcwdb
getcwdb()
function remove
remove(path: str)
function rename
rename(src: str, dst: str)
function replace
replace(src: str, dst: str)
function access
access(path: str, mode: int)
function chmod
chmod(path: str, mode: int)
function getpid
getpid()
function getppid
getppid()
function close
close(fd: int)
function read
read(fd: int, n: int)
function write
write(fd: int, data: bytes)
function lseek
lseek(fd: int, pos: int, how: int)
function fsync
fsync(fd: int)
function ftruncate
ftruncate(fd: int, length: int)
function truncate
truncate(path: str, length: int)
function getfullpathname
getfullpathname(path: str)

Resolve Windows drive-relative/UNC syntax without requiring existence.