This is an archive of the discontinued LLVM Phabricator instance.

[LNT] Python 3 support: adapt csv reader open
ClosedPublic

Authored by thopre on Oct 10 2019, 8:44 AM.

Details

Summary

csv.reader have different expectation between Python versions regarding
the file object it reads from. On Python 2 it requires the file to have
been opened in binary mode while on Python 3 it requires text mode and
universal newlines mode enabled but forwarded untranslated to the
caller. This commit use an if to cater to both requirements.

Event Timeline

thopre created this revision.Oct 10 2019, 8:44 AM
PrzemekWirkus added inline comments.Dec 5 2019, 7:46 AM
lnt/tests/nt.py
774–777

Can you verify that you should rather use mode == 'U' for universal mode?

https://docs.python.org/release/3.2/library/functions.html#open

See table below the Python 3 open function instead of newline == ''.

I'm not an expert in this area.

PrzemekWirkus added inline comments.Dec 5 2019, 7:49 AM
lnt/tests/nt.py
774–777

I need to clarify:

I was wondering if we should combine mode == 'rU' with newline=='' or not.

PrzemekWirkus added inline comments.Dec 5 2019, 7:52 AM
lnt/tests/nt.py
774–777

My thinking is that when you use Universal mode "rU" you will get all newlines -> '\n'. Would that be enough you think?

PrzemekWirkus added inline comments.Dec 5 2019, 7:58 AM
lnt/tests/nt.py
774–777
report_file = open(report_path, 'ru')

Should be enough I guess. Are you able to verify this on your end ?

774–777

s/u/U/

report_file = open(report_path, 'rU')
thopre marked an inline comment as done.Dec 5 2019, 8:27 AM
thopre added inline comments.
lnt/tests/nt.py
774–777

It works on my testing but I'm not too keen on using rU since the doc you linked says specifically it's for compatibility and should not be used in new code. My goal in this Python 3 conversion is to follow the Python 3 codestyle/idioms.

PrzemekWirkus accepted this revision.Dec 5 2019, 3:27 PM

LGTM

You've defended your code ;)

This revision is now accepted and ready to land.Dec 5 2019, 3:27 PM
thopre closed this revision.Dec 6 2019, 1:34 AM