importlib.test.source.test_abc_loader was making a bad assumption that all file

paths used '/' as a path separator.

Fixes issue #5646.
This commit is contained in:
Brett Cannon 2009-04-02 17:54:43 +00:00
parent f86213f6f2
commit 5561982b64
1 changed files with 21 additions and 16 deletions

View File

@ -129,7 +129,7 @@ class PyLoaderTests(testing_abc.LoaderTests):
def test_module(self): def test_module(self):
name = '<module>' name = '<module>'
path = 'path/to/module' path = os.path.join('', 'path', 'to', 'module')
mock = self.mocker({name: path}) mock = self.mocker({name: path})
with util.uncache(name): with util.uncache(name):
module = mock.load_module(name) module = mock.load_module(name)
@ -141,7 +141,7 @@ class PyLoaderTests(testing_abc.LoaderTests):
def test_package(self): def test_package(self):
name = '<pkg>' name = '<pkg>'
path = '/path/to/{}/__init__'.format(name) path = os.path.join('path', 'to', name, '__init__')
mock = self.mocker({name: path}) mock = self.mocker({name: path})
with util.uncache(name): with util.uncache(name):
module = mock.load_module(name) module = mock.load_module(name)
@ -153,7 +153,7 @@ class PyLoaderTests(testing_abc.LoaderTests):
def test_lacking_parent(self): def test_lacking_parent(self):
name = 'pkg.mod' name = 'pkg.mod'
path = 'path/to/pkg/mod' path = os.path.join('path', 'to', 'pkg', 'mod')
mock = self.mocker({name: path}) mock = self.mocker({name: path})
with util.uncache(name): with util.uncache(name):
module = mock.load_module(name) module = mock.load_module(name)
@ -165,7 +165,7 @@ class PyLoaderTests(testing_abc.LoaderTests):
def test_module_reuse(self): def test_module_reuse(self):
name = 'mod' name = 'mod'
path = 'path/to/mod' path = os.path.join('path', 'to', 'mod')
module = imp.new_module(name) module = imp.new_module(name)
mock = self.mocker({name: path}) mock = self.mocker({name: path})
with util.uncache(name): with util.uncache(name):
@ -179,7 +179,7 @@ class PyLoaderTests(testing_abc.LoaderTests):
name = "mod" name = "mod"
module = imp.new_module(name) module = imp.new_module(name)
module.blah = None module.blah = None
mock = self.mocker({name: 'path/to/mod'}) mock = self.mocker({name: os.path.join('path', 'to', 'mod')})
mock.source = b"1/0" mock.source = b"1/0"
with util.uncache(name): with util.uncache(name):
sys.modules[name] = module sys.modules[name] = module
@ -190,7 +190,7 @@ class PyLoaderTests(testing_abc.LoaderTests):
def test_unloadable(self): def test_unloadable(self):
name = "mod" name = "mod"
mock = self.mocker({name: 'path/to/mod'}) mock = self.mocker({name: os.path.join('path', 'to', 'mod')})
mock.source = b"1/0" mock.source = b"1/0"
with util.uncache(name): with util.uncache(name):
self.assertRaises(ZeroDivisionError, mock.load_module, name) self.assertRaises(ZeroDivisionError, mock.load_module, name)
@ -224,7 +224,7 @@ class PyLoaderGetSourceTests(unittest.TestCase):
def test_default_encoding(self): def test_default_encoding(self):
# Should have no problems with UTF-8 text. # Should have no problems with UTF-8 text.
name = 'mod' name = 'mod'
mock = PyLoaderMock({name: 'path/to/mod'}) mock = PyLoaderMock({name: os.path.join('path', 'to', 'mod')})
source = 'x = "ü"' source = 'x = "ü"'
mock.source = source.encode('utf-8') mock.source = source.encode('utf-8')
returned_source = mock.get_source(name) returned_source = mock.get_source(name)
@ -233,7 +233,7 @@ class PyLoaderGetSourceTests(unittest.TestCase):
def test_decoded_source(self): def test_decoded_source(self):
# Decoding should work. # Decoding should work.
name = 'mod' name = 'mod'
mock = PyLoaderMock({name: 'path/to/mod'}) mock = PyLoaderMock({name: os.path.join('path', 'to', 'mod')})
source = "# coding: Latin-1\nx='ü'" source = "# coding: Latin-1\nx='ü'"
assert source.encode('latin-1') != source.encode('utf-8') assert source.encode('latin-1') != source.encode('utf-8')
mock.source = source.encode('latin-1') mock.source = source.encode('latin-1')
@ -289,7 +289,7 @@ class SkipWritingBytecodeTests(unittest.TestCase):
@source_util.writes_bytecode @source_util.writes_bytecode
def run_test(self, dont_write_bytecode): def run_test(self, dont_write_bytecode):
name = 'mod' name = 'mod'
mock = PyPycLoaderMock({name: 'path/to/mod'}) mock = PyPycLoaderMock({name: os.path.join('path', 'to', 'mod')})
sys.dont_write_bytecode = dont_write_bytecode sys.dont_write_bytecode = dont_write_bytecode
with util.uncache(name): with util.uncache(name):
mock.load_module(name) mock.load_module(name)
@ -313,8 +313,9 @@ class RegeneratedBytecodeTests(unittest.TestCase):
name = 'mod' name = 'mod'
bad_magic = b'\x00\x00\x00\x00' bad_magic = b'\x00\x00\x00\x00'
assert bad_magic != imp.get_magic() assert bad_magic != imp.get_magic()
mock = PyPycLoaderMock({name: 'path/to/mod'}, mock = PyPycLoaderMock({name: os.path.join('path', 'to', 'mod')},
{name: {'path': 'path/to/mod.bytecode', {name: {'path': os.path.join('path', 'to',
'mod.bytecode'),
'magic': bad_magic}}) 'magic': bad_magic}})
with util.uncache(name): with util.uncache(name):
mock.load_module(name) mock.load_module(name)
@ -327,7 +328,7 @@ class RegeneratedBytecodeTests(unittest.TestCase):
# Bytecode with an older mtime should be regenerated. # Bytecode with an older mtime should be regenerated.
name = 'mod' name = 'mod'
old_mtime = PyPycLoaderMock.default_mtime - 1 old_mtime = PyPycLoaderMock.default_mtime - 1
mock = PyPycLoaderMock({name: 'path/to/mod'}, mock = PyPycLoaderMock({name: os.path.join('path', 'to', 'mod')},
{name: {'path': 'path/to/mod.bytecode', 'mtime': old_mtime}}) {name: {'path': 'path/to/mod.bytecode', 'mtime': old_mtime}})
with util.uncache(name): with util.uncache(name):
mock.load_module(name) mock.load_module(name)
@ -345,7 +346,8 @@ class BadBytecodeFailureTests(unittest.TestCase):
# A bad magic number should lead to an ImportError. # A bad magic number should lead to an ImportError.
name = 'mod' name = 'mod'
bad_magic = b'\x00\x00\x00\x00' bad_magic = b'\x00\x00\x00\x00'
mock = PyPycLoaderMock({}, {name: {'path': 'path/to/mod', mock = PyPycLoaderMock({}, {name: {'path': os.path.join('path', 'to',
'mod'),
'magic': bad_magic}}) 'magic': bad_magic}})
with util.uncache(name): with util.uncache(name):
self.assertRaises(ImportError, mock.load_module, name) self.assertRaises(ImportError, mock.load_module, name)
@ -353,7 +355,9 @@ class BadBytecodeFailureTests(unittest.TestCase):
def test_bad_bytecode(self): def test_bad_bytecode(self):
# Bad code object bytecode should elad to an ImportError. # Bad code object bytecode should elad to an ImportError.
name = 'mod' name = 'mod'
mock = PyPycLoaderMock({}, {name: {'path': '/path/to/mod', 'bc': b''}}) mock = PyPycLoaderMock({}, {name: {'path': os.path.join('path', 'to',
'mod'),
'bc': b''}})
with util.uncache(name): with util.uncache(name):
self.assertRaises(ImportError, mock.load_module, name) self.assertRaises(ImportError, mock.load_module, name)
@ -389,14 +393,15 @@ class MissingPathsTests(unittest.TestCase):
def test_source_path_ImportError(self): def test_source_path_ImportError(self):
# An ImportError from source_path should trigger an ImportError. # An ImportError from source_path should trigger an ImportError.
name = 'mod' name = 'mod'
mock = PyPycLoaderMock({}, {name: {'path': 'path/to/mod'}}) mock = PyPycLoaderMock({}, {name: {'path': os.path.join('path', 'to',
'mod')}})
with util.uncache(name): with util.uncache(name):
self.assertRaises(ImportError, mock.load_module, name) self.assertRaises(ImportError, mock.load_module, name)
def test_bytecode_path_ImportError(self): def test_bytecode_path_ImportError(self):
# An ImportError from bytecode_path should trigger an ImportError. # An ImportError from bytecode_path should trigger an ImportError.
name = 'mod' name = 'mod'
mock = PyPycLoaderMock({name: 'path/to/mod'}) mock = PyPycLoaderMock({name: os.path.join('path', 'to', 'mod')})
bad_meth = types.MethodType(raise_ImportError, mock) bad_meth = types.MethodType(raise_ImportError, mock)
mock.bytecode_path = bad_meth mock.bytecode_path = bad_meth
with util.uncache(name): with util.uncache(name):