]> granicus.if.org Git - python/commitdiff
Close #15387: inspect.getmodulename() now uses a new importlib.machinery.all_suffixes...
authorNick Coghlan <ncoghlan@gmail.com>
Wed, 18 Jul 2012 13:14:57 +0000 (23:14 +1000)
committerNick Coghlan <ncoghlan@gmail.com>
Wed, 18 Jul 2012 13:14:57 +0000 (23:14 +1000)
Doc/library/importlib.rst
Doc/library/inspect.rst
Lib/importlib/machinery.py
Lib/inspect.py
Misc/NEWS

index cea2da048e8b79172bcc85957580d9cde42496b7..daf9e569dd2ec49172399d349aa1b5bace6e142a 100644 (file)
@@ -533,12 +533,23 @@ find and load modules.
 
 .. attribute:: EXTENSION_SUFFIXES
 
-   A list of strings representing the the recognized file suffixes for
+   A list of strings representing the recognized file suffixes for
    extension modules.
 
    .. versionadded:: 3.3
 
 
+.. func:: all_suffixes()
+
+   Returns a combined list of strings representing all file suffixes for
+   Python modules recognized by the standard import machinery. This is a
+   helper for code which simply needs to know if a filesystem path
+   potentially represents a Python module (for example,
+   :func:`inspect.getmodulename`)
+
+   .. versionadded:: 3.3
+
+
 .. class:: BuiltinImporter
 
     An :term:`importer` for built-in modules. All known built-in modules are
index 6568e94cc3fa00c3f8d2333ae3da2d9cf1cf6f27..ac0d6e655526c7e75a249f3295e1955b7521205c 100644 (file)
@@ -198,9 +198,18 @@ attributes:
 .. function:: getmodulename(path)
 
    Return the name of the module named by the file *path*, without including the
-   names of enclosing packages.  This uses the same algorithm as the interpreter
-   uses when searching for modules.  If the name cannot be matched according to the
-   interpreter's rules, ``None`` is returned.
+   names of enclosing packages. The file extension is checked against all of
+   the entries in :func:`importlib.machinery.all_suffixes`. If it matches,
+   the final path component is returned with the extension removed.
+   Otherwise, ``None`` is returned.
+
+   Note that this function *only* returns a meaningful name for actual
+   Python modules - paths that potentially refer to Python packages will
+   still return ``None``.
+
+   .. versionchanged:: 3.3
+      This function is now based directly on :mod:`importlib` rather than the
+      deprecated :func:`getmoduleinfo`.
 
 
 .. function:: ismodule(object)
index d5e7250479c5c55649c8784da51d8416ce69eb1c..3fe0b11017578f38824ad9e332788419118822ad 100644 (file)
@@ -13,3 +13,7 @@ from ._bootstrap import SourcelessFileLoader
 from ._bootstrap import ExtensionFileLoader
 
 EXTENSION_SUFFIXES = _imp.extension_suffixes()
+
+def all_suffixes():
+    """Returns a list of all recognized module suffixes for this process"""
+    return SOURCE_SUFFIXES + BYTECODE_SUFFIXES + EXTENSION_SUFFIXES
index 074e1b44306d947c192ecb60c9b7db5d16c37fcc..40e44548f2386cea4016262ab958153942843b47 100644 (file)
@@ -450,8 +450,15 @@ def getmoduleinfo(path):
 
 def getmodulename(path):
     """Return the module name for a given file, or None."""
-    info = getmoduleinfo(path)
-    if info: return info[0]
+    fname = os.path.basename(path)
+    # Check for paths that look like an actual module file
+    suffixes = [(-len(suffix), suffix)
+                    for suffix in importlib.machinery.all_suffixes()]
+    suffixes.sort() # try longest suffixes first, in case they overlap
+    for neglen, suffix in suffixes:
+        if fname.endswith(suffix):
+            return fname[:neglen]
+    return None
 
 def getsourcefile(object):
     """Return the filename that can be used to locate an object's source.
index 11732dc00ec11cd5f8a4cf83a445ff1018cd3a7b..1f11b4a66ec9a06474715c1e3eeae07ea927333c 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -41,6 +41,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #15397: inspect.getmodulename() is now based directly on importlib
+  via a new importlib.machinery.all_suffixes() API.
+
 - Issue #14635: telnetlib will use poll() rather than select() when possible
   to avoid failing due to the select() file descriptor limit.