Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Checkins
cpython (merge default -> default): Merge
 

Index | Next | Previous | View Flat


python-checkins at python

May 12, 2012, 2:52 PM


Views: 60
Permalink
cpython (merge default -> default): Merge [In reply to]

http://hg.python.org/cpython/rev/5bd55fb3e091
changeset: 76894:5bd55fb3e091
parent: 76893:964cfda9ccc5
parent: 76891:7bf8ac742d2f
user: Antoine Pitrou <solipsis [at] pitrou>
date: Sat May 12 23:44:59 2012 +0200
summary:
Merge

files:
Doc/library/importlib.rst | 14 ++++++
Lib/importlib/__init__.py | 24 ++++++++++
Lib/importlib/test/test_api.py | 50 +++++++++++++++++++++-
Lib/pyclbr.py | 25 +++++++---
Lib/test/test_unicode.py | 8 ---
Misc/NEWS | 2 +
6 files changed, 105 insertions(+), 18 deletions(-)


diff --git a/Doc/library/importlib.rst b/Doc/library/importlib.rst
--- a/Doc/library/importlib.rst
+++ b/Doc/library/importlib.rst
@@ -86,6 +86,20 @@
that was imported (e.g. ``pkg.mod``), while :func:`__import__` returns the
top-level package or module (e.g. ``pkg``).

+.. function:: find_loader(name, path=None)
+
+ Find the loader for a module, optionally within the specified *path*. If the
+ module is in :attr:`sys.modules`, then ``sys.modules[name].__loader__`` is
+ returned (unless the loader would be ``None``, in which case
+ :exc:`ValueError` is raised). Otherwise a search using :attr:`sys.meta_path`
+ is done. ``None`` is returned if no loader is found.
+
+ A dotted name does not have its parent's implicitly imported. If that is
+ desired (although not nessarily required to find the loader, it will most
+ likely be needed if the loader actually is used to load the module), then
+ you will have to import the packages containing the module prior to calling
+ this function.
+
.. function:: invalidate_caches()

Invalidate the internal caches of the finders stored at
diff --git a/Lib/importlib/__init__.py b/Lib/importlib/__init__.py
--- a/Lib/importlib/__init__.py
+++ b/Lib/importlib/__init__.py
@@ -29,6 +29,30 @@
finder.invalidate_caches()


+def find_loader(name, path=None):
+ """Find the loader for the specified module.
+
+ First, sys.modules is checked to see if the module was already imported. If
+ so, then sys.modules[name].__loader__ is returned. If that happens to be
+ set to None, then ValueError is raised. If the module is not in
+ sys.modules, then sys.meta_path is searched for a suitable loader with the
+ value of 'path' given to the finders. None is returned if no loader could
+ be found.
+
+ Dotted names do not have their parent packages implicitly imported.
+
+ """
+ try:
+ loader = sys.modules[name].__loader__
+ if loader is None:
+ raise ValueError('{}.__loader__ is None'.format(name))
+ else:
+ return loader
+ except KeyError:
+ pass
+ return _bootstrap._find_module(name, path)
+
+
def import_module(name, package=None):
"""Import a module.

diff --git a/Lib/importlib/test/test_api.py b/Lib/importlib/test/test_api.py
--- a/Lib/importlib/test/test_api.py
+++ b/Lib/importlib/test/test_api.py
@@ -85,6 +85,54 @@
self.assertEqual(b_load_count, 1)


+class FindLoaderTests(unittest.TestCase):
+
+ class FakeMetaFinder:
+ @staticmethod
+ def find_module(name, path=None): return name, path
+
+ def test_sys_modules(self):
+ # If a module with __loader__ is in sys.modules, then return it.
+ name = 'some_mod'
+ with util.uncache(name):
+ module = imp.new_module(name)
+ loader = 'a loader!'
+ module.__loader__ = loader
+ sys.modules[name] = module
+ found = importlib.find_loader(name)
+ self.assertEqual(loader, found)
+
+ def test_sys_modules_loader_is_None(self):
+ # If sys.modules[name].__loader__ is None, raise ValueError.
+ name = 'some_mod'
+ with util.uncache(name):
+ module = imp.new_module(name)
+ module.__loader__ = None
+ sys.modules[name] = module
+ with self.assertRaises(ValueError):
+ importlib.find_loader(name)
+
+ def test_success(self):
+ # Return the loader found on sys.meta_path.
+ name = 'some_mod'
+ with util.uncache(name):
+ with util.import_state(meta_path=[self.FakeMetaFinder]):
+ self.assertEqual((name, None), importlib.find_loader(name))
+
+ def test_success_path(self):
+ # Searching on a path should work.
+ name = 'some_mod'
+ path = 'path to some place'
+ with util.uncache(name):
+ with util.import_state(meta_path=[self.FakeMetaFinder]):
+ self.assertEqual((name, path),
+ importlib.find_loader(name, path))
+
+ def test_nothing(self):
+ # None is returned upon failure to find a loader.
+ self.assertIsNone(importlib.find_loader('nevergoingtofindthismodule'))
+
+
class InvalidateCacheTests(unittest.TestCase):

def test_method_called(self):
@@ -114,7 +162,7 @@

def test_main():
from test.support import run_unittest
- run_unittest(ImportModuleTests)
+ run_unittest(ImportModuleTests, FindLoaderTests, InvalidateCacheTests)


if __name__ == '__main__':
diff --git a/Lib/pyclbr.py b/Lib/pyclbr.py
--- a/Lib/pyclbr.py
+++ b/Lib/pyclbr.py
@@ -39,8 +39,10 @@
lineno -- the line in the file on which the class statement occurred
"""

