[Python-Dev] [Python-checkins] cpython: Close #15387: inspect.getmodulename() now uses a new (original) (raw)

Nick Coghlan ncoghlan at gmail.com
Thu Jul 19 01:31:52 CEST 2012


Because the concepts it is based on are no longer used internally - determining the kind of module is now the province of importlib's finders and loaders.

-- Sent from my phone, thus the relative brevity :) On Jul 19, 2012 2:50 AM, "Jim Jewett" <jimjjewett at gmail.com> wrote:

Why is inspect.getmoduleinfo() deprecated? Is it just to remove circular dependencies?

FWIW, I much prefer an API like: tellmeabout(object) to one like: for testdata in (X, Y, Z): usable = tester(object, testdata) if valid(usable): return possibleresults[testdata] and to me, inspect.getmoduleinfo(path) looks like the first, while checking the various import.machinery.*SUFFIXES looks like the second. -jJ On 7/18/12, nick.coghlan <python-checkins at python.org> wrote: > http://hg.python.org/cpython/rev/af7961e1c362 > changeset: 78161:af7961e1c362 > user: Nick Coghlan <ncoghlan at gmail.com> > date: Wed Jul 18 23:14:57 2012 +1000 > summary: > Close #15387: inspect.getmodulename() now uses a new > importlib.machinery.allsuffixes() API rather than the deprecated > inspect.getmoduleinfo() > > files: > Doc/library/importlib.rst | 13 ++++++++++++- > Doc/library/inspect.rst | 15 ++++++++++++--- > Lib/importlib/machinery.py | 4 ++++ > Lib/inspect.py | 11 +++++++++-- > Misc/NEWS | 3 +++ > 5 files changed, 40 insertions(+), 6 deletions(-) > > > diff --git a/Doc/library/importlib.rst b/Doc/library/importlib.rst > --- a/Doc/library/importlib.rst > +++ b/Doc/library/importlib.rst > @@ -533,12 +533,23 @@ > > .. attribute:: EXTENSIONSUFFIXES > > - 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:: allsuffixes() > + > + 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 > diff --git a/Doc/library/inspect.rst b/Doc/library/inspect.rst > --- a/Doc/library/inspect.rst > +++ b/Doc/library/inspect.rst > @@ -198,9 +198,18 @@ > .. 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.allsuffixes. 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) > diff --git a/Lib/importlib/machinery.py b/Lib/importlib/machinery.py > --- a/Lib/importlib/machinery.py > +++ b/Lib/importlib/machinery.py > @@ -13,3 +13,7 @@ > from .bootstrap import ExtensionFileLoader > > EXTENSIONSUFFIXES = imp.extensionsuffixes() > + > +def allsuffixes(): > + """Returns a list of all recognized module suffixes for this process""" > + return SOURCESUFFIXES + BYTECODESUFFIXES + EXTENSIONSUFFIXES > diff --git a/Lib/inspect.py b/Lib/inspect.py > --- a/Lib/inspect.py > +++ b/Lib/inspect.py > @@ -450,8 +450,15 @@ > > 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.allsuffixes()] > + 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. > diff --git a/Misc/NEWS b/Misc/NEWS > --- a/Misc/NEWS > +++ b/Misc/NEWS > @@ -41,6 +41,9 @@ > Library > ------- > > +- Issue #15397: inspect.getmodulename() is now based directly on importlib > + via a new importlib.machinery.allsuffixes() API. > + > - Issue #14635: telnetlib will use poll() rather than select() when > possible > to avoid failing due to the select() file descriptor limit. > > > -- > Repository URL: http://hg.python.org/cpython >


Python-checkins mailing list Python-checkins at python.org http://mail.python.org/mailman/listinfo/python-checkins -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.python.org/pipermail/python-dev/attachments/20120719/4847022e/attachment-0001.html>



More information about the Python-Dev mailing list