summaryrefslogtreecommitdiffstats
path: root/Lib/inspect.py
diff options
context:
space:
mode:
authorBrett Cannon <brett@python.org>2012-05-11 16:58:42 (GMT)
committerBrett Cannon <brett@python.org>2012-05-11 16:58:42 (GMT)
commitcb66eb0deca1d5cd232f97c76a215ecaab958d30 (patch)
treef38497a2e61cf8be225c4e7394961703cea5f66c /Lib/inspect.py
parent810c64df8f8bf70a2cb7a626004185616cb88213 (diff)
downloadcpython-cb66eb0deca1d5cd232f97c76a215ecaab958d30.zip
cpython-cb66eb0deca1d5cd232f97c76a215ecaab958d30.tar.gz
cpython-cb66eb0deca1d5cd232f97c76a215ecaab958d30.tar.bz2
Issue #13959: Deprecate imp.get_suffixes() for new attributes on
importlib.machinery that provide the suffix details for import. The attributes were not put on imp so as to compartmentalize everything importlib needs for setting up imports in importlib.machinery. This also led to an indirect deprecation of inspect.getmoduleinfo() as it directly returned imp.get_suffix's returned tuple which no longer makes sense.
Diffstat (limited to 'Lib/inspect.py')
-rw-r--r--Lib/inspect.py27
1 files changed, 16 insertions, 11 deletions
diff --git a/Lib/inspect.py b/Lib/inspect.py
index fc9f612..56d05fb 100644
--- a/Lib/inspect.py
+++ b/Lib/inspect.py
@@ -29,14 +29,15 @@ Here are some of the useful functions provided by this module:
__author__ = 'Ka-Ping Yee <ping@lfw.org>'
__date__ = '1 Jan 2001'
-import sys
-import os
-import types
+import imp
+import importlib.machinery
import itertools
+import linecache
+import os
import re
-import imp
+import sys
import tokenize
-import linecache
+import types
from operator import attrgetter
from collections import namedtuple
@@ -432,6 +433,8 @@ ModuleInfo = namedtuple('ModuleInfo', 'name suffix mode module_type')
def getmoduleinfo(path):
"""Get the module name, suffix, mode, and module type for a given file."""
+ warnings.warn('inspect.getmoduleinfo() is deprecated', DeprecationWarning,
+ 2)
filename = os.path.basename(path)
suffixes = [(-len(suffix), suffix, mode, mtype)
for suffix, mode, mtype in imp.get_suffixes()]
@@ -450,12 +453,14 @@ def getsourcefile(object):
Return None if no way can be identified to get the source.
"""
filename = getfile(object)
- if filename[-4:].lower() in ('.pyc', '.pyo'):
- filename = filename[:-4] + '.py'
- for suffix, mode, kind in imp.get_suffixes():
- if 'b' in mode and filename[-len(suffix):].lower() == suffix:
- # Looks like a binary file. We want to only return a text file.
- return None
+ all_bytecode_suffixes = importlib.machinery.DEBUG_BYTECODE_SUFFIXES[:]
+ all_bytecode_suffixes += importlib.machinery.OPTIMIZED_BYTECODE_SUFFIXES[:]
+ if any(filename.endswith(s) for s in all_bytecode_suffixes):
+ filename = (os.path.splitext(filename)[0] +
+ importlib.machinery.SOURCE_SUFFIXES[0])
+ elif any(filename.endswith(s) for s in
+ importlib.machinery.EXTENSION_SUFFIXES):
+ return None
if os.path.exists(filename):
return filename
# only return a non-existent filename if the module has a PEP 302 loader