Issue #19927: Add __eq__ to path-based loaders in importlib.

This commit is contained in:
Eric Snow 2014-01-04 15:06:49 -07:00
parent 78194cd4e9
commit d749c7ae68
6 changed files with 698 additions and 621 deletions

View File

@ -1559,6 +1559,13 @@ class FileLoader:
self.name = fullname self.name = fullname
self.path = path self.path = path
def __eq__(self, other):
return (self.__class__ == other.__class__ and
self.__dict__ == other.__dict__)
def __hash__(self):
return hash(self.name) ^ hash(self.path)
@_check_name @_check_name
def load_module(self, fullname): def load_module(self, fullname):
"""Load a module from a file.""" """Load a module from a file."""
@ -1653,6 +1660,13 @@ class ExtensionFileLoader:
self.name = name self.name = name
self.path = path self.path = path
def __eq__(self, other):
return (self.__class__ == other.__class__ and
self.__dict__ == other.__dict__)
def __hash__(self):
return hash(self.name) ^ hash(self.path)
@_check_name @_check_name
def load_module(self, fullname): def load_module(self, fullname):
"""Load an extension module.""" """Load an extension module."""

View File

@ -28,6 +28,15 @@ class LoaderTests(abc.LoaderTests):
with self.assertRaises(ImportError): with self.assertRaises(ImportError):
self.load_module('XXX') self.load_module('XXX')
def test_equality(self):
other = self.machinery.ExtensionFileLoader(ext_util.NAME,
ext_util.FILEPATH)
self.assertEqual(self.loader, other)
def test_inequality(self):
other = self.machinery.ExtensionFileLoader('_' + ext_util.NAME,
ext_util.FILEPATH)
self.assertNotEqual(self.loader, other)
def test_module(self): def test_module(self):
with util.uncache(ext_util.NAME): with util.uncache(ext_util.NAME):

View File

@ -27,6 +27,11 @@ class SimpleTest(abc.LoaderTests):
""" """
def setUp(self):
self.name = 'spam'
self.filepath = os.path.join('ham', self.name + '.py')
self.loader = self.machinery.SourceFileLoader(self.name, self.filepath)
def test_load_module_API(self): def test_load_module_API(self):
class Tester(self.abc.FileLoader): class Tester(self.abc.FileLoader):
def get_source(self, _): return 'attr = 42' def get_source(self, _): return 'attr = 42'
@ -53,6 +58,14 @@ class SimpleTest(abc.LoaderTests):
with self.assertRaises(ImportError): with self.assertRaises(ImportError):
loader.get_filename(name + 'XXX') loader.get_filename(name + 'XXX')
def test_equality(self):
other = self.machinery.SourceFileLoader(self.name, self.filepath)
self.assertEqual(self.loader, other)
def test_inequality(self):
other = self.machinery.SourceFileLoader('_' + self.name, self.filepath)
self.assertNotEqual(self.loader, other)
# [basic] # [basic]
def test_module(self): def test_module(self):
with source_util.create_modules('_temp') as mapping: with source_util.create_modules('_temp') as mapping:

View File

@ -288,8 +288,7 @@ class FindSpecTests:
self.assertNotIn(name, sorted(sys.modules)) self.assertNotIn(name, sorted(sys.modules))
# Ensure successive calls behave the same. # Ensure successive calls behave the same.
spec_again = self.init.find_spec(fullname, [pkg_dir]) spec_again = self.init.find_spec(fullname, [pkg_dir])
# XXX Once #19927 is resolved, uncomment this line. self.assertEqual(spec_again, spec)
#self.assertEqual(spec_again, spec)
def test_find_submodule_missing_path(self): def test_find_submodule_missing_path(self):
name = 'spam' name = 'spam'

View File

@ -203,6 +203,8 @@ Library
no exception detail exists (no colon following the exception's name, or no exception detail exists (no colon following the exception's name, or
a colon does follow but no text follows the colon). a colon does follow but no text follows the colon).
- Issue #19927: Add __eq__ to path-based loaders in importlib.
- Issue #19827: On UNIX, setblocking() and settimeout() methods of - Issue #19827: On UNIX, setblocking() and settimeout() methods of
socket.socket can now avoid a second syscall if the ioctl() function can be socket.socket can now avoid a second syscall if the ioctl() function can be
used, or if the non-blocking flag of the socket is unchanged. used, or if the non-blocking flag of the socket is unchanged.

File diff suppressed because it is too large Load Diff