From b209758a7d4e03f10bbed7a68589630def1535ed Mon Sep 17 00:00:00 2001 From: Josselin Date: Tue, 30 Apr 2019 14:50:03 +0100 Subject: [PATCH] Add slither._add_source_code function --- slither/core/slither_core.py | 8 ++++++++ slither/slither.py | 3 +-- slither/solc_parsing/slitherSolc.py | 12 +++--------- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/slither/core/slither_core.py b/slither/core/slither_core.py index bbd93a7b4..a72fa3482 100644 --- a/slither/core/slither_core.py +++ b/slither/core/slither_core.py @@ -56,6 +56,14 @@ class Slither(Context): """str: Filename.""" return self._filename + def _add_source_code(self, path): + """ + :param path: + :return: + """ + with open(path, encoding='utf8', newline='') as f: + self.source_code[path] = f.read() + # endregion ################################################################################### ################################################################################### diff --git a/slither/slither.py b/slither/slither.py index 39435bf8c..d30099426 100644 --- a/slither/slither.py +++ b/slither/slither.py @@ -65,8 +65,7 @@ class Slither(SlitherSolc): for path, ast in cryticCompile.asts.items(): self._parse_contracts_from_loaded_json(ast, path) - with open(path, encoding='utf8', newline='') as f: - self.source_code[path] = f.read() + self._add_source_code(path) self._detectors = [] self._printers = [] diff --git a/slither/solc_parsing/slitherSolc.py b/slither/solc_parsing/slitherSolc.py index 5fa3c160e..5f0e60b13 100644 --- a/slither/solc_parsing/slitherSolc.py +++ b/slither/solc_parsing/slitherSolc.py @@ -89,9 +89,7 @@ class SlitherSolc(Slither): if 'sourcePaths' in data_loaded: for sourcePath in data_loaded['sourcePaths']: if os.path.isfile(sourcePath): - with open(sourcePath, encoding='utf8', newline='') as f: - source_code = f.read() - self.source_code[sourcePath] = source_code + self._add_source_code(sourcePath) if data_loaded[self.get_key()] == 'root': self._solc_version = '0.3' @@ -152,15 +150,11 @@ class SlitherSolc(Slither): self._source_units[sourceUnit] = name if os.path.isfile(name) and not name in self.source_code: - with open(name, encoding='utf8', newline='') as f: - source_code = f.read() - self.source_code[name] = source_code + self._add_source_code(name) else: lib_name = os.path.join('node_modules', name) if os.path.isfile(lib_name) and not name in self.source_code: - with open(lib_name, encoding='utf8', newline='') as f: - source_code = f.read() - self.source_code[name] = source_code + self._add_source_code(lib_name) # endregion ###################################################################################