Fixed #23551 -- Fixed bash autocompletion crash on Python 3.

This commit is contained in:
Marco Buttu 2014-09-20 16:38:37 +02:00 committed by Tim Graham
parent 9f576dd54f
commit e077c29155
3 changed files with 27 additions and 3 deletions

View File

@ -222,7 +222,7 @@ class ManagementUtility(object):
curr = '' curr = ''
subcommands = list(get_commands()) + ['help'] subcommands = list(get_commands()) + ['help']
options = [('--help', None)] options = [('--help', False)]
# subcommand # subcommand
if cword == 1: if cword == 1:

View File

@ -70,3 +70,5 @@ Bugfixes
* Made the :setting:`SERIALIZE <TEST_SERIALIZE>` entry in the * Made the :setting:`SERIALIZE <TEST_SERIALIZE>` entry in the
:setting:`TEST <DATABASE-TEST>` dictionary usable (:ticket:`23421`). :setting:`TEST <DATABASE-TEST>` dictionary usable (:ticket:`23421`).
* Fixed a crash in bash autocompletion on Python 3 (:ticket:`23551`).

View File

@ -32,9 +32,24 @@ class BashCompletionTests(unittest.TestCase):
del os.environ['DJANGO_AUTO_COMPLETE'] del os.environ['DJANGO_AUTO_COMPLETE']
def _user_input(self, input_str): def _user_input(self, input_str):
"""
Set the environment and the list of command line arguments.
This sets the bash variables $COMP_WORDS and $COMP_CWORD. The former is
an array consisting of the individual words in the current command
line, the latter is the index of the current cursor position, so in
case a word is completed and the cursor is placed after a whitespace,
$COMP_CWORD must be incremented by 1:
* 'django-admin start' -> COMP_CWORD=1
* 'django-admin startproject' -> COMP_CWORD=1
* 'django-admin startproject ' -> COMP_CWORD=2
"""
os.environ['COMP_WORDS'] = input_str os.environ['COMP_WORDS'] = input_str
os.environ['COMP_CWORD'] = str(len(input_str.split()) - 1) idx = len(input_str.split(' ')) - 1 # Index of the last word
sys.argv = input_str.split(' ') comp_cword = idx + 1 if input_str.endswith(' ') else idx
os.environ['COMP_CWORD'] = str(comp_cword)
sys.argv = input_str.split()
def _run_autocomplete(self): def _run_autocomplete(self):
util = ManagementUtility(argv=sys.argv) util = ManagementUtility(argv=sys.argv)
@ -68,6 +83,13 @@ class BashCompletionTests(unittest.TestCase):
output = self._run_autocomplete() output = self._run_autocomplete()
self.assertEqual(output, ['sql sqlall sqlclear sqlcustom sqldropindexes sqlflush sqlindexes sqlmigrate sqlsequencereset']) self.assertEqual(output, ['sql sqlall sqlclear sqlcustom sqldropindexes sqlflush sqlindexes sqlmigrate sqlsequencereset'])
def test_completed_subcommand(self):
"Show option flags in case a subcommand is completed"
self._user_input('django-admin startproject ') # Trailing whitespace
output = self._run_autocomplete()
for item in output:
self.assertTrue(item.startswith('--'))
def test_help(self): def test_help(self):
"No errors, just an empty list if there are no autocomplete options" "No errors, just an empty list if there are no autocomplete options"
self._user_input('django-admin help --') self._user_input('django-admin help --')