Skip to content

Commit

Permalink
BF: make a copy when addData() w/ mutables; closes psychopy#884
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremygray committed Apr 1, 2015
1 parent 2645d83 commit d3af967
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
6 changes: 6 additions & 0 deletions psychopy/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,12 @@ def addData(self, name, value):
"""
if name not in self.dataNames:
self.dataNames.append(name)
# could just copy() every value, but not always needed, so check:
try:
hash(value)
except TypeError:
# unhashable type (list, dict, ...) == mutable, so need a copy()
value = copy.deepcopy(value)
self.thisEntry[name]=value

def nextEntry(self):
Expand Down
22 changes: 22 additions & 0 deletions psychopy/tests/test_data/test_ExperimentHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,28 @@ def test_default(self):
# print e
#print 'done'

def test_addData_with_mutable_values(self):
# add mutable objects to data, check that the value *at that time* is saved
exp = data.ExperimentHandler(
name='testExp',
savePickle=False,
saveWideText=True,
dataFileName=self.tmpDir + 'mutables'
)

mutant = [1]
exp.addData('mutable', mutant)
exp.nextEntry()
mutant[0] = 9999
exp.addData('mutable', mutant)
exp.nextEntry()

exp.saveAsWideText(exp.dataFileName+'.csv', delim=',')

#get data file contents:
contents = open(exp.dataFileName+'.csv', 'rU').read()
assert contents == "mutable,\n[1],\n[9999],\n"

def test_unicode_conditions(self):
fileName = self.tmpDir + 'unicode_conds'

Expand Down

0 comments on commit d3af967

Please sign in to comment.