File tree Expand file tree Collapse file tree 3 files changed +29
-1
lines changed
Expand file tree Collapse file tree 3 files changed +29
-1
lines changed Original file line number Diff line number Diff line change @@ -50,6 +50,14 @@ def pathname2url(p):
5050 # becomes
5151 # ///C:/foo/bar/spam.foo
5252 import urllib .parse
53+ # First, clean up some special forms. We are going to sacrifice
54+ # the additional information anyway
55+ if p [:4 ] == '\\ \\ ?\\ ' :
56+ p = p [4 :]
57+ if p [:4 ].upper () == 'UNC\\ ' :
58+ p = '\\ ' + p [4 :]
59+ elif p [1 :2 ] != ':' :
60+ raise OSError ('Bad path: ' + p )
5361 if not ':' in p :
5462 # No drive specifier, just convert slashes and quote the name
5563 if p [:2 ] == '\\ \\ ' :
@@ -59,7 +67,7 @@ def pathname2url(p):
5967 p = '\\ \\ ' + p
6068 components = p .split ('\\ ' )
6169 return urllib .parse .quote ('/' .join (components ))
62- comp = p .split (':' )
70+ comp = p .split (':' , maxsplit = 2 )
6371 if len (comp ) != 2 or len (comp [0 ]) > 1 :
6472 error = 'Bad path: ' + p
6573 raise OSError (error )
Original file line number Diff line number Diff line change @@ -1526,6 +1526,24 @@ def test_quoting(self):
15261526 "url2pathname() failed; %s != %s" %
15271527 (expect , result ))
15281528
1529+ @unittest .skipUnless (sys .platform == 'win32' ,
1530+ 'test specific to the nturl2path functions.' )
1531+ def test_prefixes (self ):
1532+ # Test special prefixes are correctly handled in pathname2url()
1533+ given = '\\ \\ ?\\ C:\\ dir'
1534+ expect = '///C:/dir'
1535+ result = urllib .request .pathname2url (given )
1536+ self .assertEqual (expect , result ,
1537+ "pathname2url() failed; %s != %s" %
1538+ (expect , result ))
1539+ given = '\\ \\ ?\\ unc\\ server\\ share\\ dir'
1540+ expect = '/server/share/dir'
1541+ result = urllib .request .pathname2url (given )
1542+ self .assertEqual (expect , result ,
1543+ "pathname2url() failed; %s != %s" %
1544+ (expect , result ))
1545+
1546+
15291547 @unittest .skipUnless (sys .platform == 'win32' ,
15301548 'test specific to the urllib.url2path function.' )
15311549 def test_ntpath (self ):
Original file line number Diff line number Diff line change 1+ :mod: `urllib ` can now convert Windows paths with ``\\?\ `` prefixes into URL
2+ paths.
You can’t perform that action at this time.
0 commit comments