question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Newline characters in output capture are inappropriately translated in Python 3

See original GitHub issue

This issue is specific to the captured output (i.e. in cout.txt) when SETTINGS.CAPTURE_MODE == "fd" (i.e. the default in Linux), in Python3.

In this capture mode, Sacred attempts to capture both stdout and stderr for the process and all subprocesses by forwarding these two streams to a temporary file. The contents of this temporary file are then read out every “hearbeat” and redirected to cout.txt.

However, in Python 3, the default behavior for opening a file is to convert all types of line break characters (i.e. \r, \n, and \r\n) into the newline character \n (see here for documentation of this). This causes things like progress bars to be captured incorrectly in Python 3. This is precisely the issue seen in issue 440.

A quick-and-dirty fix that works only for Python 3 would be to alter (line 119 in stdout_capturing.py) to be with NamedTemporaryFile(mode="w+", newline='') as target:

Below is a short test script that should reproduce this behavior:

import sacred                                                                   
import sys                                                                      
                                                                  
ex = sacred.Experiment("ex")                                                    
ex.observers.append(sacred.observers.FileStorageObserver.create("my_bug_test"))

# This following line doesn't work for carriage returns, because all carriage returns
# have already been converted to new lines       
ex.captured_out_filter = sacred.utils.apply_backspaces_and_linefeeds  
                                                                                
@ex.automain                                                                    
def main():                                                                     
    for _ in range(5):                                                          
        sys.stdout.write("Should not see this\r")                               
    sys.stdout.write("Should only see this\n")                                  
    sys.stdout.flush()

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:3
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
amtsengcommented, Feb 6, 2020

Sorry for the delay, I hadn’t been getting these notifications for some reason. PR has been submitted!

1reaction
CarloLucibellocommented, Dec 15, 2019

friendly bump to @atseng95 for a PR

Read more comments on GitHub >

github_iconTop Results From Across the Web

Handling \r\n vs \n newlines in python on Mac vs Windows
When writing output to the stream, if newline is None, any '\n' characters written are translated to the system default line separator, os.linesep....
Read more >
Newline - Wikipedia
If the final character sequence in a text file is not a newline, the final line of the file may be considered to...
Read more >
What's New In Python 3.4 — Python 3.11.1 documentation
A new function escape() provides a way to escape special characters in a filename so that they do not become part of the...
Read more >
The GNU Awk User's Guide
However, gawk ignores newlines after any of the following symbols and keywords: ... output for all string constants that have been marked for...
Read more >
Handle line breaks (newlines) in Python - nkmk note
By default, print() adds a newline at the end. Therefore, if you execute print() continuously, each output result will be displayed with a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found