RedirectOutput and python 3 (ROOT6)

Hello !

I’ve a quick question to discuss the difference between python 2 and 3 and the way to treat the stdout by the gSystem.RedirectOutput.

Trying to be quick, I’m running macros for which part of the stdout is redirected to an output file through RedirectOutput method to produce display for a manual and the other part of the stdout is sent to a log file to collect the results of our tests.

I’m able to reproduce this with a very short macro provided along this post printPy3.py (247 Bytes)

To illustrate the difference seen, here is the same command run with python2 (and a ROOT 6.14.00 compiled accordingly) and a python 3 one (with a ROOT 6.14.00 compiled accordingly).

python2 printPy3.py -b > log.out && echo “test” && cat test.out && echo “log” && cat log.out

which gives this output :

test
Using print function.
TH1.Print Name = pouet, Entries= 0, Total sum= 0
Using sys.stdout function.
log

where everything print line from python is stored in test.out, along with the stdout from C++, which is what i expected. While this line

python3 printPy3.py -b > log.out && echo “test” && cat test.out && echo “log” && cat log.out

gives this result

test
TH1.Print Name = pouet, Entries= 0, Total sum= 0
log
Using print function.
Using sys.stdout function.

where everything is stored in the log file, but the stdout from C++ method.
If you have an idea of the possible difference between python 2 and 3 (or if this behaviour is due to the RedirectOutput method) I’d be glad to have advices.

Thanks in advance
cheers
JB


_ROOT Version:6.14.00
_Platform:Fedora26
_Compiler:gcc7.1


Hi @jbb,

It looks like Python3 manages stdout in a different way than Python2, in particular in a way that is not affected by the switch that RedirectOutput does in C++.

I opened a JIRA ticket to follow this issue.
https://sft.its.cern.ch/jira/browse/ROOT-9697

In the meantime, can you work around it by assigning also sys.stdout in Python3 to the file? E.g. sys.stdout = open("test.out","w")?

Cheers,
Enric

Thanks for the report and the suggestion

Unfortunatly it does not seem to work, and I was expecting this as I’d actually need both the python stdout and the dump from ROOT C++ method to be in the same file (the test.out and not the log).

I’ve changed the dummy macro to illustrate this, see printPy3.py (397 Bytes)

which gives this result

python3 printPy3.py -b > log.out && echo “test” && cat test.out && echo “log” && cat log.out
test
This message will be logged instead of displayed
pouet pouet
log
Using print function.
TH1.Print Name = pouet, Entries= 0, Total sum= 0

Where the ROOT C++ print line, is in the log whereas I’d like it to be in the tesT.out file.
thanks anyway
cheers
jb

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.