Method: flask.helpers.get_root_path
Calls: 220, Exceptions: 27, Paths: 3Back
Path 1: 192 calls (0.87)
'test_blueprints' (73) 'test_config' (22) 'test_basic' (21) 'test_async' (16) 'test_cli' (11) 'test_helpers' (10) 'test_templating' (7) 'test_reqctx' ...
'/Users/andrehora/Documents/git/projects-pathspotter/flask/tests' (182) '/Users/andrehora/Documents/git/projects-pathspotter/flask/tests/test_apps/hel...
1def get_root_path(import_name: str) -> str:
2 """Find the root path of a package, or the path that contains a
3 module. If it cannot be found, returns the current working
4 directory.
5
6 Not to be confused with the value returned by :func:`find_package`.
7
8 :meta private:
9 """
10 # Module already imported and has a file attribute. Use that first.
11 mod = sys.modules.get(import_name)
12
13 if mod is not None and hasattr(mod, "__file__") and mod.__file__ is not None:
14 return os.path.dirname(os.path.abspath(mod.__file__))
15
16 # Next attempt: check the loader.
17 try:
18 spec = importlib.util.find_spec(import_name)
19
20 if spec is None:
21 raise ValueError
22 except (ImportError, ValueError):
23 loader = None
24 else:
25 loader = spec.loader
26
27 # Loader does not exist or we're referring to an unloaded main
28 # module or a main module without path (interactive sessions), go
29 # with the current working directory.
30 if loader is None:
31 return os.getcwd()
32
33 if hasattr(loader, "get_filename"):
34 filepath = loader.get_filename(import_name)
35 else:
36 # Fall back to imports.
37 __import__(import_name)
38 mod = sys.modules[import_name]
39 filepath = getattr(mod, "__file__", None)
40
41 # If we don't have a file path it might be because it is a
42 # namespace package. In this case pick the root path from the
43 # first module that is contained in the package.
44 if filepath is None:
45 raise RuntimeError(
46 "No root path can be found for the provided module"
47 f" {import_name!r}. This can happen because the module"
48 " came from an import hook that does not provide file"
49 " name information or because it's a namespace package."
50 " In this case the root path needs to be explicitly"
51 " provided."
52 )
53
54 # filepath is import_name.py for a module, or __init__.py for a package.
55 return os.path.dirname(os.path.abspath(filepath))
Path 2: 27 calls (0.12)
'testapp' (6) 'appname' (6) 'app' (4) 'appname1' (3) 'flaskgroup' (3) 'app2_foo_bar' (2) 'appname2' (1) 'createapp' (1) 'testappgroup' (1)
'/Users/andrehora/Documents/git/projects-pathspotter/flask' (27)
ValueError (27)
1def get_root_path(import_name: str) -> str:
2 """Find the root path of a package, or the path that contains a
3 module. If it cannot be found, returns the current working
4 directory.
5
6 Not to be confused with the value returned by :func:`find_package`.
7
8 :meta private:
9 """
10 # Module already imported and has a file attribute. Use that first.
11 mod = sys.modules.get(import_name)
12
13 if mod is not None and hasattr(mod, "__file__") and mod.__file__ is not None:
14 return os.path.dirname(os.path.abspath(mod.__file__))
15
16 # Next attempt: check the loader.
17 try:
18 spec = importlib.util.find_spec(import_name)
19
20 if spec is None:
21 raise ValueError
22 except (ImportError, ValueError):
23 loader = None
24 else:
25 loader = spec.loader
26
27 # Loader does not exist or we're referring to an unloaded main
28 # module or a main module without path (interactive sessions), go
29 # with the current working directory.
30 if loader is None:
31 return os.getcwd()
32
33 if hasattr(loader, "get_filename"):
34 filepath = loader.get_filename(import_name)
35 else:
36 # Fall back to imports.
37 __import__(import_name)
38 mod = sys.modules[import_name]
39 filepath = getattr(mod, "__file__", None)
40
41 # If we don't have a file path it might be because it is a
42 # namespace package. In this case pick the root path from the
43 # first module that is contained in the package.
44 if filepath is None:
45 raise RuntimeError(
46 "No root path can be found for the provided module"
47 f" {import_name!r}. This can happen because the module"
48 " came from an import hook that does not provide file"
49 " name information or because it's a namespace package."
50 " In this case the root path needs to be explicitly"
51 " provided."
52 )
53
54 # filepath is import_name.py for a module, or __init__.py for a package.
55 return os.path.dirname(os.path.abspath(filepath))
Path 3: 1 calls (0.0)
'importerror' (1)
'/Users/andrehora/Documents/git/projects-pathspotter/flask/.tox/tmp/py310/test_name_with_import_error0/modules_tmp' (1)
1def get_root_path(import_name: str) -> str:
2 """Find the root path of a package, or the path that contains a
3 module. If it cannot be found, returns the current working
4 directory.
5
6 Not to be confused with the value returned by :func:`find_package`.
7
8 :meta private:
9 """
10 # Module already imported and has a file attribute. Use that first.
11 mod = sys.modules.get(import_name)
12
13 if mod is not None and hasattr(mod, "__file__") and mod.__file__ is not None:
14 return os.path.dirname(os.path.abspath(mod.__file__))
15
16 # Next attempt: check the loader.
17 try:
18 spec = importlib.util.find_spec(import_name)
19
20 if spec is None:
21 raise ValueError
22 except (ImportError, ValueError):
23 loader = None
24 else:
25 loader = spec.loader
26
27 # Loader does not exist or we're referring to an unloaded main
28 # module or a main module without path (interactive sessions), go
29 # with the current working directory.
30 if loader is None:
31 return os.getcwd()
32
33 if hasattr(loader, "get_filename"):
34 filepath = loader.get_filename(import_name)
35 else:
36 # Fall back to imports.
37 __import__(import_name)
38 mod = sys.modules[import_name]
39 filepath = getattr(mod, "__file__", None)
40
41 # If we don't have a file path it might be because it is a
42 # namespace package. In this case pick the root path from the
43 # first module that is contained in the package.
44 if filepath is None:
45 raise RuntimeError(
46 "No root path can be found for the provided module"
47 f" {import_name!r}. This can happen because the module"
48 " came from an import hook that does not provide file"
49 " name information or because it's a namespace package."
50 " In this case the root path needs to be explicitly"
51 " provided."
52 )
53
54 # filepath is import_name.py for a module, or __init__.py for a package.
55 return os.path.dirname(os.path.abspath(filepath))