From 05c6233908d5c1ffbeac05f405d8e9c5000ecdeb Mon Sep 17 00:00:00 2001 From: Blake Harnden Date: Fri, 21 Jun 2019 09:29:19 -0700 Subject: [PATCH] added utility method to replace execfile for python2/3 support --- daemon/core/api/tlv/corehandlers.py | 2 +- daemon/core/utils.py | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/daemon/core/api/tlv/corehandlers.py b/daemon/core/api/tlv/corehandlers.py index 7ccd15c0..64d952ed 100644 --- a/daemon/core/api/tlv/corehandlers.py +++ b/daemon/core/api/tlv/corehandlers.py @@ -859,7 +859,7 @@ class CoreHandler(socketserver.BaseRequestHandler): raise else: thread = threading.Thread( - target=execfile, + target=utils.execute_file, args=(file_name, {"__file__": file_name, "coreemu": self.coreemu}) ) thread.daemon = True diff --git a/daemon/core/utils.py b/daemon/core/utils.py index dbbdc321..98d7a9bf 100644 --- a/daemon/core/utils.py +++ b/daemon/core/utils.py @@ -18,6 +18,27 @@ from core import CoreCommandError DEVNULL = open(os.devnull, "wb") +def execute_file(path, exec_globals=None, exec_locals=None): + """ + Provides an alternative way to run execfile to be compatible for + both python2/3. + + :param str path: path of file to execute + :param dict exec_globals: globals values to pass to execution + :param dict exec_locals: local values to pass to execution + :return: nothing + """ + if exec_globals is None: + exec_globals = {} + exec_globals.update({ + "__file__": path, + "__name__": "__main__" + }) + with open(path, "rb") as f: + data = compile(f.read(), path, "exec") + exec(data, exec_globals, exec_locals) + + def hashkey(value): """ Provide a consistent hash that can be used in place