Merge pull request #3971 from ThiefMaster/get-root-path
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Move get_root_path back to flask.helpers
authored by David Lord · committed by GitHub · parents 1a8549d, 10b0d6b · Browse files · 2 files·+52 added−49 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+52 added−49 removed
- other:+52 added−49 removed
Provenance
- Commit
1ccf063- Tree
2cd13a3- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
1a8549d- Parent 2
10b0d6b
2 files changed+52 added−49 removed
modified:src/flask/helpers.py+51 added
1 import os2+import pkgutil3 import socket4+import sys5 import typing as t6 import warnings7 from datetime import timedelta8 from functools import update_wrapper669 unmodified lines678 directory, path, **_prepare_send_file_kwargs(**kwargs)679 )680 681 682+def get_root_path(import_name: str) -> str:683+ """Find the root path of a package, or the path that contains a684+ module. If it cannot be found, returns the current working685+ directory.686+687+ Not to be confused with the value returned by :func:`find_package`.688+689+ :meta private:690+ """691+ # Module already imported and has a file attribute. Use that first.692+ mod = sys.modules.get(import_name)693+694+ if mod is not None and hasattr(mod, "__file__"):695+ return os.path.dirname(os.path.abspath(mod.__file__))696+697+ # Next attempt: check the loader.698+ loader = pkgutil.get_loader(import_name)699+700+ # Loader does not exist or we're referring to an unloaded main701+ # module or a main module without path (interactive sessions), go702+ # with the current working directory.703+ if loader is None or import_name == "__main__":704+ return os.getcwd()705+706+ if hasattr(loader, "get_filename"):707+ filepath = loader.get_filename(import_name) # type: ignore708+ else:709+ # Fall back to imports.710+ __import__(import_name)711+ mod = sys.modules[import_name]712+ filepath = getattr(mod, "__file__", None)713+714+ # If we don't have a file path it might be because it is a715+ # namespace package. In this case pick the root path from the716+ # first module that is contained in the package.717+ if filepath is None:718+ raise RuntimeError(719+ "No root path can be found for the provided module"720+ f" {import_name!r}. This can happen because the module"721+ " came from an import hook that does not provide file"722+ " name information or because it's a namespace package."723+ " In this case the root path needs to be explicitly"724+ " provided."725+ )726+727+ # filepath is import_name.py for a module, or __init__.py for a package.728+ return os.path.dirname(os.path.abspath(filepath))729+730+731 class locked_cached_property(werkzeug.utils.cached_property):732 """A :func:`property` that is only evaluated once. Like733 :class:`werkzeug.utils.cached_property` except access uses a lock734 for thread safety.117 unmodified linesmodified:src/flask/scaffold.py+1 added-49 removed
12 unmodified lines13 from werkzeug.exceptions import HTTPException14 15 from .cli import AppGroup16 from .globals import current_app17+from .helpers import get_root_path18 from .helpers import locked_cached_property19 from .helpers import send_from_directory20 from .templating import _default_template_ctx_processor21 from .typing import AfterRequestCallable723 unmodified lines745 assert view_func is not None, "expected view func if endpoint is not provided."746 return view_func.__name__747 748 748-def get_root_path(import_name: str) -> str:749- """Find the root path of a package, or the path that contains a750- module. If it cannot be found, returns the current working751- directory.752-753- Not to be confused with the value returned by :func:`find_package`.754-755- :meta private:756- """757- # Module already imported and has a file attribute. Use that first.758- mod = sys.modules.get(import_name)759-760- if mod is not None and hasattr(mod, "__file__"):761- return os.path.dirname(os.path.abspath(mod.__file__))762-763- # Next attempt: check the loader.764- loader = pkgutil.get_loader(import_name)765-766- # Loader does not exist or we're referring to an unloaded main767- # module or a main module without path (interactive sessions), go768- # with the current working directory.769- if loader is None or import_name == "__main__":770- return os.getcwd()771-772- if hasattr(loader, "get_filename"):773- filepath = loader.get_filename(import_name) # type: ignore774- else:775- # Fall back to imports.776- __import__(import_name)777- mod = sys.modules[import_name]778- filepath = getattr(mod, "__file__", None)779-780- # If we don't have a file path it might be because it is a781- # namespace package. In this case pick the root path from the782- # first module that is contained in the package.783- if filepath is None:784- raise RuntimeError(785- "No root path can be found for the provided module"786- f" {import_name!r}. This can happen because the module"787- " came from an import hook that does not provide file"788- " name information or because it's a namespace package."789- " In this case the root path needs to be explicitly"790- " provided."791- )792-793- # filepath is import_name.py for a module, or __init__.py for a package.794- return os.path.dirname(os.path.abspath(filepath))795-796-749 def _matching_loader_thinks_module_is_package(loader, mod_name):750 """Attempt to figure out if the given name is a package or a module.751 752 :param: loader: The loader that handled the name.113 unmodified lines