Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
a8b47a81
提交
a8b47a81
authored
6月 15, 2011
作者:
Olivier Delalleau
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Refactored try / except / finally for Python 2.4 compatibility
上级
481bd2ef
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
105 行增加
和
103 行删除
+105
-103
cmodule.py
theano/gof/cmodule.py
+105
-103
没有找到文件。
theano/gof/cmodule.py
浏览文件 @
a8b47a81
...
...
@@ -734,115 +734,117 @@ class ModuleCache(object):
raise
try
:
compile_steps
=
fn
(
location
=
location
)
.
__iter__
()
# Check if we already know a module with the same hash. If we
# do, then there is no need to even compile it.
duplicated_module
=
False
# The first compilation step is to yield the source code.
src_code
=
compile_steps
.
next
()
module_hash
=
get_module_hash
(
src_code
,
key
)
if
module_hash
in
self
.
module_hash_to_key_data
:
debug
(
"Duplicated module! Will re-use the previous one"
)
duplicated_module
=
True
# Load the already existing module.
key_data
=
self
.
module_hash_to_key_data
[
module_hash
]
# Note that we do not pass the `fn` argument, since it
# should not be used considering that the module should
# already be compiled.
module
=
self
.
module_from_key
(
key
=
None
,
key_data
=
key_data
)
name
=
module
.
__file__
# Add current key to the set of keys associated to the same
# module. We only save the KeyData object of versioned
# modules.
try
:
key_data
.
add_key
(
key
,
save_pkl
=
bool
(
_version
))
key_broken
=
False
except
cPickle
.
PicklingError
:
# This should only happen if we tried to save the
# pickled file.
assert
_version
# The key we are trying to add is broken: we will not
# add it after all.
key_data
.
remove_key
(
key
)
key_broken
=
True
if
(
_version
and
not
key_broken
and
self
.
check_for_broken_eq
):
self
.
check_key
(
key
,
key_data
.
key_pkl
)
# We can delete the work directory.
_rmtree
(
location
,
ignore_nocleanup
=
True
,
msg
=
'temporary workdir of duplicated module'
)
else
:
# Will fail if there is an error compiling the C code.
# The exception will be caught and the work dir will be
# deleted.
while
True
:
try
:
# The module should be returned by the last
# step of the compilation.
module
=
compile_steps
.
next
()
except
StopIteration
:
break
# Obtain path to the '.so' module file.
name
=
module
.
__file__
debug
(
"Adding module to cache"
,
key
,
name
)
assert
name
.
startswith
(
location
)
assert
name
not
in
self
.
module_from_name
# Changing the hash of the key is not allowed during
# compilation. That is the only cause found that makes the
# following assert fail.
assert
hash
(
key
)
==
hash_key
assert
key
not
in
self
.
entry_from_key
key_pkl
=
os
.
path
.
join
(
location
,
'key.pkl'
)
assert
not
os
.
path
.
exists
(
key_pkl
)
key_data
=
KeyData
(
keys
=
set
([
key
]),
module_hash
=
module_hash
,
key_pkl
=
key_pkl
,
entry
=
name
)
if
_version
:
# save the key
# Embedding two try statements for Python 2.4 compatibility
# (cannot do try / except / finally).
try
:
compile_steps
=
fn
(
location
=
location
)
.
__iter__
()
# Check if we already know a module with the same hash. If we
# do, then there is no need to even compile it.
duplicated_module
=
False
# The first compilation step is to yield the source code.
src_code
=
compile_steps
.
next
()
module_hash
=
get_module_hash
(
src_code
,
key
)
if
module_hash
in
self
.
module_hash_to_key_data
:
debug
(
"Duplicated module! Will re-use the previous one"
)
duplicated_module
=
True
# Load the already existing module.
key_data
=
self
.
module_hash_to_key_data
[
module_hash
]
# Note that we do not pass the `fn` argument, since it
# should not be used considering that the module should
# already be compiled.
module
=
self
.
module_from_key
(
key
=
None
,
key_data
=
key_data
)
name
=
module
.
__file__
# Add current key to the set of keys associated to the same
# module. We only save the KeyData object of versioned
# modules.
try
:
key_data
.
save_pkl
(
)
key_data
.
add_key
(
key
,
save_pkl
=
bool
(
_version
)
)
key_broken
=
False
except
cPickle
.
PicklingError
:
# This should only happen if we tried to save the
# pickled file.
assert
_version
# The key we are trying to add is broken: we will not
# add it after all.
key_data
.
remove_key
(
key
)
key_broken
=
True
# Remove key from the KeyData object, to make sure
# we never try to save it again.
# We still keep the KeyData object and save it so
# that the module can be re-used in the future.
key_data
.
keys
=
set
()
key_data
.
save_pkl
()
if
not
key_broken
and
self
.
check_for_broken_eq
:
self
.
check_key
(
key
,
key_pkl
)
# Adding the KeyData file to this set means it is a
# versioned module.
self
.
loaded_key_pkl
.
add
(
key_pkl
)
# Map the new module to its KeyData object. Note that we
# need to do it regardless of whether the key is versioned
# or not if we want to be able to re-use this module inside
# the same process.
self
.
module_hash_to_key_data
[
module_hash
]
=
key_data
if
(
_version
and
not
key_broken
and
self
.
check_for_broken_eq
):
self
.
check_key
(
key
,
key_data
.
key_pkl
)
except
:
# TODO try / except / finally is not Python2.4-friendly.
# This may happen e.g. when an Op has no C implementation. In
# any case, we do not want to keep around the temporary work
# directory, as it may cause trouble if we create too many of
# these. The 'ignore_if_missing' flag is set just in case this
# directory would have already been deleted.
_rmtree
(
location
,
ignore_if_missing
=
True
,
msg
=
'exception -- typically means no C implementation'
)
raise
# We can delete the work directory.
_rmtree
(
location
,
ignore_nocleanup
=
True
,
msg
=
'temporary workdir of duplicated module'
)
else
:
# Will fail if there is an error compiling the C code.
# The exception will be caught and the work dir will be
# deleted.
while
True
:
try
:
# The module should be returned by the last
# step of the compilation.
module
=
compile_steps
.
next
()
except
StopIteration
:
break
# Obtain path to the '.so' module file.
name
=
module
.
__file__
debug
(
"Adding module to cache"
,
key
,
name
)
assert
name
.
startswith
(
location
)
assert
name
not
in
self
.
module_from_name
# Changing the hash of the key is not allowed during
# compilation. That is the only cause found that makes the
# following assert fail.
assert
hash
(
key
)
==
hash_key
assert
key
not
in
self
.
entry_from_key
key_pkl
=
os
.
path
.
join
(
location
,
'key.pkl'
)
assert
not
os
.
path
.
exists
(
key_pkl
)
key_data
=
KeyData
(
keys
=
set
([
key
]),
module_hash
=
module_hash
,
key_pkl
=
key_pkl
,
entry
=
name
)
if
_version
:
# save the key
try
:
key_data
.
save_pkl
()
key_broken
=
False
except
cPickle
.
PicklingError
:
key_broken
=
True
# Remove key from the KeyData object, to make sure
# we never try to save it again.
# We still keep the KeyData object and save it so
# that the module can be re-used in the future.
key_data
.
keys
=
set
()
key_data
.
save_pkl
()
if
not
key_broken
and
self
.
check_for_broken_eq
:
self
.
check_key
(
key
,
key_pkl
)
# Adding the KeyData file to this set means it is a
# versioned module.
self
.
loaded_key_pkl
.
add
(
key_pkl
)
# Map the new module to its KeyData object. Note that we
# need to do it regardless of whether the key is versioned
# or not if we want to be able to re-use this module inside
# the same process.
self
.
module_hash_to_key_data
[
module_hash
]
=
key_data
except
:
# This may happen e.g. when an Op has no C implementation. In
# any case, we do not want to keep around the temporary work
# directory, as it may cause trouble if we create too many of
# these. The 'ignore_if_missing' flag is set just in case this
# directory would have already been deleted.
_rmtree
(
location
,
ignore_if_missing
=
True
,
msg
=
'exception -- typically means no C implementation'
)
raise
finally
:
# Release lock if needed.
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论