psychopy.org | Reference | Downloads | Github

Bug: disappearing line breaks in code cells in Builder


#1

I am finding that sometimes, Python code in some code block components gets messed up. By that I mean that all the line breaks within a code block will be removed, leading to just a single long line of python text, which obviously does not work any more. Spaces used for indentation are still present, it is just the line breaks which disappear.

My interim ‘solution’ is to manually fix it by entering line breaks again. Although this is pretty frustrating and error prone given that its crucial to get the level of indenting correct.

My setup is a mixture of Mac + PC. I am editing the .psyexp file in the builder view. Sometimes I edit on a Mac and sometimes on a PC. The files are stored in a shared dropbox folder that keeps them synced. Editing of the .psyexp only happens on one computer at a time and is only done on multiple computers because I’m either in the lab with students, or editing on laptop outside the lab.

The problem has been occurring in 1.90.1, but is also occurring now I am using 3.0.3.

I cannot work out the definitive circumstance to reproduce the problem. My hunch is that it’s some esoteric text encoding issue with Mac vs PC, but that doesn’t account for why it only affects some code blocks some of the time.

Any help or insights much appreciated.


#2

Thanks for letting us know which versions you are using. Could you provide a minimal working example of the bug?


#3

I’ve found exactly the same issue recently, in the latest stand-alone release. I find that text typed into PsychoPy carries over fine between Mac and PC. The issue mostly seems to arise when I’ve been testing code somewhere else (e.g. PyCharm) or working on text in an editor like BBEdit, and then pasting it into a PsychoPy code component. It will look fine on the Mac, but just that pasted section will lose the line breaks when opened in Windows.

I wonder if it might be due to PsychoPy using Windows-style double character line breaks (CR + LF) when typing, regardless of platform, but when text is pasted in on a Mac, it comes with just a single LF character?


#4

Yes! In retrospect, I think this is basically it. It’s always happened when I’ve pasted code into the code blocks, which has always been on a Mac. Then the line breaks disappear only on the PC, not on a Mac, as far as I can remember.


#5

Below is a screenshot from the BBEdit text editor, where one can control the line ending character(s) on a per-document basis:

Might be worth checking whether controlling the source of the pasted text like that influences the end result. I’ll try to remember to do this next time I’m working on such a file. But to fix the problem properly would probably require making PsychoPy more aware of processing text line endings, either when it is pasted in or when the document is saved.


#6

You can also make the EOLs visible using View > Show EOLs in the Coder menu. Also, there is a preference setting called newline convention in the Coder tab. Here, you can choose your style of newline, and that should determine how your EOL convention when reading scripts.


#7

No luck I’m afraid. I’ve tried all combinations of pasting in text (on Mac) which was either LF or CRLF in my text editor (Atom), and Coder View > Preferences > Coder > newline convention (unix and dos). In all instances the newlines are removed when opened on the PC.

When pasting into a code block in Builder View on Mac, the pasted lines have an additional line return when pasting from CRLF format. Even when I leave in those extra blank lines we are left with zero newlines when opened on the PC.

So I think this is the right direction of enquiry, but there is no working solution as yet. As far as I can tell ALL newlines are removed when pasting into a code block on Mac (regardless of format being LF or CRLF), when opening on a PC.

PS. As far as I can tell it is not possible to ask for the EOL characters for code blocks in the Builder View.


#8

Ok, perhaps we can look at formatting the pasted code according to the newline param. Will have a look at adding a fix on Github.


#9

Much appreciated.


#10

@drbenvincent, I believe the fix listed below should address your problem. Now, when a file is opened, all EOLs are converted to the EOL Coder preferences. So, if you have mixed EOL, they should now all be the same when you compile your script or open it in Coder.


#11

Great stuff! Thanks for the quick turnaround. I guess that should be available in the 3.0.4 release?


#12

Yes, I would think it will be in the next release, so long as the code is ok


#13

Well done David, great to get this bug squashed.