prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Cookiecutter needs to always use utf-8 for writing files
  I get this on Windows (pitty me):
  
  ```
    File "c:\program files\python 3.5\lib\site-packages\cookiecutter\generate.py", line 318, in generate_files
      run_hook('post_gen_project', project_dir, context)
    File "c:\program files\python 3.5\lib\site-packages\cookiecutter\hooks.py", line 107, in run_hook
      return run_script_with_context(script, project_dir, context)
    File "c:\program files\python 3.5\lib\site-packages\cookiecutter\hooks.py", line 90, in run_script_with_context
      temp.write(Template(contents).render(**context))
    File "c:\program files\python 3.5\lib\tempfile.py", line 482, in func_wrapper
      return func(*args, **kwargs)
    File "c:\program files\python 3.5\lib\encodings\cp1252.py", line 19, in encode
      return codecs.charmap_encode(input,self.errors,encoding_table)[0]
  UnicodeEncodeError: 'charmap' codec can't encode character '\u0103' in position 1626: character maps to <undefined>
  ```
  
  cookiecutter should pass `encoding='utf-8'` to `NamedTemporaryFile`
  
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `cookiecutter/hooks.py`
  Content:
  ```
  1 #!/usr/bin/env python
  2 # -*- coding: utf-8 -*-
  3 
  4 """
  5 cookiecutter.hooks
  6 ------------------
  7 
  8 Functions for discovering and executing various cookiecutter hooks.
  9 """
  10 
  11 import io
  12 import logging
  13 import os
  14 import subprocess
  15 import sys
  16 import tempfile
  17 
  18 from jinja2 import Template
  19 
  20 from cookiecutter import utils
  21 from .exceptions import FailedHookException
  22 
  23 
  24 _HOOKS = [
  25     'pre_gen_project',
  26     'post_gen_project',
  27     # TODO: other hooks should be listed here
  28 ]
  29 EXIT_SUCCESS = 0
  30 
  31 
  32 def find_hooks():
  33     """
  34     Must be called with the project template as the current working directory.
  35     Returns a dict of all hook scripts provided.
  36     Dict's key will be the hook/script's name, without extension, while
  37     values will be the absolute path to the script.
  38     Missing scripts will not be included in the returned dict.
  39     """
  40     hooks_dir = 'hooks'
  41     r = {}
  42     logging.debug('hooks_dir is {0}'.format(hooks_dir))
  43     if not os.path.isdir(hooks_dir):
  44         logging.debug('No hooks/ dir in template_dir')
  45         return r
  46     for f in os.listdir(hooks_dir):
  47         basename = os.path.splitext(os.path.basename(f))[0]
  48         if basename in _HOOKS:
  49             r[basename] = os.path.abspath(os.path.join(hooks_dir, f))
  50     return r
  51 
  52 
  53 def run_script(script_path, cwd='.'):
  54     """
  55     Executes a script from a working directory.
  56 
  57     :param script_path: Absolute path to the script to run.
  58     :param cwd: The directory to run the script from.
  59     """
  60     run_thru_shell = sys.platform.startswith('win')
  61     if script_path.endswith('.py'):
  62         script_command = [sys.executable, script_path]
  63     else:
  64         script_command = [script_path]
  65 
  66     utils.make_executable(script_path)
  67 
  68     proc = subprocess.Popen(
  69         script_command,
  70         shell=run_thru_shell,
  71         cwd=cwd
  72     )
  73     exit_status = proc.wait()
  74     if exit_status != EXIT_SUCCESS:
  75         raise FailedHookException(
  76             "Hook script failed (exit status: %d)" % exit_status)
  77 
  78 
  79 def run_script_with_context(script_path, cwd, context):
  80     """
  81     Executes a script after rendering with it Jinja.
  82 
  83     :param script_path: Absolute path to the script to run.
  84     :param cwd: The directory to run the script from.
  85     :param context: Cookiecutter project template context.
  86     """
  87     _, extension = os.path.splitext(script_path)
  88 
  89     contents = io.open(script_path, 'r', encoding='utf-8').read()
  90 
  91     with tempfile.NamedTemporaryFile(
  92         delete=False,
  93         mode='w',
  94         suffix=extension
  95     ) as temp:
  96         temp.write(Template(contents).render(**context))
  97 
  98     run_script(temp.name, cwd)
  99 
  100 
  101 def run_hook(hook_name, project_dir, context):
  102     """
  103     Try to find and execute a hook from the specified project directory.
  104 
  105     :param hook_name: The hook to execute.
  106     :param project_dir: The directory to execute the script from.
  107     :param context: Cookiecutter project context.
  108     """
  109     script = find_hooks().get(hook_name)
  110     if script is None:
  111         logging.debug('No hooks found')
  112         return
  113     run_script_with_context(script, project_dir, context)
  114
  ```
  --- 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()
  
  ```
  
golden_diff: diff --git a/cookiecutter/hooks.py b/cookiecutter/hooks.py
  --- a/cookiecutter/hooks.py
  +++ b/cookiecutter/hooks.py
  @@ -90,10 +90,11 @@
   
       with tempfile.NamedTemporaryFile(
           delete=False,
  -        mode='w',
  +        mode='wb',
           suffix=extension
       ) as temp:
  -        temp.write(Template(contents).render(**context))
  +        output = Template(contents).render(**context)
  +        temp.write(output.encode('utf-8'))
   
       run_script(temp.name, cwd)
  
verification_info: {"golden_diff": "diff --git a/cookiecutter/hooks.py b/cookiecutter/hooks.py\n--- a/cookiecutter/hooks.py\n+++ b/cookiecutter/hooks.py\n@@ -90,10 +90,11 @@\n \n     with tempfile.NamedTemporaryFile(\n         delete=False,\n-        mode='w',\n+        mode='wb',\n         suffix=extension\n     ) as temp:\n-        temp.write(Template(contents).render(**context))\n+        output = Template(contents).render(**context)\n+        temp.write(output.encode('utf-8'))\n \n     run_script(temp.name, cwd)\n", "issue": "Cookiecutter needs to always use utf-8 for writing files\nI get this on Windows (pitty me):\n\n```\n  File \"c:\\program files\\python 3.5\\lib\\site-packages\\cookiecutter\\generate.py\", line 318, in generate_files\n    run_hook('post_gen_project', project_dir, context)\n  File \"c:\\program files\\python 3.5\\lib\\site-packages\\cookiecutter\\hooks.py\", line 107, in run_hook\n    return run_script_with_context(script, project_dir, context)\n  File \"c:\\program files\\python 3.5\\lib\\site-packages\\cookiecutter\\hooks.py\", line 90, in run_script_with_context\n    temp.write(Template(contents).render(**context))\n  File \"c:\\program files\\python 3.5\\lib\\tempfile.py\", line 482, in func_wrapper\n    return func(*args, **kwargs)\n  File \"c:\\program files\\python 3.5\\lib\\encodings\\cp1252.py\", line 19, in encode\n    return codecs.charmap_encode(input,self.errors,encoding_table)[0]\nUnicodeEncodeError: 'charmap' codec can't encode character '\\u0103' in position 1626: character maps to <undefined>\n```\n\ncookiecutter should pass `encoding='utf-8'` to `NamedTemporaryFile`\n\n", "before_files": [{"content": "#!/usr/bin/env python\n# -*- coding: utf-8 -*-\n\n\"\"\"\ncookiecutter.hooks\n------------------\n\nFunctions for discovering and executing various cookiecutter hooks.\n\"\"\"\n\nimport io\nimport logging\nimport os\nimport subprocess\nimport sys\nimport tempfile\n\nfrom jinja2 import Template\n\nfrom cookiecutter import utils\nfrom .exceptions import FailedHookException\n\n\n_HOOKS = [\n    'pre_gen_project',\n    'post_gen_project',\n    # TODO: other hooks should be listed here\n]\nEXIT_SUCCESS = 0\n\n\ndef find_hooks():\n    \"\"\"\n    Must be called with the project template as the current working directory.\n    Returns a dict of all hook scripts provided.\n    Dict's key will be the hook/script's name, without extension, while\n    values will be the absolute path to the script.\n    Missing scripts will not be included in the returned dict.\n    \"\"\"\n    hooks_dir = 'hooks'\n    r = {}\n    logging.debug('hooks_dir is {0}'.format(hooks_dir))\n    if not os.path.isdir(hooks_dir):\n        logging.debug('No hooks/ dir in template_dir')\n        return r\n    for f in os.listdir(hooks_dir):\n        basename = os.path.splitext(os.path.basename(f))[0]\n        if basename in _HOOKS:\n            r[basename] = os.path.abspath(os.path.join(hooks_dir, f))\n    return r\n\n\ndef run_script(script_path, cwd='.'):\n    \"\"\"\n    Executes a script from a working directory.\n\n    :param script_path: Absolute path to the script to run.\n    :param cwd: The directory to run the script from.\n    \"\"\"\n    run_thru_shell = sys.platform.startswith('win')\n    if script_path.endswith('.py'):\n        script_command = [sys.executable, script_path]\n    else:\n        script_command = [script_path]\n\n    utils.make_executable(script_path)\n\n    proc = subprocess.Popen(\n        script_command,\n        shell=run_thru_shell,\n        cwd=cwd\n    )\n    exit_status = proc.wait()\n    if exit_status != EXIT_SUCCESS:\n        raise FailedHookException(\n            \"Hook script failed (exit status: %d)\" % exit_status)\n\n\ndef run_script_with_context(script_path, cwd, context):\n    \"\"\"\n    Executes a script after rendering with it Jinja.\n\n    :param script_path: Absolute path to the script to run.\n    :param cwd: The directory to run the script from.\n    :param context: Cookiecutter project template context.\n    \"\"\"\n    _, extension = os.path.splitext(script_path)\n\n    contents = io.open(script_path, 'r', encoding='utf-8').read()\n\n    with tempfile.NamedTemporaryFile(\n        delete=False,\n        mode='w',\n        suffix=extension\n    ) as temp:\n        temp.write(Template(contents).render(**context))\n\n    run_script(temp.name, cwd)\n\n\ndef run_hook(hook_name, project_dir, context):\n    \"\"\"\n    Try to find and execute a hook from the specified project directory.\n\n    :param hook_name: The hook to execute.\n    :param project_dir: The directory to execute the script from.\n    :param context: Cookiecutter project context.\n    \"\"\"\n    script = find_hooks().get(hook_name)\n    if script is None:\n        logging.debug('No hooks found')\n        return\n    run_script_with_context(script, project_dir, context)\n", "path": "cookiecutter/hooks.py"}], "after_files": [{"content": "#!/usr/bin/env python\n# -*- coding: utf-8 -*-\n\n\"\"\"\ncookiecutter.hooks\n------------------\n\nFunctions for discovering and executing various cookiecutter hooks.\n\"\"\"\n\nimport io\nimport logging\nimport os\nimport subprocess\nimport sys\nimport tempfile\n\nfrom jinja2 import Template\n\nfrom cookiecutter import utils\nfrom .exceptions import FailedHookException\n\n\n_HOOKS = [\n    'pre_gen_project',\n    'post_gen_project',\n    # TODO: other hooks should be listed here\n]\nEXIT_SUCCESS = 0\n\n\ndef find_hooks():\n    \"\"\"\n    Must be called with the project template as the current working directory.\n    Returns a dict of all hook scripts provided.\n    Dict's key will be the hook/script's name, without extension, while\n    values will be the absolute path to the script.\n    Missing scripts will not be included in the returned dict.\n    \"\"\"\n    hooks_dir = 'hooks'\n    r = {}\n    logging.debug('hooks_dir is {0}'.format(hooks_dir))\n    if not os.path.isdir(hooks_dir):\n        logging.debug('No hooks/ dir in template_dir')\n        return r\n    for f in os.listdir(hooks_dir):\n        basename = os.path.splitext(os.path.basename(f))[0]\n        if basename in _HOOKS:\n            r[basename] = os.path.abspath(os.path.join(hooks_dir, f))\n    return r\n\n\ndef run_script(script_path, cwd='.'):\n    \"\"\"\n    Executes a script from a working directory.\n\n    :param script_path: Absolute path to the script to run.\n    :param cwd: The directory to run the script from.\n    \"\"\"\n    run_thru_shell = sys.platform.startswith('win')\n    if script_path.endswith('.py'):\n        script_command = [sys.executable, script_path]\n    else:\n        script_command = [script_path]\n\n    utils.make_executable(script_path)\n\n    proc = subprocess.Popen(\n        script_command,\n        shell=run_thru_shell,\n        cwd=cwd\n    )\n    exit_status = proc.wait()\n    if exit_status != EXIT_SUCCESS:\n        raise FailedHookException(\n            \"Hook script failed (exit status: %d)\" % exit_status)\n\n\ndef run_script_with_context(script_path, cwd, context):\n    \"\"\"\n    Executes a script after rendering with it Jinja.\n\n    :param script_path: Absolute path to the script to run.\n    :param cwd: The directory to run the script from.\n    :param context: Cookiecutter project template context.\n    \"\"\"\n    _, extension = os.path.splitext(script_path)\n\n    contents = io.open(script_path, 'r', encoding='utf-8').read()\n\n    with tempfile.NamedTemporaryFile(\n        delete=False,\n        mode='wb',\n        suffix=extension\n    ) as temp:\n        output = Template(contents).render(**context)\n        temp.write(output.encode('utf-8'))\n\n    run_script(temp.name, cwd)\n\n\ndef run_hook(hook_name, project_dir, context):\n    \"\"\"\n    Try to find and execute a hook from the specified project directory.\n\n    :param hook_name: The hook to execute.\n    :param project_dir: The directory to execute the script from.\n    :param context: Cookiecutter project context.\n    \"\"\"\n    script = find_hooks().get(hook_name)\n    if script is None:\n        logging.debug('No hooks found')\n        return\n    run_script_with_context(script, project_dir, context)\n", "path": "cookiecutter/hooks.py"}]}