Skip to content

Commit

Permalink
Merge pull request #80 from NasaGeek/EP-90-set-argv-more-carefully
Browse files Browse the repository at this point in the history
Fix a couple issues with setting Tcl's ::arg* vars
  • Loading branch information
NasaGeek authored Jan 25, 2022
2 parents fd6f716 + a26d6fb commit 7d75ec8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
7 changes: 3 additions & 4 deletions pysrc/tohil/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,13 +304,12 @@ def _init_tcl_env():
}
return [list 0 ""]
}
# Clear in case we're being reloaded
unset -nocomplain ::argc ::argv ::argv0
"""

_tohil.eval(tcl_init)
if _sys.argv:
# In python3.8 and above, argv falls back to [""], but before that it would
# be just [] here (since we're not setting it ourselves).
if _sys.argv and _sys.argv != [""]:
# Set up argv globals as if we were tclsh
argv0 = _sys.argv[0]
argv = _sys.argv[1:]
Expand Down
12 changes: 9 additions & 3 deletions tests/test_arg_globals.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,25 @@


class TestArgGlobals(unittest.TestCase):
def setUp(self):
tohil.eval("unset -nocomplain ::argv ::argv0 ::argc")

def test_empty_argv(self):
# This can occur naturally, but only before python3.8
sys.argv = []
importlib.reload(tohil)
self.assertEqual(tohil.call("info", "exists", "::argv"), 0)
self.assertEqual(tohil.call("info", "exists", "::argv0"), 0)
self.assertEqual(tohil.call("info", "exists", "::argc"), 0)

def test_argv_blank(self):
# This is python's 3.8+ fallback behavior and can generally be equated
# to "nothing was set"
sys.argv = [""]
importlib.reload(tohil)
self.assertEqual(tohil.call("set", "::argv", to=list), [])
self.assertEqual(tohil.call("set", "::argv0"), "")
self.assertEqual(tohil.call("set", "::argc"), 0)
self.assertEqual(tohil.call("info", "exists", "::argv"), 0)
self.assertEqual(tohil.call("info", "exists", "::argv0"), 0)
self.assertEqual(tohil.call("info", "exists", "::argc"), 0)

def test_argv_multiple(self):
sys.argv = ["script.py", "arg1", "arg2"]
Expand Down

0 comments on commit 7d75ec8

Please sign in to comment.