Issue #15294: Fix a regression in pkgutil.extend_path()'s handling of nested namespace packages.
diff --git a/Lib/pkgutil.py b/Lib/pkgutil.py
index c5b0c4d..59c99bf 100644
--- a/Lib/pkgutil.py
+++ b/Lib/pkgutil.py
@@ -513,12 +513,22 @@
         # frozen package.  Return the path unchanged in that case.
         return path
 
-    pname = os.path.join(*name.split('.')) # Reconstitute as relative path
     sname_pkg = name + ".pkg"
 
     path = path[:] # Start with a copy of the existing path
 
-    for dir in sys.path:
+    parent_package, _, final_name = name.rpartition('.')
+    if parent_package:
+        try:
+            search_path = sys.modules[parent_package].__path__
+        except (KeyError, AttributeError):
+            # We can't do anything: find_loader() returns None when
+            # passed a dotted name.
+            return path
+    else:
+        search_path = sys.path
+
+    for dir in search_path:
         if not isinstance(dir, str):
             continue
 
@@ -526,7 +536,7 @@
         if finder is not None:
             # Is this finder PEP 420 compliant?
             if hasattr(finder, 'find_loader'):
-                loader, portions = finder.find_loader(name)
+                loader, portions = finder.find_loader(final_name)
             else:
                 # No, no need to call it
                 loader = None