Skip to content

Commit 555db5d

Browse files
committed
Updated documentation and tests
1 parent db9c3a6 commit 555db5d

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
* Setting the following pyscript variables:
1313
* `__name__`: __main__
1414
* `__file__`: script path (as typed, ~ will be expanded)
15+
* Other
16+
* Removed undocumented `py run` command since it was replaced by `run_pyscript` a while ago
1517

1618
## 0.10.0 (February 7, 2020)
1719
* Enhancements

cmd2/cmd2.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3120,7 +3120,9 @@ def py_quit():
31203120
self._in_py = True
31213121
py_code_to_run = ''
31223122

3123-
# Locals for the Python environment we are creating
3123+
# Use self.py_locals as the locals() dictionary in the Python environment we are creating, but make
3124+
# a copy to prevent pyscripts from editing it. (e.g. locals().clear()). Only make a shallow copy since
3125+
# it's OK for py_locals to contain objects which are editable in a pyscript.
31243126
localvars = dict(self.py_locals)
31253127
localvars[self.py_bridge_name] = py_bridge
31263128
localvars['quit'] = py_quit

tests/test_cmd2.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ def test_base_shell(base_app, monkeypatch):
206206

207207
def test_base_py(base_app):
208208
# Make sure py can't edit Cmd.py_locals. It used to be that cmd2 was passing its py_locals
209-
# dictionary to the py environment instead of a copy.
209+
# dictionary to the py environment instead of a shallow copy.
210210
base_app.py_locals['test_var'] = 5
211211
out, err = run_cmd(base_app, 'py del[locals()["test_var"]]')
212212
assert not out and not err
@@ -215,6 +215,13 @@ def test_base_py(base_app):
215215
out, err = run_cmd(base_app, 'py print(test_var)')
216216
assert out[0].rstrip() == '5'
217217

218+
# Place an editable object in py_locals. Since we make a shallow copy of py_locals,
219+
# this object should be editable from the py environment.
220+
base_app.py_locals['my_list'] = []
221+
out, err = run_cmd(base_app, 'py my_list.append(2)')
222+
assert not out and not err
223+
assert base_app.py_locals['my_list'][0] == 2
224+
218225
# Try a print statement
219226
out, err = run_cmd(base_app, 'py print("spaces" + " in this " + "command")')
220227
assert out[0].rstrip() == 'spaces in this command'

0 commit comments

Comments
 (0)