Using the mouse component with 'end routine on press' 'valid click' causes TypeError: can't pickle weakref objects

OS: windows 10
PsychoPy version: 202.1.3
Standard Standalone

I am using the mouse component to capture when the mouse moves an object to specific location. By setting ‘end routine on press’ to ‘valid click’. That works fine.
But I get the following:

Error in atexit._run_exitfuncs:
Traceback (most recent call last):
  File "C:\Users\phili\AppData\Roaming\psychopy3\versions\psychopy\data\experiment.py", line 381, in saveAsPickle
    pickle.dump(self, f)
TypeError: can't pickle weakref objects

Because of this no data file is produced.

If I change ‘setting end routine on press’ to ‘never’. I do not get the error above. But of course that is not what I want.

Any help would be greatly appreciated. I am sure there is a simple solution but I cannot work it out.

Phil

I had the following piece of code for my mouse function under Each Frame.

if mouse.isPressedIn(questionmark2) and movingPiece is None:
    picked.append(questionmark2)
if mouse.isPressedIn(leftresponse3):
    basketchosen = 'left'
if mouse.isPressedIn(rightresponse3):
    basketchosen = 'right'
movingPiece = movePicked(picked, mouse, movingPiece)
thisExp.addData('movingPiece', movingPiece)
thisExp.addData('basketchosen', basketchosen)

The last 2 lines of code is what is causing the problem. If I comment them out the problem goes away. This gets me around my problem but it would be good to understand why this is causes the failure…

In the data file, you can only save simple text or numeric data (i.e. it is effectively a text file). So you can’t meaningfully embed other sorts of objects (like pictures or sounds or other complex Python objects).

So in your code above, thisExp.addData('basketchosen', basketchosen) is probably OK, as basketchosen should be a simple text value (either 'left' or 'right'). So the problem is presumably thisExp.addData('movingPiece', movingPiece).

It’s not clear from the code above what movingPiece is, but because you check whether it is None, I’m guessing it is some kind of object, such as a stimulus component. An object can be turned into some sort of representation which can be stored in a file on disk (‘pickling’ it), but tabular data files aren’t really suitable for that.

You should be able to get around this by just saving the name of the object instead, e.g:

thisExp.addData('movingPiece', movingPiece.name)

although that will cause an error if movingPiece is None. I’m not sure how your movePicked() function works in terms of what it returns: you might require some additional checking to handle that possibility before the .addData() step.

Michael,

Thanks for your reply. That makes sense. I was only using the statement for debugging so have now commented it out.

Phil