Skip to content

Commit

Permalink
Set argv global variables from Python like tclsh would
Browse files Browse the repository at this point in the history
EP-85
  • Loading branch information
NasaGeek committed Dec 16, 2021
1 parent ebfc9e4 commit 1ba9436
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
11 changes: 11 additions & 0 deletions pysrc/tohil/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,9 +304,20 @@ 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:
# Set up argv globals as if we were tclsh
argv0 = _sys.argv[0]
argv = _sys.argv[1:]
argc = len(argv)
_tohil.call("set", "::argv0", argv0)
_tohil.call("set", "::argv", argv)
_tohil.call("set", "::argc", argc)

_init_tcl_env()

Expand Down
28 changes: 28 additions & 0 deletions tests/test_arg_globals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import importlib
import sys
import unittest

import tohil


class TestArgGlobals(unittest.TestCase):
def test_empty_argv(self):
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):
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)

def test_argv_multiple(self):
sys.argv = ["script.py", "arg1", "arg2"]
importlib.reload(tohil)
self.assertEqual(tohil.call("set", "::argv", to=list), sys.argv[1:])
self.assertEqual(tohil.call("set", "::argv0"), "script.py")
self.assertEqual(tohil.call("set", "::argc"), 2)

0 comments on commit 1ba9436

Please sign in to comment.