+import io
+import os
import sys
-import imp
+import importlib
import tokenize
from token import NAME, DEDENT, OP
from operator import itemgetter
@@ -133,19 +135,24 @@
# Search the path for the module
f = None
if inpackage is not None:
- f, fname, (_s, _m, ty) = imp.find_module(module, path)
+ search_path = path
else:
- f, fname, (_s, _m, ty) = imp.find_module(module, path + sys.path)
- if ty == imp.PKG_DIRECTORY:
- dict['__path__'] = [fname]
- path = [fname] + path
- f, fname, (_s, _m, ty) = imp.find_module('__init__', [fname])
+ search_path = path + sys.path
+ loader = importlib.find_loader(fullmodule, search_path)
+ fname = loader.get_filename(fullmodule)
_modules[fullmodule] = dict
- if ty != imp.PY_SOURCE:
+ if loader.is_package(fullmodule):
+ dict['__path__'] = [os.path.dirname(fname)]
+ try:
+ source = loader.get_source(fullmodule)
+ if source is None:
+ return dict
+ except (AttributeError, ImportError):
# not Python source, can't do anything with this module
- f.close()
return dict

+ f = io.StringIO(source)
+
stack = [] # stack of (class, indent) pairs

g = tokenize.generate_tokens(f.readline)
diff --git a/Lib/test/test_unicode.py b/Lib/test/test_unicode.py
--- a/Lib/test/test_unicode.py
+++ b/Lib/test/test_unicode.py
@@ -1422,14 +1422,6 @@
self.assertRaises(TypeError, str, b"hello", "test.unicode2")
self.assertRaises(TypeError, "hello".encode, "test.unicode1")
self.assertRaises(TypeError, "hello".encode, "test.unicode2")
- # executes PyUnicode_Encode()
- import imp
- self.assertRaises(
- ImportError,
- imp.find_module,
- "non-existing module",
- ["non-existing dir"]
- )

# Error handling (wrong arguments)
self.assertRaises(TypeError, "hello".encode, 42, 42, 42)
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -23,6 +23,8 @@
Library
-------

+- Issue #13959: Introduce importlib.find_loader().
+
- Issue #14082: shutil.copy2() now copies extended attributes, if possible.
Patch by Hynek Schlawack.


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

