prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  activate_this.py failed for python2 virtualenvs
  **Issue**
  
  It seems recently pipenv introduced a new type of activate_this.py. On windows the content of activate_this.py has something like this:
  ```
  prev_length = len(sys.path)
  for lib in "'..\\Lib\\site-packages".split(os.pathsep):
      path = os.path.realpath(os.path.join(bin_dir, lib))
      site.addsitedir(path.decode("utf-8") if "'yes" else path)
  sys.path[:] = sys.path[prev_length:] + sys.path[0:prev_length]
  ```
  As you can see the "'..\\Lib\\site-packages" is obviously wrong.
  
  **Environment**
  
  Provide at least:
  - OS: Windows 10
  - ``pip list`` of the host python where ``virtualenv`` is installed:
  
    ```console
    virtualenv          20.0.18
    virtualenv-clone    0.5.4
    ```
  
  **Output of the virtual environment creation**
  
  As I'm using virtualenv through pipenv, so I failed to grab the virtualenv output
  
  ```
  [  ==] Creating virtual environment...created virtual environment CPython2.7.17.final.0-64 in 641ms
  
    creator CPython2Windows(dest=C:\Users\win10\.virtualenvs\win10-obmjl69F, clear=False, global=False)
  
    seeder FromAppData(download=False, pip=latest, setuptools=latest, wheel=latest, via=copy, app_data_dir=C:\Users\win10\AppData\Local\pypa\virtualenv\seed-app-data\v1.0.1)
  
    activators BashActivator,BatchActivator,FishActivator,PowerShellActivator,PythonActivator
  ```
  
  However I've located the related code and wrote out its runtime variable information:
  The following is the output of _repr_unicode function in ```src/virtualenv/activation/python/__init__.py```
  
  ```
  '(win10) 
  'C:\\Users\\win10\\.virtualenvs\\win10-obmjl69F
  'win10-obmjl69F
  'Scripts
  ';
  '..\\Lib\\site-packages
  'yes
  ```
  As you can see, there's an additional ' before each item. I've done a small experiment on python 3.6 and 3.7:
  ```
  >>> value = "..\\123456"
  >>> repr(value.encode("utf-8"))
  "b'..\\\\123456'"
  >>> repr(value.encode("utf-8"))[1:-1]
  "'..\\\\123456"
  >>>
  ```
  I believe there's something wrong with this function. This function is introduced in PR #1503 
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `src/virtualenv/activation/python/__init__.py`
  Content:
  ```
  1 from __future__ import absolute_import, unicode_literals
  2 
  3 import os
  4 from collections import OrderedDict
  5 
  6 from virtualenv.util.path import Path
  7 from virtualenv.util.six import ensure_text
  8 
  9 from ..via_template import ViaTemplateActivator
  10 
  11 
  12 class PythonActivator(ViaTemplateActivator):
  13     def templates(self):
  14         yield Path("activate_this.py")
  15 
  16     def replacements(self, creator, dest_folder):
  17         replacements = super(PythonActivator, self).replacements(creator, dest_folder)
  18         lib_folders = OrderedDict((os.path.relpath(str(i), str(dest_folder)), None) for i in creator.libs)
  19         win_py2 = creator.interpreter.platform == "win32" and creator.interpreter.version_info.major == 2
  20         replacements.update(
  21             {
  22                 "__LIB_FOLDERS__": ensure_text(os.pathsep.join(lib_folders.keys())),
  23                 "__DECODE_PATH__": ("yes" if win_py2 else ""),
  24             }
  25         )
  26         return replacements
  27 
  28     @staticmethod
  29     def _repr_unicode(creator, value):
  30         py2 = creator.interpreter.version_info.major == 2
  31         if py2:  # on Python 2 we need to encode this into explicit utf-8, py3 supports unicode literals
  32             value = ensure_text(repr(value.encode("utf-8"))[1:-1])
  33         return value
  34
  ```
  
  Path: `src/virtualenv/activation/via_template.py`
  Content:
  ```
  1 from __future__ import absolute_import, unicode_literals
  2 
  3 import os
  4 import sys
  5 from abc import ABCMeta, abstractmethod
  6 
  7 from six import add_metaclass
  8 
  9 from virtualenv.util.six import ensure_text
  10 
  11 from .activator import Activator
  12 
  13 if sys.version_info >= (3, 7):
  14     from importlib.resources import read_text
  15 else:
  16     from importlib_resources import read_text
  17 
  18 
  19 @add_metaclass(ABCMeta)
  20 class ViaTemplateActivator(Activator):
  21     @abstractmethod
  22     def templates(self):
  23         raise NotImplementedError
  24 
  25     def generate(self, creator):
  26         dest_folder = creator.bin_dir
  27         replacements = self.replacements(creator, dest_folder)
  28         self._generate(replacements, self.templates(), dest_folder, creator)
  29         if self.flag_prompt is not None:
  30             creator.pyenv_cfg["prompt"] = self.flag_prompt
  31 
  32     def replacements(self, creator, dest_folder):
  33         return {
  34             "__VIRTUAL_PROMPT__": "" if self.flag_prompt is None else self.flag_prompt,
  35             "__VIRTUAL_ENV__": ensure_text(str(creator.dest)),
  36             "__VIRTUAL_NAME__": creator.env_name,
  37             "__BIN_NAME__": ensure_text(str(creator.bin_dir.relative_to(creator.dest))),
  38             "__PATH_SEP__": ensure_text(os.pathsep),
  39         }
  40 
  41     def _generate(self, replacements, templates, to_folder, creator):
  42         for template in templates:
  43             text = self.instantiate_template(replacements, template, creator)
  44             dest = to_folder / self.as_name(template)
  45             dest.write_text(text, encoding="utf-8")
  46 
  47     def as_name(self, template):
  48         return template.name
  49 
  50     def instantiate_template(self, replacements, template, creator):
  51         # read text and do replacements
  52         text = read_text(self.__module__, str(template), encoding="utf-8", errors="strict")
  53         for key, value in replacements.items():
  54             value = self._repr_unicode(creator, value)
  55             text = text.replace(key, value)
  56         return text
  57 
  58     @staticmethod
  59     def _repr_unicode(creator, value):
  60         # by default we just let it be unicode
  61         return value
  62
  ```
  --- 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/src/virtualenv/activation/python/__init__.py b/src/virtualenv/activation/python/__init__.py
  --- a/src/virtualenv/activation/python/__init__.py
  +++ b/src/virtualenv/activation/python/__init__.py
  @@ -1,6 +1,7 @@
   from __future__ import absolute_import, unicode_literals
   
   import os
  +import sys
   from collections import OrderedDict
   
   from virtualenv.util.path import Path
  @@ -29,5 +30,6 @@
       def _repr_unicode(creator, value):
           py2 = creator.interpreter.version_info.major == 2
           if py2:  # on Python 2 we need to encode this into explicit utf-8, py3 supports unicode literals
  -            value = ensure_text(repr(value.encode("utf-8"))[1:-1])
  +            start = 2 if sys.version_info[0] == 3 else 1
  +            value = ensure_text(repr(value.encode("utf-8"))[start:-1])
           return value
  diff --git a/src/virtualenv/activation/via_template.py b/src/virtualenv/activation/via_template.py
  --- a/src/virtualenv/activation/via_template.py
  +++ b/src/virtualenv/activation/via_template.py
  @@ -25,9 +25,10 @@
       def generate(self, creator):
           dest_folder = creator.bin_dir
           replacements = self.replacements(creator, dest_folder)
  -        self._generate(replacements, self.templates(), dest_folder, creator)
  +        generated = self._generate(replacements, self.templates(), dest_folder, creator)
           if self.flag_prompt is not None:
               creator.pyenv_cfg["prompt"] = self.flag_prompt
  +        return generated
   
       def replacements(self, creator, dest_folder):
           return {
  @@ -39,10 +40,13 @@
           }
   
       def _generate(self, replacements, templates, to_folder, creator):
  +        generated = []
           for template in templates:
               text = self.instantiate_template(replacements, template, creator)
               dest = to_folder / self.as_name(template)
               dest.write_text(text, encoding="utf-8")
  +            generated.append(dest)
  +        return generated
   
       def as_name(self, template):
           return template.name
  
