提交 130b5abc authored 作者: Olivier Delalleau's avatar Olivier Delalleau

Simplified code for In.borrow and Param.borrow

This commit simplifies the code so that In.borrow and Param.borrow can only be True or False (not None). It also makes the corresponding code for these two objects more similar. There should be no functional change, it just makes the code easier to understand.
上级 3733f854
......@@ -161,11 +161,10 @@ class In(SymbolicInput):
True: permit the compiled function to modify the python object being passed as the input
False: do not permit the compiled function to modify the python object being passed as the input.
borrow: Bool (default: False if mutable evaluate to False, True otherwise)
borrow: Bool (default: take the same value as mutable)
True: permit the output of the compiled function to be aliased to the input
False: do not permit any output to be aliased to the input
strict: Bool (default: False)
True: means that the value you pass for this input must have exactly the right type
False: the value you pass for this input may be cast automatically to the proper type
......@@ -201,24 +200,25 @@ class In(SymbolicInput):
#in the function or from the caller
self.shared = shared
# mutable implies the output can be both aliased to the input and that the input can be
# destroyed. borrow simply implies the output can be aliased to the input. Thus
# mutable=True should require borrow=True. Raise warning when borrow is explicitely set
# to False with mutable=True.
if borrow is None:
self.borrow = mutable
else:
self.borrow = borrow
# mutable implies the output can be both aliased to the input and that
# the input can be destroyed. borrow simply implies the output can be
# aliased to the input. Thus mutable=True should require borrow=True.
# Raise warning when borrow is explicitely set to False with
# mutable=True.
if mutable:
if borrow == False:
if not self.borrow:
_logger.warning("Symbolic input for variable %s (name=%s) has "
"flags mutable=True, borrow=False. This combination is "
"incompatible since mutable=True implies that the "
"input variable may be both aliased (borrow=True) and "
"over-written. We set borrow=True and continue.",
variable, name)
borrow = True
# borrow=None basically means False. We can't set default value to False because of the
# above business with mutable.
if borrow is None:
borrow = False
self.borrow = True
if implicit is None:
implicit = (isinstance(value, gof.Container) or
......@@ -233,7 +233,6 @@ class In(SymbolicInput):
autoname=autoname,
implicit=implicit)
self.value = value
self.borrow = borrow
if self.implicit and value is None:
raise TypeError('An implicit input must be given a default value')
......
......@@ -268,9 +268,9 @@ class Param(object):
:param mutable: True -> function is allowed to modify this argument.
:param borrow: True -> function is allowed to alias some output to
this input
:param borrow: Whether the function is allowed to alias some output to
this input. Using None (default) means we re-use the same value as the
`mutable` flag.
False: do not permit any output to be aliased to the input
:param strict: False -> function arguments may be copied or cast to match the
......@@ -289,25 +289,29 @@ class Param(object):
self.default = default
self.name = name
self.mutable = mutable
# mutable implies the output can be both aliased to the input and that the input can be
# destroyed. borrow simply implies the output can be aliased to the input. Thus
# mutable=True should require borrow=True. Raise warning when borrow is explicitely set
# to False with mutable=True.
if borrow is None:
self.borrow = self.mutable
else:
self.borrow = borrow
# mutable implies the output can be both aliased to the input and that
# the input can be destroyed. borrow simply implies the output can be
# aliased to the input. Thus mutable=True should require borrow=True.
# Raise warning when borrow is explicitly set to False with
# mutable=True.
if mutable:
# Do not compare with "if not borrow", because borrow can be None,
# and "not None" would be True.
if borrow is False:
if not self.borrow:
_logger.warning("Symbolic input for variable %s (name=%s) has "
"flags mutable=True, borrow=False. This combination is "
"incompatible since mutable=True implies that the "
"input variable may be both aliased (borrow=True) and "
"over-written. We set borrow=True and continue.",
variable, name)
borrow = True
self.borrow = True
self.strict = strict
self.allow_downcast = allow_downcast
self.implicit = implicit
self.borrow = borrow
def pfunc(params, outputs=None, mode=None, updates=[], givens=[],
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论