@@ -476,7 +476,7 @@ def test_missing_localfile(self):
476476
477477 def test_file_notexists (self ):
478478 fd , tmp_file = tempfile .mkstemp ()
479- tmp_file_canon_url = 'file:' + urllib .request .pathname2url (tmp_file )
479+ tmp_file_canon_url = urllib .request .pathname2url (tmp_file , add_scheme = True )
480480 parsed = urllib .parse .urlsplit (tmp_file_canon_url )
481481 tmp_fileurl = parsed ._replace (netloc = 'localhost' ).geturl ()
482482 try :
@@ -620,7 +620,7 @@ def tearDown(self):
620620
621621 def constructLocalFileUrl (self , filePath ):
622622 filePath = os .path .abspath (filePath )
623- return "file:" + urllib .request .pathname2url (filePath )
623+ return urllib .request .pathname2url (filePath , add_scheme = True )
624624
625625 def createNewTempFile (self , data = b"" ):
626626 """Creates a new temporary file containing the specified data,
@@ -1436,6 +1436,21 @@ def test_pathname2url(self):
14361436 self .assertEqual (fn (f'{ sep } a{ sep } b.c' ), '///a/b.c' )
14371437 self .assertEqual (fn (f'{ sep } a{ sep } b%#c' ), '///a/b%25%23c' )
14381438
1439+ def test_pathname2url_add_scheme (self ):
1440+ sep = os .path .sep
1441+ subtests = [
1442+ ('' , 'file:' ),
1443+ (sep , 'file:///' ),
1444+ ('a' , 'file:a' ),
1445+ (f'a{ sep } b.c' , 'file:a/b.c' ),
1446+ (f'{ sep } a{ sep } b.c' , 'file:///a/b.c' ),
1447+ (f'{ sep } a{ sep } b%#c' , 'file:///a/b%25%23c' ),
1448+ ]
1449+ for path , expected_url in subtests :
1450+ with self .subTest (path = path ):
1451+ self .assertEqual (
1452+ urllib .request .pathname2url (path , add_scheme = True ), expected_url )
1453+
14391454 @unittest .skipUnless (sys .platform == 'win32' ,
14401455 'test specific to Windows pathnames.' )
14411456 def test_pathname2url_win (self ):
@@ -1503,6 +1518,49 @@ def test_url2pathname(self):
15031518 self .assertEqual (fn ('//localhost/foo/bar' ), f'{ sep } foo{ sep } bar' )
15041519 self .assertEqual (fn ('///foo/bar' ), f'{ sep } foo{ sep } bar' )
15051520 self .assertEqual (fn ('////foo/bar' ), f'{ sep } { sep } foo{ sep } bar' )
1521+ self .assertEqual (fn ('data:blah' ), 'data:blah' )
1522+ self .assertEqual (fn ('data://blah' ), f'data:{ sep } { sep } blah' )
1523+
1524+ def test_url2pathname_require_scheme (self ):
1525+ sep = os .path .sep
1526+ subtests = [
1527+ ('file:' , '' ),
1528+ ('FILE:' , '' ),
1529+ ('FiLe:' , '' ),
1530+ ('file:/' , f'{ sep } ' ),
1531+ ('file:///' , f'{ sep } ' ),
1532+ ('file:////' , f'{ sep } { sep } ' ),
1533+ ('file:foo' , 'foo' ),
1534+ ('file:foo/bar' , f'foo{ sep } bar' ),
1535+ ('file:/foo/bar' , f'{ sep } foo{ sep } bar' ),
1536+ ('file://localhost/foo/bar' , f'{ sep } foo{ sep } bar' ),
1537+ ('file:///foo/bar' , f'{ sep } foo{ sep } bar' ),
1538+ ('file:////foo/bar' , f'{ sep } { sep } foo{ sep } bar' ),
1539+ ('file:data:blah' , 'data:blah' ),
1540+ ('file:data://blah' , f'data:{ sep } { sep } blah' ),
1541+ ]
1542+ for url , expected_path in subtests :
1543+ with self .subTest (url = url ):
1544+ self .assertEqual (
1545+ urllib .request .url2pathname (url , require_scheme = True ),
1546+ expected_path )
1547+
1548+ error_subtests = [
1549+ '' ,
1550+ ':' ,
1551+ 'foo' ,
1552+ 'http:foo' ,
1553+ 'localfile:foo' ,
1554+ 'data:foo' ,
1555+ 'data:file:foo' ,
1556+ 'data:file://foo' ,
1557+ ]
1558+ for url in error_subtests :
1559+ with self .subTest (url = url ):
1560+ self .assertRaises (
1561+ urllib .error .URLError ,
1562+ urllib .request .url2pathname ,
1563+ url , require_scheme = True )
15061564
15071565 @unittest .skipUnless (sys .platform == 'win32' ,
15081566 'test specific to Windows pathnames.' )
0 commit comments