Subject User Time
cpython (merge default -> default): Merge python-checkins at python Jan 3, 2012, 1:51 PM
    cpython (merge default -> default): Merge python-checkins at python Jan 13, 2012, 8:48 PM
        cpython (merge default -> default): Merge python-checkins at python Jan 17, 2012, 5:08 PM
            cpython (merge default -> default): Merge python-checkins at python Jan 31, 2012, 11:58 AM
        cpython (merge default -> default): Merge python-checkins at python Jan 26, 2012, 5:58 AM
            cpython (merge default -> default): Merge python-checkins at python Feb 3, 2012, 9:08 AM
                cpython (merge default -> default): Merge python-checkins at python Feb 15, 2012, 1:34 PM
                    cpython (merge default -> default): Merge python-checkins at python Feb 23, 2012, 7:18 AM
                        cpython (merge default -> default): Merge python-checkins at python Mar 6, 2012, 4:50 AM
                    cpython (merge default -> default): merge python-checkins at python Feb 23, 2012, 3:30 PM
                    cpython (merge default -> default): merge python-checkins at python Feb 23, 2012, 4:35 PM
                    cpython (merge default -> default): merge python-checkins at python Feb 23, 2012, 5:48 PM
                    cpython (merge default -> default): Merge python-checkins at python Feb 26, 2012, 4:05 PM
                cpython (merge default -> default): Merge python-checkins at python Feb 16, 2012, 3:03 PM
            cpython (merge default -> default): Merge python-checkins at python Feb 7, 2012, 6:20 AM
                cpython (merge default -> default): Merge python-checkins at python Feb 19, 2012, 4:55 PM
                    cpython (merge default -> default): Merge python-checkins at python Mar 4, 2012, 12:04 PM
                        cpython (merge default -> default): Merge python-checkins at python Mar 12, 2012, 1:54 PM
                            cpython (merge default -> default): merge python-checkins at python Mar 23, 2012, 5:30 AM
                                cpython (merge default -> default): merge python-checkins at python Apr 2, 2012, 5:34 PM
                                    cpython (merge default -> default): Merge python-checkins at python Apr 12, 2012, 6:13 PM
                                        cpython (merge default -> default): merge python-checkins at python Apr 20, 2012, 12:23 PM
                                        cpython (merge default -> default): Merge python-checkins at python Apr 20, 2012, 2:19 PM
                                            cpython (merge default -> default): merge python-checkins at python May 4, 2012, 1:06 PM
                                                cpython (merge default -> default): Merge python-checkins at python May 17, 2012, 12:07 PM
                                            cpython (merge default -> default): Merge python-checkins at python May 4, 2012, 1:18 PM
                                            cpython (merge default -> default): Merge python-checkins at python May 4, 2012, 2:18 PM
                                        cpython (merge default -> default): merge python-checkins at python Apr 21, 2012, 3:53 PM
                                        cpython (merge default -> default): merge python-checkins at python Apr 21, 2012, 6:22 PM
                                        cpython (merge default -> default): merge python-checkins at python Apr 21, 2012, 11:06 PM
                                        cpython (merge default -> default): merge python-checkins at python Apr 22, 2012, 10:02 AM
                                        cpython (merge default -> default): merge python-checkins at python Apr 22, 2012, 10:30 AM
                                        cpython (merge default -> default): Merge python-checkins at python Apr 25, 2012, 5:19 PM
                                    cpython (merge default -> default): merge python-checkins at python Apr 15, 2012, 12:25 PM
                                        cpython (merge default -> default): merge python-checkins at python Apr 27, 2012, 11:02 AM
                                        cpython (merge default -> default): merge python-checkins at python Apr 27, 2012, 12:31 PM
                                        cpython (merge default -> default): merge python-checkins at python Apr 27, 2012, 2:33 PM
                                        cpython (merge default -> default): merge python-checkins at python Apr 29, 2012, 9:50 AM
                                            cpython (merge default -> default): Merge python-checkins at python May 12, 2012, 10:05 AM
                                            cpython (merge default -> default): Merge python-checkins at python May 12, 2012, 2:52 PM
                                            cpython (merge default -> default): Merge python-checkins at python May 12, 2012, 2:52 PM
                                        cpython (merge default -> default): merge python-checkins at python Apr 29, 2012, 11:40 AM
                                    cpython (merge default -> default): merge python-checkins at python Apr 15, 2012, 4:06 PM
                                        cpython (merge default -> default): merge python-checkins at python Apr 29, 2012, 1:42 PM
                                        cpython (merge default -> default): merge python-checkins at python Apr 29, 2012, 2:57 PM
                                    cpython (merge default -> default): merge python-checkins at python Apr 15, 2012, 7:29 PM
                            cpython (merge default -> default): Merge python-checkins at python Mar 25, 2012, 10:16 AM
                                cpython (merge default -> default): merge python-checkins at python Apr 6, 2012, 9:55 AM
                                    cpython (merge default -> default): Merge python-checkins at python Apr 17, 2012, 6:42 PM
                                        cpython (merge default -> default): merge python-checkins at python Apr 29, 2012, 8:25 PM
                        cpython (merge default -> default): Merge python-checkins at python Mar 12, 2012, 3:25 PM
                        cpython (merge default -> default): Merge python-checkins at python Mar 14, 2012, 12:59 PM
                        cpython (merge default -> default): merge python-checkins at python Mar 14, 2012, 6:17 PM
                        cpython (merge default -> default): merge python-checkins at python Mar 16, 2012, 1:19 AM
                        cpython (merge default -> default): Merge python-checkins at python Mar 16, 2012, 4:28 PM
        cpython (merge default -> default): Merge python-checkins at python Jan 26, 2012, 3:37 PM
        cpython (merge default -> default): Merge python-checkins at python Jan 26, 2012, 4:09 PM

  Index | Next | Previous | View Flat
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.