instance_path for namespace packages uses path closest to submodule
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Evgeny Prigorodov · committed by David Lord · parent fb89745 · Browse files · 3 files·+47 added−11 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+47 added−11 removed
- tests:+19 added−0 removed
- other:+28 added−11 removed
Provenance
- Commit
88bcf78- Tree
f9f4b63- Author
Evgeny Prigorodov <eprigorodov@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
fb89745
3 files changed+47 added−11 removed
modified:src/flask/scaffold.py+26 added-11 removed
778 unmodified lines779 f" import hooks."780 )781 782 783-def _find_package_path(root_mod_name):783+def _find_package_path(import_name):784 """Find the path that contains the package or module."""785+ root_mod_name, _, _ = import_name.partition(".")786+787 try:786- spec = importlib.util.find_spec(root_mod_name)788+ root_spec = importlib.util.find_spec(root_mod_name)789 788- if spec is None:790+ if root_spec is None:791 raise ValueError("not found")792 # ImportError: the machinery told us it does not exist793 # ValueError:794 # - the module name was invalid795 # - the module name is __main__794- # - *we* raised `ValueError` due to `spec` being `None`796+ # - *we* raised `ValueError` due to `root_spec` being `None`797 except (ImportError, ValueError):798 pass # handled below799 else:800 # namespace package799- if spec.origin in {"namespace", None}:800- return os.path.dirname(next(iter(spec.submodule_search_locations)))801+ if root_spec.origin in {"namespace", None}:802+ package_spec = importlib.util.find_spec(import_name)803+ if package_spec is not None and package_spec.submodule_search_locations:804+ # Pick the path in the namespace that contains the submodule.805+ package_path = os.path.commonpath(806+ package_spec.submodule_search_locations807+ )808+ search_locations = (809+ location810+ for location in root_spec.submodule_search_locations811+ if package_path.startswith(location)812+ )813+ else:814+ # Pick the first path.815+ search_locations = iter(root_spec.submodule_search_locations)816+ return os.path.dirname(next(search_locations))817 # a package (with __init__.py)802- elif spec.submodule_search_locations:803- return os.path.dirname(os.path.dirname(spec.origin))818+ elif root_spec.submodule_search_locations:819+ return os.path.dirname(os.path.dirname(root_spec.origin))820 # just a normal module821 else:806- return os.path.dirname(spec.origin)822+ return os.path.dirname(root_spec.origin)823 824 # we were unable to find the `package_path` using PEP 451 loaders825 loader = pkgutil.get_loader(root_mod_name)826 33 unmodified lines860 The path is the entry in :attr:`sys.path` that contains the package861 for import. If the package is not installed, it's assumed that the862 package was imported from the current working directory.863 """848- root_mod_name, _, _ = import_name.partition(".")849- package_path = _find_package_path(root_mod_name)864+ package_path = _find_package_path(import_name)865 py_prefix = os.path.abspath(sys.prefix)866 867 # installed to the system868 if package_path.startswith(py_prefix):21 unmodified linesmodified:tests/test_instance_config.py+19 added
57 unmodified lines58 59 assert app.instance_path == str(modules_tmpdir.join("instance"))60 61 62+def test_uninstalled_namespace_paths(tmpdir, monkeypatch, purge_module):63+ def create_namespace(package):64+ project = tmpdir.join(f"project-{package}")65+ monkeypatch.syspath_prepend(str(project))66+ project.join("namespace").join(package).join("__init__.py").write(67+ "import flask\napp = flask.Flask(__name__)\n", ensure=True68+ )69+ return project70+71+ _ = create_namespace("package1")72+ project2 = create_namespace("package2")73+ purge_module("namespace.package2")74+ purge_module("namespace")75+76+ from namespace.package2 import app77+78+ assert app.instance_path == str(project2.join("instance"))79+80+81 def test_installed_module_paths(82 modules_tmpdir, modules_tmpdir_prefix, purge_module, site_packages, limit_loader83 ):84 site_packages.join("site_app.py").write(56 unmodified linesmodified:CHANGES.rst+2 added
6 unmodified lines7 8 - Inline some optional imports that are only used for certain CLI9 commands. :pr:`4606`10 - Relax type annotation for ``after_request`` functions. :issue:`4600`11+- ``instance_path`` for namespace packages uses the path closest to12+ the imported submodule. :issue:`4600`13 14 15 Version 2.1.216 -------------1280 unmodified lines