Skip to content

Commit

Permalink
Mechanism: fix no-input execute causing shared default variable memory (
Browse files Browse the repository at this point in the history
#3148)

Calling Mechanism.execute with no input uses the Mechanism's
defaults.variable as input. This was passed in without creating a copy,
which resulted in defaults.variable sharing memory with an
InputPort.variable, which could result in silent changes to the
Mechanism's defaults.variable.
  • Loading branch information
kmantel authored Dec 19, 2024
1 parent cedcae9 commit eb5ff88
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion psyneulink/core/components/mechanisms/mechanism.py
Original file line number Diff line number Diff line change
Expand Up @@ -2539,7 +2539,7 @@ def execute(self,

# No input was specified, so use Mechanism's default variable
if input is None:
input = self.defaults.variable
input = copy_parameter_value(self.defaults.variable)
# FIX: this input value is sent to input CIMs when compositions are nested
# variable should be based on afferent projections
variable = self._get_variable_from_input(input, context)
Expand Down
8 changes: 8 additions & 0 deletions tests/mechanisms/test_mechanisms.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,14 @@ def test_noise_change_warning_to_function(self, noise):
):
t.parameters.noise.set(pnl.NormalDist)

def test_execute_no_input_doesnt_change_default_variable(self):
m = pnl.ProcessingMechanism(default_variable=1)
assert m.defaults.variable == [[1]]
m.execute()
assert m.defaults.variable == [[1]]
m.execute(2)
assert m.defaults.variable == [[1]]


class TestMechanismFunctionParameters:
f = pnl.Linear()
Expand Down

0 comments on commit eb5ff88

Please sign in to comment.