[Python-checkins] peps: The repr() of a Windows path now uses forward slashes (Guido).

antoine.pitrou python-checkins at python.org
Sun Nov 17 16:33:10 CET 2013


http://hg.python.org/peps/rev/82a9a30d8e57
changeset:   5282:82a9a30d8e57
user:        Antoine Pitrou <solipsis at pitrou.net>
date:        Sun Nov 17 16:33:06 2013 +0100
summary:
  The repr() of a Windows path now uses forward slashes (Guido).
Note that str() and bytes() still use backward slashes, which is the canonical path syntax.

files:
  pep-0428.txt |  37 ++++++++++++++++++++++---------------
  1 files changed, 22 insertions(+), 15 deletions(-)


diff --git a/pep-0428.txt b/pep-0428.txt
--- a/pep-0428.txt
+++ b/pep-0428.txt
@@ -243,8 +243,8 @@
 
 * A POSIX path is absolute if it has a root.  A Windows path is absolute if
   it has both a drive *and* a root.  A Windows UNC path (e.g.
-  ``\\some\share\myfile.txt``) always has a drive and a root
-  (here, ``\\some\share`` and ``\``, respectively).
+  ``\\host\share\myfile.txt``) always has a drive and a root
+  (here, ``\\host\share`` and ``\``, respectively).
 
 * A path which has either a drive *or* a root is said to be anchored.
   Its anchor is the concatenation of the drive and root.  Under POSIX,
@@ -281,7 +281,7 @@
 However, with Windows paths, the drive is retained as necessary::
 
     >>> PureWindowsPath('c:/foo', '/Windows')
-    PureWindowsPath('c:\\Windows')
+    PureWindowsPath('c:/Windows')
     >>> PureWindowsPath('c:/foo', 'd:')
     PureWindowsPath('d:')
 
@@ -303,7 +303,7 @@
 notation)::
 
     >>> PureWindowsPath('//some/path')
-    PureWindowsPath('\\\\some\\path\\')
+    PureWindowsPath('//some/path/')
 
 On POSIX, they are collapsed except if there are exactly two leading slashes,
 which is a special case in the POSIX specification on `pathname resolution`_
@@ -343,12 +343,12 @@
     'c:/windows'
 
 To get the bytes representation (which might be useful under Unix systems),
-call ``bytes()`` on it::
+call ``bytes()`` on it, which internally uses ``os.fsencode()``::
 
     >>> bytes(p)
     b'/home/antoine/pathlib/setup.py'
 
-To represent the path as a ``file`` URI, call the ``as_uri()`` method::
+To represent the path as a ``file:`` URI, call the ``as_uri()`` method::
 
     >>> p = PurePosixPath('/etc/passwd')
     >>> p.as_uri()
@@ -357,6 +357,13 @@
     >>> p.as_uri()
     'file:///c:/Windows'
 
+The repr() of a path always uses forward slashes, even under Windows, for
+readability and to remind users that forward slashes are ok::
+
+    >>> p = PureWindowsPath('c:/Windows')
+    >>> p
+    PureWindowsPath('c:/Windows')
+
 
 Properties
 ----------
@@ -416,7 +423,7 @@
 
     >>> p = PureWindowsPath('c:/Downloads/pathlib.tar.gz')
     >>> p.with_name('setup.py')
-    PureWindowsPath('c:\\Downloads\\setup.py')
+    PureWindowsPath('c:/Downloads/setup.py')
 
 It fails with a ``ValueError`` if the path doesn't have an actual name::
 
@@ -426,7 +433,7 @@
       File "<stdin>", line 1, in <module>
       File "pathlib.py", line 875, in with_name
         raise ValueError("%r has an empty name" % (self,))
-    ValueError: PureWindowsPath('c:\\') has an empty name
+    ValueError: PureWindowsPath('c:/') has an empty name
     >>> p.name
     ''
 
@@ -435,7 +442,7 @@
 
     >>> p = PureWindowsPath('c:/Downloads/pathlib.tar.gz')
     >>> p.with_suffix('.bz2')
-    PureWindowsPath('c:\\Downloads\\pathlib.tar.bz2')
+    PureWindowsPath('c:/Downloads/pathlib.tar.bz2')
     >>> p = PureWindowsPath('README')
     >>> p.with_suffix('.bz2')
     PureWindowsPath('README.bz2')
@@ -491,11 +498,11 @@
 The ``parent()`` method returns an ancestor of the path::
 
     >>> p.parent()
-    PureWindowsPath('c:\\python33\\bin')
+    PureWindowsPath('c:/python33/bin')
     >>> p.parent(2)
-    PureWindowsPath('c:\\python33')
+    PureWindowsPath('c:/python33')
     >>> p.parent(3)
-    PureWindowsPath('c:\\')
+    PureWindowsPath('c:/')
 
 The ``parents()`` method automates repeated invocations of ``parent()``, until
 the anchor is reached::
@@ -503,9 +510,9 @@
     >>> p = PureWindowsPath('c:/python33/bin/python.exe')
     >>> for parent in p.parents(): parent
     ...
-    PureWindowsPath('c:\\python33\\bin')
-    PureWindowsPath('c:\\python33')
-    PureWindowsPath('c:\\')
+    PureWindowsPath('c:/python33/bin')
+    PureWindowsPath('c:/python33')
+    PureWindowsPath('c:/')
 
 
 Querying

-- 
Repository URL: http://hg.python.org/peps


More information about the Python-checkins mailing list