Close #15387: inspect.getmodulename() now uses a new importlib.machinery.all_suffixes() API rather than the deprecated inspect.getmoduleinfo()
diff --git a/Lib/importlib/machinery.py b/Lib/importlib/machinery.py
index d5e7250..3fe0b11 100644
--- a/Lib/importlib/machinery.py
+++ b/Lib/importlib/machinery.py
@@ -13,3 +13,7 @@
 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
diff --git a/Lib/inspect.py b/Lib/inspect.py
index 074e1b4..40e4454 100644
--- 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.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.