Skip to content

Commit 400b831

Browse files
committed
Load command uses utf-8 encoding for opening files in Python 3 instead of OS-default
This fixes a unit test bug where on Windows it was trying to load a utf-8 file as some other encoding starting with "cp".
1 parent ce8da64 commit 400b831

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

cmd2.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1659,9 +1659,15 @@ def do_load(self, file_path):
16591659
return
16601660

16611661
try:
1662-
# Add all commands in the script to the command queue
1663-
with open(expanded_path) as target:
1664-
self.cmdqueue.extend(target.read().splitlines())
1662+
# Specify file encoding in Python 3, but Python 2 doesn't allow that argument to open()
1663+
if six.PY3:
1664+
# Add all commands in the script to the command queue
1665+
with open(expanded_path, encoding='utf-8') as target:
1666+
self.cmdqueue.extend(target.read().splitlines())
1667+
else:
1668+
# Add all commands in the script to the command queue
1669+
with open(expanded_path) as target:
1670+
self.cmdqueue.extend(target.read().splitlines())
16651671

16661672
# Append in an "end of script (eos)" command to cleanup the self._script_dir list
16671673
self.cmdqueue.append('eos')

docs/freefeatures.rst

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,21 @@ Script files
1313
============
1414

1515
Text files can serve as scripts for your ``cmd2``-based
16-
application, with the ``load``, ``save``, and ``edit``
17-
commands.
16+
application, with the ``load``, ``_relative_load``, ``save``, and ``edit`` commands.
17+
18+
Both ASCII and UTF-8 encoded unicode text files are supported.
19+
20+
Simply include one command per line, typed exactly as you would inside a ``cmd2`` application.
1821

1922
.. automethod:: cmd2.Cmd.do_load
2023

24+
.. automethod:: cmd2.Cmd.do__relative_load
25+
2126
.. automethod:: cmd2.Cmd.do_save
2227

2328
.. automethod:: cmd2.Cmd.do_edit
2429

30+
2531
Comments
2632
========
2733

0 commit comments

Comments
 (0)