Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
冰.封万里
flask
提交
10b0d6b3
F
flask
项目概览
冰.封万里
/
flask
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
F
flask
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
10b0d6b3
编写于
4月 24, 2021
作者:
A
Adrian Moennich
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Move get_root_path back to flask.helpers
Many libraries use it so the move is somewhat disruptive.
上级
1a8549de
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
52 addition
and
49 deletion
+52
-49
src/flask/helpers.py
src/flask/helpers.py
+51
-0
src/flask/scaffold.py
src/flask/scaffold.py
+1
-49
未找到文件。
src/flask/helpers.py
浏览文件 @
10b0d6b3
import
os
import
pkgutil
import
socket
import
sys
import
typing
as
t
import
warnings
from
datetime
import
timedelta
...
...
@@ -677,6 +679,55 @@ def send_from_directory(directory: str, path: str, **kwargs: t.Any) -> "Response
)
def
get_root_path
(
import_name
:
str
)
->
str
:
"""Find the root path of a package, or the path that contains a
module. If it cannot be found, returns the current working
directory.
Not to be confused with the value returned by :func:`find_package`.
:meta private:
"""
# Module already imported and has a file attribute. Use that first.
mod
=
sys
.
modules
.
get
(
import_name
)
if
mod
is
not
None
and
hasattr
(
mod
,
"__file__"
):
return
os
.
path
.
dirname
(
os
.
path
.
abspath
(
mod
.
__file__
))
# Next attempt: check the loader.
loader
=
pkgutil
.
get_loader
(
import_name
)
# Loader does not exist or we're referring to an unloaded main
# module or a main module without path (interactive sessions), go
# with the current working directory.
if
loader
is
None
or
import_name
==
"__main__"
:
return
os
.
getcwd
()
if
hasattr
(
loader
,
"get_filename"
):
filepath
=
loader
.
get_filename
(
import_name
)
# type: ignore
else
:
# Fall back to imports.
__import__
(
import_name
)
mod
=
sys
.
modules
[
import_name
]
filepath
=
getattr
(
mod
,
"__file__"
,
None
)
# If we don't have a file path it might be because it is a
# namespace package. In this case pick the root path from the
# first module that is contained in the package.
if
filepath
is
None
:
raise
RuntimeError
(
"No root path can be found for the provided module"
f
"
{
import_name
!
r
}
. This can happen because the module"
" came from an import hook that does not provide file"
" name information or because it's a namespace package."
" In this case the root path needs to be explicitly"
" provided."
)
# filepath is import_name.py for a module, or __init__.py for a package.
return
os
.
path
.
dirname
(
os
.
path
.
abspath
(
filepath
))
class
locked_cached_property
(
werkzeug
.
utils
.
cached_property
):
"""A :func:`property` that is only evaluated once. Like
:class:`werkzeug.utils.cached_property` except access uses a lock
...
...
src/flask/scaffold.py
浏览文件 @
10b0d6b3
...
...
@@ -14,6 +14,7 @@ from werkzeug.exceptions import HTTPException
from
.cli
import
AppGroup
from
.globals
import
current_app
from
.helpers
import
get_root_path
from
.helpers
import
locked_cached_property
from
.helpers
import
send_from_directory
from
.templating
import
_default_template_ctx_processor
...
...
@@ -745,55 +746,6 @@ def _endpoint_from_view_func(view_func: t.Callable) -> str:
return
view_func
.
__name__
def
get_root_path
(
import_name
:
str
)
->
str
:
"""Find the root path of a package, or the path that contains a
module. If it cannot be found, returns the current working
directory.
Not to be confused with the value returned by :func:`find_package`.
:meta private:
"""
# Module already imported and has a file attribute. Use that first.
mod
=
sys
.
modules
.
get
(
import_name
)
if
mod
is
not
None
and
hasattr
(
mod
,
"__file__"
):
return
os
.
path
.
dirname
(
os
.
path
.
abspath
(
mod
.
__file__
))
# Next attempt: check the loader.
loader
=
pkgutil
.
get_loader
(
import_name
)
# Loader does not exist or we're referring to an unloaded main
# module or a main module without path (interactive sessions), go
# with the current working directory.
if
loader
is
None
or
import_name
==
"__main__"
:
return
os
.
getcwd
()
if
hasattr
(
loader
,
"get_filename"
):
filepath
=
loader
.
get_filename
(
import_name
)
# type: ignore
else
:
# Fall back to imports.
__import__
(
import_name
)
mod
=
sys
.
modules
[
import_name
]
filepath
=
getattr
(
mod
,
"__file__"
,
None
)
# If we don't have a file path it might be because it is a
# namespace package. In this case pick the root path from the
# first module that is contained in the package.
if
filepath
is
None
:
raise
RuntimeError
(
"No root path can be found for the provided module"
f
"
{
import_name
!
r
}
. This can happen because the module"
" came from an import hook that does not provide file"
" name information or because it's a namespace package."
" In this case the root path needs to be explicitly"
" provided."
)
# filepath is import_name.py for a module, or __init__.py for a package.
return
os
.
path
.
dirname
(
os
.
path
.
abspath
(
filepath
))
def
_matching_loader_thinks_module_is_package
(
loader
,
mod_name
):
"""Attempt to figure out if the given name is a package or a module.
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录