Skip to content

Commit

Permalink
Skip first sys.argv on frozen environment
Browse files Browse the repository at this point in the history
  • Loading branch information
xoriole committed Feb 2, 2022
1 parent 6c3a148 commit b303931
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/tribler-gui/tribler_gui/core_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ def on_event_manager_initial_error(self, _):
else:
self.start_tribler_core()

def get_core_args_based_on_env(self):
# If the core is running on frozen environment, sys.argv[0] becomes tribler executable while
# running from source code, sys.argv[0] is run_tribler.py which is run by python runtime.
# So, we should skip the first argument to prevent argument parsing error on frozen environment
# so that the arguments are parsed correctly by ArgumentParser (see run_tribler.py)
sys_argv = sys.argv if not hasattr(sys, '_MEIPASS') else sys.argv[1:]
return sys_argv + ['--core']

def start_tribler_core(self):
self.use_existing_core = False

Expand All @@ -92,7 +100,7 @@ def start_tribler_core(self):

core_args = self.core_args
if not core_args:
core_args = sys.argv + ['--core']
core_args = self.get_core_args_based_on_env()

self.core_process = QProcess()
self.core_process.setProcessEnvironment(core_env)
Expand Down
19 changes: 19 additions & 0 deletions src/tribler-gui/tribler_gui/tests/test_core_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,22 @@ def test_on_core_stdout_stderr_read_ready_os_error():
# check that OSError exception is suppressed when writing to stdout and stderr
core_manager.on_core_stdout_read_ready()
core_manager.on_core_stderr_read_ready()


@patch('tribler_gui.core_manager.EventRequestManager', new=MagicMock())
def test_get_core_args_based_on_env():
import sys # pylint: disable=import-outside-toplevel
# test that correct core arguments are composed based on frozen environment
core_manager = CoreManager(MagicMock(), MagicMock(), MagicMock(), MagicMock())

# Unfrozen environment
core_args = core_manager.get_core_args_based_on_env()
assert not hasattr(sys, '_MEIPASS')
assert '--core' in core_args
assert sys.argv[0] in core_args

# Frozen environment
sys._MEIPASS = __file__ # pylint: disable=protected-access
core_args = core_manager.get_core_args_based_on_env()
assert '--core' in core_args
assert sys.argv[0] not in core_args

0 comments on commit b303931

Please sign in to comment.