From 9156618a56d635bb51261d019a3703a1b4e3b588 Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Fri, 14 Feb 2020 16:28:41 -0500 Subject: Fixed bug where pyscripts could edit cmd2.Cmd.py_locals dictionary. Fixed bug where cmd2 set sys.path[0] for a pyscript to its cwd instead of the script's directory. Fixed bug where sys.path was not being restored after a pyscript ran. Setting the following pyscript variables: __name__: __main__ __file__: script path (as typed) Removed do_py.run() function since it didn't handle arguments and offered no benefit over run_pyscript. --- tests/pyscript/environment.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 tests/pyscript/environment.py (limited to 'tests/pyscript/environment.py') diff --git a/tests/pyscript/environment.py b/tests/pyscript/environment.py new file mode 100644 index 00000000..5e4d93d6 --- /dev/null +++ b/tests/pyscript/environment.py @@ -0,0 +1,20 @@ +# flake8: noqa F821 +# Tests that cmd2 populates __name__, __file__, and sets sys.path[0] to our directory +import os +import sys +app.cmd_echo = True + +if __name__ != '__main__': + print("Error: __name__ is: {}".format(__name__)) + quit() + +if __file__ != sys.argv[0]: + print("Error: __file__ is: {}".format(__file__)) + quit() + +our_dir = os.path.dirname(os.path.abspath(__file__)) +if our_dir != sys.path[0]: + print("Error: our_dir is: {}".format(our_dir)) + quit() + +print("PASSED") -- cgit v1.2.1