Skip to main content

Lib/pathlib/__init__.py

This file is the public face of the pathlib package. It defines the complete object hierarchy for filesystem paths, from the pure (I/O-free) base classes through the concrete OS-aware subclasses. In CPython 3.12 the implementation was split across several submodules (_local, _os, types); this __init__.py re-exports everything that callers expect at the top level.

Source:

cpython 3.14 @ ab2d84fe1023/Lib/pathlib/__init__.py

Map

LinesSymbolRole
1-18module docstring, importsstdlib imports plus conditional pwd/grp
20-27try: import pwd / grpoptional Unix-only imports
29-33from pathlib._os import ...low-level helpers (copyfile2, PathInfo, etc.)
36-40__all__six public names exported from the package
43-46UnsupportedOperationraised when an OS does not support an operation
49-75_PathParentssequence view over a path's logical ancestors
78-...PurePathbase class for all path types; no I/O
~340PurePosixPathpure POSIX subclass
~360PureWindowsPathpure Windows subclass
~380Pathconcrete path with I/O methods
1285PosixPathPath + PurePosixPath; gated on non-Windows
1297WindowsPathPath + PureWindowsPath; gated on Windows

Reading

Re-exports and __all__

The six names in __all__ are the entire public API of the package. All concrete implementations have moved to pathlib._local; this file only re-imports them so that from pathlib import Path continues to work.

# CPython: Lib/pathlib/__init__.py:36 __all__
__all__ = [
"UnsupportedOperation",
"PurePath", "PurePosixPath", "PureWindowsPath",
"Path", "PosixPath", "WindowsPath",
]

PurePath slot layout

PurePath.__slots__ documents the lazy-parsing strategy. The raw strings are stored in _raw_paths at construction time. The parsed _drv, _root, and _tail_cached slots are filled only when the corresponding properties are first accessed, avoiding redundant os.path.splitroot calls.

# CPython: Lib/pathlib/__init__.py:88 PurePath.__slots__
__slots__ = (
'_raw_paths',
'_drv', '_root', '_tail_cached',
'_str',
'_str_normcase_cached',
'_parts_normcase_cached',
# ...
)

Concrete platform subclasses

PosixPath and WindowsPath are thin mixins. Each guards itself with an os.name check so that instantiation on the wrong platform raises UnsupportedOperation rather than silently returning an unusable object.

# CPython: Lib/pathlib/__init__.py:1285 PosixPath
class PosixPath(Path, PurePosixPath):
__slots__ = ()

if os.name == 'nt':
def __new__(cls, *args, **kwargs):
raise UnsupportedOperation(
f"cannot instantiate {cls.__name__!r} on your system")

gopy notes

Status: not yet ported.

Planned package path: module/pathlib/.

The pure path arithmetic (drive/root/tail parsing, comparison, hashing) is self-contained Python and can be ported as a pure-Go struct with string fields. The concrete Path class wraps os.* calls that gopy already exposes through module/os/. The _os helpers (copyfile2, PathInfo) belong in a separate module/pathlib/_os/ internal package once the flat layout is established. The optional pwd/grp imports depend on module/pwd/ and module/grp/ which are not yet scheduled.