[Python-checkins] cpython: Issue #19927: Add __eq__ to path-based loaders in importlib.

eric.snow python-checkins at python.org
Sat Jan 4 23:13:51 CET 2014


http://hg.python.org/cpython/rev/a72a0e4dad20
changeset:   88299:a72a0e4dad20
user:        Eric Snow <ericsnowcurrently at gmail.com>
date:        Sat Jan 04 15:06:49 2014 -0700
summary:
  Issue #19927: Add __eq__ to path-based loaders in importlib.

files:
  Lib/importlib/_bootstrap.py                        |    14 +
  Lib/test/test_importlib/extension/test_loader.py   |     9 +
  Lib/test/test_importlib/source/test_file_loader.py |    13 +
  Lib/test/test_importlib/test_api.py                |     3 +-
  Misc/NEWS                                          |     2 +
  Python/importlib.h                                 |  1278 +++++----
  6 files changed, 698 insertions(+), 621 deletions(-)


diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py
--- a/Lib/importlib/_bootstrap.py
+++ b/Lib/importlib/_bootstrap.py
@@ -1559,6 +1559,13 @@
         self.name = fullname
         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
     def load_module(self, fullname):
         """Load a module from a file."""
@@ -1653,6 +1660,13 @@
         self.name = name
         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
     def load_module(self, fullname):
         """Load an extension module."""
diff --git a/Lib/test/test_importlib/extension/test_loader.py b/Lib/test/test_importlib/extension/test_loader.py
--- a/Lib/test/test_importlib/extension/test_loader.py
+++ b/Lib/test/test_importlib/extension/test_loader.py
@@ -28,6 +28,15 @@
         with self.assertRaises(ImportError):
             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):
         with util.uncache(ext_util.NAME):
diff --git a/Lib/test/test_importlib/source/test_file_loader.py b/Lib/test/test_importlib/source/test_file_loader.py
--- a/Lib/test/test_importlib/source/test_file_loader.py
+++ b/Lib/test/test_importlib/source/test_file_loader.py
@@ -27,6 +27,11 @@
 
     """
 
+    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):
         class Tester(self.abc.FileLoader):
             def get_source(self, _): return 'attr = 42'
@@ -53,6 +58,14 @@
         with self.assertRaises(ImportError):
             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]
     def test_module(self):
         with source_util.create_modules('_temp') as mapping:
diff --git a/Lib/test/test_importlib/test_api.py b/Lib/test/test_importlib/test_api.py
--- a/Lib/test/test_importlib/test_api.py
+++ b/Lib/test/test_importlib/test_api.py
@@ -288,8 +288,7 @@
             self.assertNotIn(name, sorted(sys.modules))
             # Ensure successive calls behave the same.
             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):
         name = 'spam'
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -203,6 +203,8 @@
   no exception detail exists (no colon following the exception's name, or
   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
   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.
diff --git a/Python/importlib.h b/Python/importlib.h
--- a/Python/importlib.h
+++ b/Python/importlib.h
[stripped]

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list