Skip to content

Commit 3a202de

Browse files
miss-islingtonarhadthedev
andauthoredJun 10, 2022
gh-91317: Document that Path does not collapse initial // (GH-32193)
Documentation for `pathlib` says: > Spurious slashes and single dots are collapsed, but double dots ('..') are not, since this would change the meaning of a path in the face of symbolic links: However, it omits that initial double slashes also aren't collapsed. Later, in documentation of `PurePath.drive`, `PurePath.root`, and `PurePath.name` it mentions UNC but: - this abbreviation says nothing to a person who is unaware about existence of UNC (Wikipedia doesn't help either by [giving a disambiguation page](https://en.wikipedia.org/wiki/UNC)) - it shows up only if a person needs to use a specific property or decides to fully learn what the module provides. For context, see the BPO entry. (cherry picked from commit 78f1a43) Co-authored-by: Oleg Iarygin <oleg@arhadthedev.net>
1 parent f9d0240 commit 3a202de

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed
 

‎Doc/library/pathlib.rst

+30-3
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,13 @@ we also call *flavours*:
133133
PureWindowsPath('c:/Program Files')
134134

135135
Spurious slashes and single dots are collapsed, but double dots (``'..'``)
136-
are not, since this would change the meaning of a path in the face of
137-
symbolic links::
136+
and leading double slashes (``'//'``) are not, since this would change the
137+
meaning of a path for various reasons (e.g. symbolic links, UNC paths)::
138138

139139
>>> PurePath('foo//bar')
140140
PurePosixPath('foo/bar')
141+
>>> PurePath('//foo/bar')
142+
PurePosixPath('//foo/bar')
141143
>>> PurePath('foo/./bar')
142144
PurePosixPath('foo/bar')
143145
>>> PurePath('foo/../bar')
@@ -166,13 +168,17 @@ we also call *flavours*:
166168
.. class:: PureWindowsPath(*pathsegments)
167169

168170
A subclass of :class:`PurePath`, this path flavour represents Windows
169-
filesystem paths::
171+
filesystem paths, including `UNC paths`_::
170172

171173
>>> PureWindowsPath('c:/Program Files/')
172174
PureWindowsPath('c:/Program Files')
175+
>>> PureWindowsPath('//server/share/file')
176+
PureWindowsPath('//server/share/file')
173177

174178
*pathsegments* is specified similarly to :class:`PurePath`.
175179

180+
.. _unc paths: https://en.wikipedia.org/wiki/Path_(computing)#UNC
181+
176182
Regardless of the system you're running on, you can instantiate all of
177183
these classes, since they don't provide any operation that does system calls.
178184

@@ -309,6 +315,27 @@ Pure paths provide the following methods and properties:
309315
>>> PureWindowsPath('//host/share').root
310316
'\\'
311317

318+
If the path starts with more than two successive slashes,
319+
:class:`~pathlib.PurePosixPath` collapses them::
320+
321+
>>> PurePosixPath('//etc').root
322+
'//'
323+
>>> PurePosixPath('///etc').root
324+
'/'
325+
>>> PurePosixPath('////etc').root
326+
'/'
327+
328+
.. note::
329+
330+
This behavior conforms to *The Open Group Base Specifications Issue 6*,
331+
paragraph `4.11 *Pathname Resolution* <xbd_path_resolution>`_:
332+
333+
*"A pathname that begins with two successive slashes may be interpreted in
334+
an implementation-defined manner, although more than two leading slashes
335+
shall be treated as a single slash."*
336+
337+
.. xbd_path_resolution: https://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap04.html#tag_04_11
338+
312339
.. data:: PurePath.anchor
313340

314341
The concatenation of the drive and root::
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Document that :class:`pathlib.PurePath` does not collapse
2+
initial double slashes because they denote UNC paths.

0 commit comments

Comments
 (0)
Please sign in to comment.