From: Georg Brandl <georg@python.org>
Date: Sat, 31 Jul 2010 21:40:15 +0000 (+0000)
Subject: #7909: the prefixes \\.\ and \\?\ indicate special Windows paths, do not try to manip... 
X-Git-Tag: v3.2a2~593
X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=cfb68218b701e1bf73bb82e51dfc8446ab65452a;p=python

#7909: the prefixes \\.\ and \\?\ indicate special Windows paths, do not try to manipulate them.  See http://msdn.microsoft.com/en-us/library/aa365247%28VS.85%29.aspx for details.
---

diff --git a/Lib/ntpath.py b/Lib/ntpath.py
index a6bcca4f4a..ee7ac679c0 100644
--- a/Lib/ntpath.py
+++ b/Lib/ntpath.py
@@ -71,6 +71,12 @@ def _get_colon(path):
     else:
         return ':'
 
+def _get_special(path):
+    if isinstance(path, bytes):
+        return (b'\\\\.\\', b'\\\\?\\')
+    else:
+        return ('\\\\.\\', '\\\\?\\')
+
 # Normalize the case of a pathname and map slashes to backslashes.
 # Other normalizations (such as optimizing '../' away) are not done
 # (this is done by normpath).
@@ -524,6 +530,13 @@ def normpath(path):
     """Normalize path, eliminating double slashes, etc."""
     sep = _get_sep(path)
     dotdot = _get_dot(path) * 2
+    special_prefixes = _get_special(path)
+    if path.startswith(special_prefixes):
+        # in the case of paths with these prefixes:
+        # \\.\ -> device names
+        # \\?\ -> literal paths
+        # do not do any normalization, but return the path unchanged
+        return path
     path = path.replace(_get_altsep(path), sep)
     prefix, path = splitdrive(path)
 
diff --git a/Lib/test/test_ntpath.py b/Lib/test/test_ntpath.py
index 0fd81b63ce..86e3eda46b 100644
--- a/Lib/test/test_ntpath.py
+++ b/Lib/test/test_ntpath.py
@@ -174,6 +174,9 @@ class TestNtpath(unittest.TestCase):
         tester("ntpath.normpath('C:////a/b')", r'C:\a\b')
         tester("ntpath.normpath('//machine/share//a/b')", r'\\machine\share\a\b')
 
+        tester("ntpath.normpath('\\\\.\\NUL')", r'\\.\NUL')
+        tester("ntpath.normpath('\\\\?\\D:/XY\\Z')", r'\\?\D:/XY\Z')
+
     def test_expandvars(self):
         with support.EnvironmentVarGuard() as env:
             env.clear()
diff --git a/Misc/NEWS b/Misc/NEWS
index 768c7df053..398f7db6e1 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -15,6 +15,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #7909: Do not touch paths with the special prefixes ``\\.\``
+  or ``\\?\`` in ntpath.normpath().
+
 - Issue #1286: Allow using fileinput.FileInput as a context manager.
 
 - Add lfu_cache() and lru_cache() decorators to the functools module.