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):
50
50
# becomes
51
51
# ///C:/foo/bar/spam.foo
52
52
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 )
53
61
if not ':' in p :
54
62
# No drive specifier, just convert slashes and quote the name
55
63
if p [:2 ] == '\\ \\ ' :
@@ -59,7 +67,7 @@ def pathname2url(p):
59
67
p = '\\ \\ ' + p
60
68
components = p .split ('\\ ' )
61
69
return urllib .parse .quote ('/' .join (components ))
62
- comp = p .split (':' )
70
+ comp = p .split (':' , maxsplit = 2 )
63
71
if len (comp ) != 2 or len (comp [0 ]) > 1 :
64
72
error = 'Bad path: ' + p
65
73
raise OSError (error )
Original file line number Diff line number Diff line change @@ -1489,6 +1489,24 @@ def test_quoting(self):
1489
1489
"url2pathname() failed; %s != %s" %
1490
1490
(expect , result ))
1491
1491
1492
+ @unittest .skipUnless (sys .platform == 'win32' ,
1493
+ 'test specific to the nturl2path functions.' )
1494
+ def test_prefixes (self ):
1495
+ # Test special prefixes are correctly handled in pathname2url()
1496
+ given = '\\ \\ ?\\ C:\\ dir'
1497
+ expect = '///C:/dir'
1498
+ result = urllib .request .pathname2url (given )
1499
+ self .assertEqual (expect , result ,
1500
+ "pathname2url() failed; %s != %s" %
1501
+ (expect , result ))
1502
+ given = '\\ \\ ?\\ unc\\ server\\ share\\ dir'
1503
+ expect = '/server/share/dir'
1504
+ result = urllib .request .pathname2url (given )
1505
+ self .assertEqual (expect , result ,
1506
+ "pathname2url() failed; %s != %s" %
1507
+ (expect , result ))
1508
+
1509
+
1492
1510
@unittest .skipUnless (sys .platform == 'win32' ,
1493
1511
'test specific to the urllib.url2path function.' )
1494
1512
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