We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Dynamic bash autocompletion should escape blanks
Thanks for #755, wonderful feature! I found an issue when my completion callback returns entries that have (legitimate) blanks in them. In this case, entries get split into separate arguments, whereas I would have expected that blanks are escaped with a backslash (as bash does by default).

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `click/_bashcomplete.py`
Content:
```
1 import collections
2 import copy
3 import os
4 import re
5 
6 from .utils import echo
7 from .parser import split_arg_string
8 from .core import MultiCommand, Option, Argument
9 from .types import Choice
10 
11 WORDBREAK = '='
12 
13 COMPLETION_SCRIPT = '''
14 %(complete_func)s() {
15     COMPREPLY=( $( env COMP_WORDS="${COMP_WORDS[*]}" \\
16                    COMP_CWORD=$COMP_CWORD \\
17                    %(autocomplete_var)s=complete $1 ) )
18     return 0
19 }
20 
21 complete -F %(complete_func)s -o default %(script_names)s
22 '''
23 
24 _invalid_ident_char_re = re.compile(r'[^a-zA-Z0-9_]')
25 
26 
27 def get_completion_script(prog_name, complete_var):
28     cf_name = _invalid_ident_char_re.sub('', prog_name.replace('-', '_'))
29     return (COMPLETION_SCRIPT % {
30         'complete_func': '_%s_completion' % cf_name,
31         'script_names': prog_name,
32         'autocomplete_var': complete_var,
33     }).strip() + ';'
34 
35 
36 def resolve_ctx(cli, prog_name, args):
37     """
38     Parse into a hierarchy of contexts. Contexts are connected through the parent variable.
39     :param cli: command definition
40     :param prog_name: the program that is running
41     :param args: full list of args
42     :return: the final context/command parsed
43     """
44     ctx = cli.make_context(prog_name, args, resilient_parsing=True)
45     while ctx.protected_args + ctx.args and isinstance(ctx.command, MultiCommand):
46         a = ctx.protected_args + ctx.args
47         cmd = ctx.command.get_command(ctx, a[0])
48         if cmd is None:
49             return None
50         ctx = cmd.make_context(a[0], a[1:], parent=ctx, resilient_parsing=True)
51     return ctx
52 
53 
54 def start_of_option(param_str):
55     """
56     :param param_str: param_str to check
57     :return: whether or not this is the start of an option declaration (i.e. starts "-" or "--")
58     """
59     return param_str and param_str[:1] == '-'
60 
61 
62 def is_incomplete_option(all_args, cmd_param):
63     """
64     :param all_args: the full original list of args supplied
65     :param cmd_param: the current command paramter
66     :return: whether or not the last option declaration (i.e. starts "-" or "--") is incomplete and
67     corresponds to this cmd_param. In other words whether this cmd_param option can still accept
68     values
69     """
70     if cmd_param.is_flag:
71         return False
72     last_option = None
73     for index, arg_str in enumerate(reversed([arg for arg in all_args if arg != WORDBREAK])):
74         if index + 1 > cmd_param.nargs:
75             break
76         if start_of_option(arg_str):
77             last_option = arg_str
78 
79     return True if last_option and last_option in cmd_param.opts else False
80 
81 
82 def is_incomplete_argument(current_params, cmd_param):
83     """
84     :param current_params: the current params and values for this argument as already entered
85     :param cmd_param: the current command parameter
86     :return: whether or not the last argument is incomplete and corresponds to this cmd_param. In
87     other words whether or not the this cmd_param argument can still accept values
88     """
89     current_param_values = current_params[cmd_param.name]
90     if current_param_values is None:
91         return True
92     if cmd_param.nargs == -1:
93         return True
94     if isinstance(current_param_values, collections.Iterable) \
95             and cmd_param.nargs > 1 and len(current_param_values) < cmd_param.nargs:
96         return True
97     return False
98 
99 def get_user_autocompletions(ctx, args, incomplete, cmd_param):
100     """
101     :param ctx: context associated with the parsed command
102     :param args: full list of args
103     :param incomplete: the incomplete text to autocomplete
104     :param cmd_param: command definition
105     :return: all the possible user-specified completions for the param
106     """
107     if isinstance(cmd_param.type, Choice):
108         return cmd_param.type.choices
109     elif cmd_param.autocompletion is not None:
110         return cmd_param.autocompletion(ctx=ctx,
111                                         args=args,
112                                         incomplete=incomplete)
113     else:
114         return []
115 
116 def get_choices(cli, prog_name, args, incomplete):
117     """
118     :param cli: command definition
119     :param prog_name: the program that is running
120     :param args: full list of args
121     :param incomplete: the incomplete text to autocomplete
122     :return: all the possible completions for the incomplete
123     """
124     all_args = copy.deepcopy(args)
125 
126     ctx = resolve_ctx(cli, prog_name, args)
127     if ctx is None:
128         return
129 
130     # In newer versions of bash long opts with '='s are partitioned, but it's easier to parse
131     # without the '='
132     if start_of_option(incomplete) and WORDBREAK in incomplete:
133         partition_incomplete = incomplete.partition(WORDBREAK)
134         all_args.append(partition_incomplete[0])
135         incomplete = partition_incomplete[2]
136     elif incomplete == WORDBREAK:
137         incomplete = ''
138 
139     choices = []
140     found_param = False
141     if start_of_option(incomplete):
142         # completions for options
143         for param in ctx.command.params:
144             if isinstance(param, Option):
145                 choices.extend([param_opt for param_opt in param.opts + param.secondary_opts
146                                 if param_opt not in all_args or param.multiple])
147         found_param = True
148     if not found_param:
149         # completion for option values by choices
150         for cmd_param in ctx.command.params:
151             if isinstance(cmd_param, Option) and is_incomplete_option(all_args, cmd_param):
152                 choices.extend(get_user_autocompletions(ctx, all_args, incomplete, cmd_param))
153                 found_param = True
154                 break
155     if not found_param:
156         # completion for argument values by choices
157         for cmd_param in ctx.command.params:
158             if isinstance(cmd_param, Argument) and is_incomplete_argument(ctx.params, cmd_param):
159                 choices.extend(get_user_autocompletions(ctx, all_args, incomplete, cmd_param))
160                 found_param = True
161                 break
162 
163     if not found_param and isinstance(ctx.command, MultiCommand):
164         # completion for any subcommands
165         choices.extend(ctx.command.list_commands(ctx))
166 
167     for item in choices:
168         if item.startswith(incomplete):
169             yield item
170 
171 
172 def do_complete(cli, prog_name):
173     cwords = split_arg_string(os.environ['COMP_WORDS'])
174     cword = int(os.environ['COMP_CWORD'])
175     args = cwords[1:cword]
176     try:
177         incomplete = cwords[cword]
178     except IndexError:
179         incomplete = ''
180 
181     for item in get_choices(cli, prog_name, args, incomplete):
182         echo(item)
183 
184     return True
185 
186 
187 def bashcomplete(cli, prog_name, complete_var, complete_instr):
188     if complete_instr == 'source':
189         echo(get_completion_script(prog_name, complete_var))
190         return True
191     elif complete_instr == 'complete':
192         return do_complete(cli, prog_name)
193     return False
194
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/click/_bashcomplete.py b/click/_bashcomplete.py
--- a/click/_bashcomplete.py
+++ b/click/_bashcomplete.py
@@ -12,6 +12,7 @@
 
 COMPLETION_SCRIPT = '''
 %(complete_func)s() {
+    local IFS=$'\n'
     COMPREPLY=( $( env COMP_WORDS="${COMP_WORDS[*]}" \\
                    COMP_CWORD=$COMP_CWORD \\
                    %(autocomplete_var)s=complete $1 ) )

{"golden_diff": "diff --git a/click/_bashcomplete.py b/click/_bashcomplete.py\n--- a/click/_bashcomplete.py\n+++ b/click/_bashcomplete.py\n@@ -12,6 +12,7 @@\n \n COMPLETION_SCRIPT = '''\n %(complete_func)s() {\n+    local IFS=$'\\n'\n     COMPREPLY=( $( env COMP_WORDS=\"${COMP_WORDS[*]}\" \\\\\n                    COMP_CWORD=$COMP_CWORD \\\\\n                    %(autocomplete_var)s=complete $1 ) )\n", "issue": "Dynamic bash autocompletion should escape blanks\nThanks for #755, wonderful feature! I found an issue when my completion callback returns entries that have (legitimate) blanks in them. In this case, entries get split into separate arguments, whereas I would have expected that blanks are escaped with a backslash (as bash does by default).\n", "before_files": [{"content": "import collections\nimport copy\nimport os\nimport re\n\nfrom .utils import echo\nfrom .parser import split_arg_string\nfrom .core import MultiCommand, Option, Argument\nfrom .types import Choice\n\nWORDBREAK = '='\n\nCOMPLETION_SCRIPT = '''\n%(complete_func)s() {\n    COMPREPLY=( $( env COMP_WORDS=\"${COMP_WORDS[*]}\" \\\\\n                   COMP_CWORD=$COMP_CWORD \\\\\n                   %(autocomplete_var)s=complete $1 ) )\n    return 0\n}\n\ncomplete -F %(complete_func)s -o default %(script_names)s\n'''\n\n_invalid_ident_char_re = re.compile(r'[^a-zA-Z0-9_]')\n\n\ndef get_completion_script(prog_name, complete_var):\n    cf_name = _invalid_ident_char_re.sub('', prog_name.replace('-', '_'))\n    return (COMPLETION_SCRIPT % {\n        'complete_func': '_%s_completion' % cf_name,\n        'script_names': prog_name,\n        'autocomplete_var': complete_var,\n    }).strip() + ';'\n\n\ndef resolve_ctx(cli, prog_name, args):\n    \"\"\"\n    Parse into a hierarchy of contexts. Contexts are connected through the parent variable.\n    :param cli: command definition\n    :param prog_name: the program that is running\n    :param args: full list of args\n    :return: the final context/command parsed\n    \"\"\"\n    ctx = cli.make_context(prog_name, args, resilient_parsing=True)\n    while ctx.protected_args + ctx.args and isinstance(ctx.command, MultiCommand):\n        a = ctx.protected_args + ctx.args\n        cmd = ctx.command.get_command(ctx, a[0])\n        if cmd is None:\n            return None\n        ctx = cmd.make_context(a[0], a[1:], parent=ctx, resilient_parsing=True)\n    return ctx\n\n\ndef start_of_option(param_str):\n    \"\"\"\n    :param param_str: param_str to check\n    :return: whether or not this is the start of an option declaration (i.e. starts \"-\" or \"--\")\n    \"\"\"\n    return param_str and param_str[:1] == '-'\n\n\ndef is_incomplete_option(all_args, cmd_param):\n    \"\"\"\n    :param all_args: the full original list of args supplied\n    :param cmd_param: the current command paramter\n    :return: whether or not the last option declaration (i.e. starts \"-\" or \"--\") is incomplete and\n    corresponds to this cmd_param. In other words whether this cmd_param option can still accept\n    values\n    \"\"\"\n    if cmd_param.is_flag:\n        return False\n    last_option = None\n    for index, arg_str in enumerate(reversed([arg for arg in all_args if arg != WORDBREAK])):\n        if index + 1 > cmd_param.nargs:\n            break\n        if start_of_option(arg_str):\n            last_option = arg_str\n\n    return True if last_option and last_option in cmd_param.opts else False\n\n\ndef is_incomplete_argument(current_params, cmd_param):\n    \"\"\"\n    :param current_params: the current params and values for this argument as already entered\n    :param cmd_param: the current command parameter\n    :return: whether or not the last argument is incomplete and corresponds to this cmd_param. In\n    other words whether or not the this cmd_param argument can still accept values\n    \"\"\"\n    current_param_values = current_params[cmd_param.name]\n    if current_param_values is None:\n        return True\n    if cmd_param.nargs == -1:\n        return True\n    if isinstance(current_param_values, collections.Iterable) \\\n            and cmd_param.nargs > 1 and len(current_param_values) < cmd_param.nargs:\n        return True\n    return False\n\ndef get_user_autocompletions(ctx, args, incomplete, cmd_param):\n    \"\"\"\n    :param ctx: context associated with the parsed command\n    :param args: full list of args\n    :param incomplete: the incomplete text to autocomplete\n    :param cmd_param: command definition\n    :return: all the possible user-specified completions for the param\n    \"\"\"\n    if isinstance(cmd_param.type, Choice):\n        return cmd_param.type.choices\n    elif cmd_param.autocompletion is not None:\n        return cmd_param.autocompletion(ctx=ctx,\n                                        args=args,\n                                        incomplete=incomplete)\n    else:\n        return []\n\ndef get_choices(cli, prog_name, args, incomplete):\n    \"\"\"\n    :param cli: command definition\n    :param prog_name: the program that is running\n    :param args: full list of args\n    :param incomplete: the incomplete text to autocomplete\n    :return: all the possible completions for the incomplete\n    \"\"\"\n    all_args = copy.deepcopy(args)\n\n    ctx = resolve_ctx(cli, prog_name, args)\n    if ctx is None:\n        return\n\n    # In newer versions of bash long opts with '='s are partitioned, but it's easier to parse\n    # without the '='\n    if start_of_option(incomplete) and WORDBREAK in incomplete:\n        partition_incomplete = incomplete.partition(WORDBREAK)\n        all_args.append(partition_incomplete[0])\n        incomplete = partition_incomplete[2]\n    elif incomplete == WORDBREAK:\n        incomplete = ''\n\n    choices = []\n    found_param = False\n    if start_of_option(incomplete):\n        # completions for options\n        for param in ctx.command.params:\n            if isinstance(param, Option):\n                choices.extend([param_opt for param_opt in param.opts + param.secondary_opts\n                                if param_opt not in all_args or param.multiple])\n        found_param = True\n    if not found_param:\n        # completion for option values by choices\n        for cmd_param in ctx.command.params:\n            if isinstance(cmd_param, Option) and is_incomplete_option(all_args, cmd_param):\n                choices.extend(get_user_autocompletions(ctx, all_args, incomplete, cmd_param))\n                found_param = True\n                break\n    if not found_param:\n        # completion for argument values by choices\n        for cmd_param in ctx.command.params:\n            if isinstance(cmd_param, Argument) and is_incomplete_argument(ctx.params, cmd_param):\n                choices.extend(get_user_autocompletions(ctx, all_args, incomplete, cmd_param))\n                found_param = True\n                break\n\n    if not found_param and isinstance(ctx.command, MultiCommand):\n        # completion for any subcommands\n        choices.extend(ctx.command.list_commands(ctx))\n\n    for item in choices:\n        if item.startswith(incomplete):\n            yield item\n\n\ndef do_complete(cli, prog_name):\n    cwords = split_arg_string(os.environ['COMP_WORDS'])\n    cword = int(os.environ['COMP_CWORD'])\n    args = cwords[1:cword]\n    try:\n        incomplete = cwords[cword]\n    except IndexError:\n        incomplete = ''\n\n    for item in get_choices(cli, prog_name, args, incomplete):\n        echo(item)\n\n    return True\n\n\ndef bashcomplete(cli, prog_name, complete_var, complete_instr):\n    if complete_instr == 'source':\n        echo(get_completion_script(prog_name, complete_var))\n        return True\n    elif complete_instr == 'complete':\n        return do_complete(cli, prog_name)\n    return False\n", "path": "click/_bashcomplete.py"}], "after_files": [{"content": "import collections\nimport copy\nimport os\nimport re\n\nfrom .utils import echo\nfrom .parser import split_arg_string\nfrom .core import MultiCommand, Option, Argument\nfrom .types import Choice\n\nWORDBREAK = '='\n\nCOMPLETION_SCRIPT = '''\n%(complete_func)s() {\n    local IFS=$'\\n'\n    COMPREPLY=( $( env COMP_WORDS=\"${COMP_WORDS[*]}\" \\\\\n                   COMP_CWORD=$COMP_CWORD \\\\\n                   %(autocomplete_var)s=complete $1 ) )\n    return 0\n}\n\ncomplete -F %(complete_func)s -o default %(script_names)s\n'''\n\n_invalid_ident_char_re = re.compile(r'[^a-zA-Z0-9_]')\n\n\ndef get_completion_script(prog_name, complete_var):\n    cf_name = _invalid_ident_char_re.sub('', prog_name.replace('-', '_'))\n    return (COMPLETION_SCRIPT % {\n        'complete_func': '_%s_completion' % cf_name,\n        'script_names': prog_name,\n        'autocomplete_var': complete_var,\n    }).strip() + ';'\n\n\ndef resolve_ctx(cli, prog_name, args):\n    \"\"\"\n    Parse into a hierarchy of contexts. Contexts are connected through the parent variable.\n    :param cli: command definition\n    :param prog_name: the program that is running\n    :param args: full list of args\n    :return: the final context/command parsed\n    \"\"\"\n    ctx = cli.make_context(prog_name, args, resilient_parsing=True)\n    while ctx.protected_args + ctx.args and isinstance(ctx.command, MultiCommand):\n        a = ctx.protected_args + ctx.args\n        cmd = ctx.command.get_command(ctx, a[0])\n        if cmd is None:\n            return None\n        ctx = cmd.make_context(a[0], a[1:], parent=ctx, resilient_parsing=True)\n    return ctx\n\n\ndef start_of_option(param_str):\n    \"\"\"\n    :param param_str: param_str to check\n    :return: whether or not this is the start of an option declaration (i.e. starts \"-\" or \"--\")\n    \"\"\"\n    return param_str and param_str[:1] == '-'\n\n\ndef is_incomplete_option(all_args, cmd_param):\n    \"\"\"\n    :param all_args: the full original list of args supplied\n    :param cmd_param: the current command paramter\n    :return: whether or not the last option declaration (i.e. starts \"-\" or \"--\") is incomplete and\n    corresponds to this cmd_param. In other words whether this cmd_param option can still accept\n    values\n    \"\"\"\n    if cmd_param.is_flag:\n        return False\n    last_option = None\n    for index, arg_str in enumerate(reversed([arg for arg in all_args if arg != WORDBREAK])):\n        if index + 1 > cmd_param.nargs:\n            break\n        if start_of_option(arg_str):\n            last_option = arg_str\n\n    return True if last_option and last_option in cmd_param.opts else False\n\n\ndef is_incomplete_argument(current_params, cmd_param):\n    \"\"\"\n    :param current_params: the current params and values for this argument as already entered\n    :param cmd_param: the current command parameter\n    :return: whether or not the last argument is incomplete and corresponds to this cmd_param. In\n    other words whether or not the this cmd_param argument can still accept values\n    \"\"\"\n    current_param_values = current_params[cmd_param.name]\n    if current_param_values is None:\n        return True\n    if cmd_param.nargs == -1:\n        return True\n    if isinstance(current_param_values, collections.Iterable) \\\n            and cmd_param.nargs > 1 and len(current_param_values) < cmd_param.nargs:\n        return True\n    return False\n\ndef get_user_autocompletions(ctx, args, incomplete, cmd_param):\n    \"\"\"\n    :param ctx: context associated with the parsed command\n    :param args: full list of args\n    :param incomplete: the incomplete text to autocomplete\n    :param cmd_param: command definition\n    :return: all the possible user-specified completions for the param\n    \"\"\"\n    if isinstance(cmd_param.type, Choice):\n        return cmd_param.type.choices\n    elif cmd_param.autocompletion is not None:\n        return cmd_param.autocompletion(ctx=ctx,\n                                        args=args,\n                                        incomplete=incomplete)\n    else:\n        return []\n\ndef get_choices(cli, prog_name, args, incomplete):\n    \"\"\"\n    :param cli: command definition\n    :param prog_name: the program that is running\n    :param args: full list of args\n    :param incomplete: the incomplete text to autocomplete\n    :return: all the possible completions for the incomplete\n    \"\"\"\n    all_args = copy.deepcopy(args)\n\n    ctx = resolve_ctx(cli, prog_name, args)\n    if ctx is None:\n        return\n\n    # In newer versions of bash long opts with '='s are partitioned, but it's easier to parse\n    # without the '='\n    if start_of_option(incomplete) and WORDBREAK in incomplete:\n        partition_incomplete = incomplete.partition(WORDBREAK)\n        all_args.append(partition_incomplete[0])\n        incomplete = partition_incomplete[2]\n    elif incomplete == WORDBREAK:\n        incomplete = ''\n\n    choices = []\n    found_param = False\n    if start_of_option(incomplete):\n        # completions for options\n        for param in ctx.command.params:\n            if isinstance(param, Option):\n                choices.extend([param_opt for param_opt in param.opts + param.secondary_opts\n                                if param_opt not in all_args or param.multiple])\n        found_param = True\n    if not found_param:\n        # completion for option values by choices\n        for cmd_param in ctx.command.params:\n            if isinstance(cmd_param, Option) and is_incomplete_option(all_args, cmd_param):\n                choices.extend(get_user_autocompletions(ctx, all_args, incomplete, cmd_param))\n                found_param = True\n                break\n    if not found_param:\n        # completion for argument values by choices\n        for cmd_param in ctx.command.params:\n            if isinstance(cmd_param, Argument) and is_incomplete_argument(ctx.params, cmd_param):\n                choices.extend(get_user_autocompletions(ctx, all_args, incomplete, cmd_param))\n                found_param = True\n                break\n\n    if not found_param and isinstance(ctx.command, MultiCommand):\n        # completion for any subcommands\n        choices.extend(ctx.command.list_commands(ctx))\n\n    for item in choices:\n        if item.startswith(incomplete):\n            yield item\n\n\ndef do_complete(cli, prog_name):\n    cwords = split_arg_string(os.environ['COMP_WORDS'])\n    cword = int(os.environ['COMP_CWORD'])\n    args = cwords[1:cword]\n    try:\n        incomplete = cwords[cword]\n    except IndexError:\n        incomplete = ''\n\n    for item in get_choices(cli, prog_name, args, incomplete):\n        echo(item)\n\n    return True\n\n\ndef bashcomplete(cli, prog_name, complete_var, complete_instr):\n    if complete_instr == 'source':\n        echo(get_completion_script(prog_name, complete_var))\n        return True\n    elif complete_instr == 'complete':\n        return do_complete(cli, prog_name)\n    return False\n", "path": "click/_bashcomplete.py"}]}