verification_info: {"golden_diff": "diff --git a/src/virtualenv/activation/python/__init__.py b/src/virtualenv/activation/python/__init__.py\n--- a/src/virtualenv/activation/python/__init__.py\n+++ b/src/virtualenv/activation/python/__init__.py\n@@ -1,6 +1,7 @@\n from __future__ import absolute_import, unicode_literals\n \n import os\n+import sys\n from collections import OrderedDict\n \n from virtualenv.util.path import Path\n@@ -29,5 +30,6 @@\n     def _repr_unicode(creator, value):\n         py2 = creator.interpreter.version_info.major == 2\n         if py2:  # on Python 2 we need to encode this into explicit utf-8, py3 supports unicode literals\n-            value = ensure_text(repr(value.encode(\"utf-8\"))[1:-1])\n+            start = 2 if sys.version_info[0] == 3 else 1\n+            value = ensure_text(repr(value.encode(\"utf-8\"))[start:-1])\n         return value\ndiff --git a/src/virtualenv/activation/via_template.py b/src/virtualenv/activation/via_template.py\n--- a/src/virtualenv/activation/via_template.py\n+++ b/src/virtualenv/activation/via_template.py\n@@ -25,9 +25,10 @@\n     def generate(self, creator):\n         dest_folder = creator.bin_dir\n         replacements = self.replacements(creator, dest_folder)\n-        self._generate(replacements, self.templates(), dest_folder, creator)\n+        generated = self._generate(replacements, self.templates(), dest_folder, creator)\n         if self.flag_prompt is not None:\n             creator.pyenv_cfg[\"prompt\"] = self.flag_prompt\n+        return generated\n \n     def replacements(self, creator, dest_folder):\n         return {\n@@ -39,10 +40,13 @@\n         }\n \n     def _generate(self, replacements, templates, to_folder, creator):\n+        generated = []\n         for template in templates:\n             text = self.instantiate_template(replacements, template, creator)\n             dest = to_folder / self.as_name(template)\n             dest.write_text(text, encoding=\"utf-8\")\n+            generated.append(dest)\n+        return generated\n \n     def as_name(self, template):\n         return template.name\n", "issue": "activate_this.py failed for python2 virtualenvs\n**Issue**\r\n\r\nIt seems recently pipenv introduced a new type of activate_this.py. On windows the content of activate_this.py has something like this:\r\n```\r\nprev_length = len(sys.path)\r\nfor lib in \"'..\\\\Lib\\\\site-packages\".split(os.pathsep):\r\n    path = os.path.realpath(os.path.join(bin_dir, lib))\r\n    site.addsitedir(path.decode(\"utf-8\") if \"'yes\" else path)\r\nsys.path[:] = sys.path[prev_length:] + sys.path[0:prev_length]\r\n```\r\nAs you can see the \"'..\\\\Lib\\\\site-packages\" is obviously wrong.\r\n\r\n**Environment**\r\n\r\nProvide at least:\r\n- OS: Windows 10\r\n- ``pip list`` of the host python where ``virtualenv`` is installed:\r\n\r\n  ```console\r\n  virtualenv          20.0.18\r\n  virtualenv-clone    0.5.4\r\n  ```\r\n\r\n**Output of the virtual environment creation**\r\n\r\nAs I'm using virtualenv through pipenv, so I failed to grab the virtualenv output\r\n\r\n```\r\n[  ==] Creating virtual environment...created virtual environment CPython2.7.17.final.0-64 in 641ms\r\n\r\n  creator CPython2Windows(dest=C:\\Users\\win10\\.virtualenvs\\win10-obmjl69F, clear=False, global=False)\r\n\r\n  seeder FromAppData(download=False, pip=latest, setuptools=latest, wheel=latest, via=copy, app_data_dir=C:\\Users\\win10\\AppData\\Local\\pypa\\virtualenv\\seed-app-data\\v1.0.1)\r\n\r\n  activators BashActivator,BatchActivator,FishActivator,PowerShellActivator,PythonActivator\r\n```\r\n\r\nHowever I've located the related code and wrote out its runtime variable information:\r\nThe following is the output of _repr_unicode function in ```src/virtualenv/activation/python/__init__.py```\r\n\r\n```\r\n'(win10) \r\n'C:\\\\Users\\\\win10\\\\.virtualenvs\\\\win10-obmjl69F\r\n'win10-obmjl69F\r\n'Scripts\r\n';\r\n'..\\\\Lib\\\\site-packages\r\n'yes\r\n```\r\nAs you can see, there's an additional ' before each item. I've done a small experiment on python 3.6 and 3.7:\r\n```\r\n>>> value = \"..\\\\123456\"\r\n>>> repr(value.encode(\"utf-8\"))\r\n\"b'..\\\\\\\\123456'\"\r\n>>> repr(value.encode(\"utf-8\"))[1:-1]\r\n\"'..\\\\\\\\123456\"\r\n>>>\r\n```\r\nI believe there's something wrong with this function. This function is introduced in PR #1503 \n", "before_files": [{"content": "from __future__ import absolute_import, unicode_literals\n\nimport os\nfrom collections import OrderedDict\n\nfrom virtualenv.util.path import Path\nfrom virtualenv.util.six import ensure_text\n\nfrom ..via_template import ViaTemplateActivator\n\n\nclass PythonActivator(ViaTemplateActivator):\n    def templates(self):\n        yield Path(\"activate_this.py\")\n\n    def replacements(self, creator, dest_folder):\n        replacements = super(PythonActivator, self).replacements(creator, dest_folder)\n        lib_folders = OrderedDict((os.path.relpath(str(i), str(dest_folder)), None) for i in creator.libs)\n        win_py2 = creator.interpreter.platform == \"win32\" and creator.interpreter.version_info.major == 2\n        replacements.update(\n            {\n                \"__LIB_FOLDERS__\": ensure_text(os.pathsep.join(lib_folders.keys())),\n                \"__DECODE_PATH__\": (\"yes\" if win_py2 else \"\"),\n            }\n        )\n        return replacements\n\n    @staticmethod\n    def _repr_unicode(creator, value):\n        py2 = creator.interpreter.version_info.major == 2\n        if py2:  # on Python 2 we need to encode this into explicit utf-8, py3 supports unicode literals\n            value = ensure_text(repr(value.encode(\"utf-8\"))[1:-1])\n        return value\n", "path": "src/virtualenv/activation/python/__init__.py"}, {"content": "from __future__ import absolute_import, unicode_literals\n\nimport os\nimport sys\nfrom abc import ABCMeta, abstractmethod\n\nfrom six import add_metaclass\n\nfrom virtualenv.util.six import ensure_text\n\nfrom .activator import Activator\n\nif sys.version_info >= (3, 7):\n    from importlib.resources import read_text\nelse:\n    from importlib_resources import read_text\n\n\n@add_metaclass(ABCMeta)\nclass ViaTemplateActivator(Activator):\n    @abstractmethod\n    def templates(self):\n        raise NotImplementedError\n\n    def generate(self, creator):\n        dest_folder = creator.bin_dir\n        replacements = self.replacements(creator, dest_folder)\n        self._generate(replacements, self.templates(), dest_folder, creator)\n        if self.flag_prompt is not None:\n            creator.pyenv_cfg[\"prompt\"] = self.flag_prompt\n\n    def replacements(self, creator, dest_folder):\n        return {\n            \"__VIRTUAL_PROMPT__\": \"\" if self.flag_prompt is None else self.flag_prompt,\n            \"__VIRTUAL_ENV__\": ensure_text(str(creator.dest)),\n            \"__VIRTUAL_NAME__\": creator.env_name,\n            \"__BIN_NAME__\": ensure_text(str(creator.bin_dir.relative_to(creator.dest))),\n            \"__PATH_SEP__\": ensure_text(os.pathsep),\n        }\n\n    def _generate(self, replacements, templates, to_folder, creator):\n        for template in templates:\n            text = self.instantiate_template(replacements, template, creator)\n            dest = to_folder / self.as_name(template)\n            dest.write_text(text, encoding=\"utf-8\")\n\n    def as_name(self, template):\n        return template.name\n\n    def instantiate_template(self, replacements, template, creator):\n        # read text and do replacements\n        text = read_text(self.__module__, str(template), encoding=\"utf-8\", errors=\"strict\")\n        for key, value in replacements.items():\n            value = self._repr_unicode(creator, value)\n            text = text.replace(key, value)\n        return text\n\n    @staticmethod\n    def _repr_unicode(creator, value):\n        # by default we just let it be unicode\n        return value\n", "path": "src/virtualenv/activation/via_template.py"}], "after_files": [{"content": "from __future__ import absolute_import, unicode_literals\n\nimport os\nimport sys\nfrom collections import OrderedDict\n\nfrom virtualenv.util.path import Path\nfrom virtualenv.util.six import ensure_text\n\nfrom ..via_template import ViaTemplateActivator\n\n\nclass PythonActivator(ViaTemplateActivator):\n    def templates(self):\n        yield Path(\"activate_this.py\")\n\n    def replacements(self, creator, dest_folder):\n        replacements = super(PythonActivator, self).replacements(creator, dest_folder)\n        lib_folders = OrderedDict((os.path.relpath(str(i), str(dest_folder)), None) for i in creator.libs)\n        win_py2 = creator.interpreter.platform == \"win32\" and creator.interpreter.version_info.major == 2\n        replacements.update(\n            {\n                \"__LIB_FOLDERS__\": ensure_text(os.pathsep.join(lib_folders.keys())),\n                \"__DECODE_PATH__\": (\"yes\" if win_py2 else \"\"),\n            }\n        )\n        return replacements\n\n    @staticmethod\n    def _repr_unicode(creator, value):\n        py2 = creator.interpreter.version_info.major == 2\n        if py2:  # on Python 2 we need to encode this into explicit utf-8, py3 supports unicode literals\n            start = 2 if sys.version_info[0] == 3 else 1\n            value = ensure_text(repr(value.encode(\"utf-8\"))[start:-1])\n        return value\n", "path": "src/virtualenv/activation/python/__init__.py"}, {"content": "from __future__ import absolute_import, unicode_literals\n\nimport os\nimport sys\nfrom abc import ABCMeta, abstractmethod\n\nfrom six import add_metaclass\n\nfrom virtualenv.util.six import ensure_text\n\nfrom .activator import Activator\n\nif sys.version_info >= (3, 7):\n    from importlib.resources import read_text\nelse:\n    from importlib_resources import read_text\n\n\n@add_metaclass(ABCMeta)\nclass ViaTemplateActivator(Activator):\n    @abstractmethod\n    def templates(self):\n        raise NotImplementedError\n\n    def generate(self, creator):\n        dest_folder = creator.bin_dir\n        replacements = self.replacements(creator, dest_folder)\n        generated = self._generate(replacements, self.templates(), dest_folder, creator)\n        if self.flag_prompt is not None:\n            creator.pyenv_cfg[\"prompt\"] = self.flag_prompt\n        return generated\n\n    def replacements(self, creator, dest_folder):\n        return {\n            \"__VIRTUAL_PROMPT__\": \"\" if self.flag_prompt is None else self.flag_prompt,\n            \"__VIRTUAL_ENV__\": ensure_text(str(creator.dest)),\n            \"__VIRTUAL_NAME__\": creator.env_name,\n            \"__BIN_NAME__\": ensure_text(str(creator.bin_dir.relative_to(creator.dest))),\n            \"__PATH_SEP__\": ensure_text(os.pathsep),\n        }\n\n    def _generate(self, replacements, templates, to_folder, creator):\n        generated = []\n        for template in templates:\n            text = self.instantiate_template(replacements, template, creator)\n            dest = to_folder / self.as_name(template)\n            dest.write_text(text, encoding=\"utf-8\")\n            generated.append(dest)\n        return generated\n\n    def as_name(self, template):\n        return template.name\n\n    def instantiate_template(self, replacements, template, creator):\n        # read text and do replacements\n        text = read_text(self.__module__, str(template), encoding=\"utf-8\", errors=\"strict\")\n        for key, value in replacements.items():\n            value = self._repr_unicode(creator, value)\n            text = text.replace(key, value)\n        return text\n\n    @staticmethod\n    def _repr_unicode(creator, value):\n        # by default we just let it be unicode\n        return value\n", "path": "src/virtualenv/activation/via_template.py"}]}