From 8d6daccfeb06ed7d887d773385b50eb037dd3b49 Mon Sep 17 00:00:00 2001 From: Mikhail Golubev Date: Mon, 27 May 2019 22:02:06 +0300 Subject: [PATCH] Secure line ending in .py files used for caching tests Otherwise, automatic line ending conversion might affect SHA 256 hashes calculated in test data based on its content. It allowed to uniformly compute it both for binary and text files. GitOrigin-RevId: b41ff089235c178b182271a81a26146cff54e053 --- python/helpers/generator3.py | 10 ++-------- python/helpers/tests/.gitattributes | 3 +++ 2 files changed, 5 insertions(+), 8 deletions(-) create mode 100644 python/helpers/tests/.gitattributes diff --git a/python/helpers/generator3.py b/python/helpers/generator3.py index 05e09ef115c9..16f412588447 100644 --- a/python/helpers/generator3.py +++ b/python/helpers/generator3.py @@ -377,14 +377,8 @@ def builtin_module_hash(mod_qname): def physical_module_hash(mod_path): - pure_py = os.path.splitext(mod_path)[1] == '.py' - if pure_py: - # Open .py files in text mode to avoid LF/CRLF conversion issues. It should happen only in tests. - with fopen(mod_path, 'r') as f: - return sha256_digest(f.read().encode(OUT_ENCODING)) - else: - with fopen(mod_path, 'rb') as f: - return sha256_digest(f) + with fopen(mod_path, 'rb') as f: + return sha256_digest(f) def version_to_tuple(version): diff --git a/python/helpers/tests/.gitattributes b/python/helpers/tests/.gitattributes new file mode 100644 index 000000000000..ce93867e18d3 --- /dev/null +++ b/python/helpers/tests/.gitattributes @@ -0,0 +1,3 @@ +# In these files persistent line endings are crucial +# as we use their binary content for hashes. +generator3_tests/data/SkeletonCaching/**/*.py text eol=lf \ No newline at end of file