prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  poetry shell does not activate virtualenv 
  <!-- Checked checkbox should look like this: [x] -->
  - [x] I am on the [latest](https://github.com/sdispater/poetry/releases/latest) Poetry version.
  - [x] I have searched the [issues](https://github.com/sdispater/poetry/issues) of this repo and believe that this is not a duplicate.
  
  <!--
    Once those are done, if you're able to fill in the following list with your information,
    it'd be very helpful to whoever handles the issue.
  -->
  
  - **OS version and name**: Mac OS X, High Sierra
  - **Poetry version**: 0.12.5
  
  ## Issue
  Similar to ```pipenv shell```, I would have expected that when running ```poetry shell``` the virtualenv gets activated, but apparently this is not the case...
  
  
  ```console
  ➜ which python
  /Users/timon/.pyenv/shims/python
  ➜ poetry shell
  Spawning shell within /Users/timon/Library/Caches/pypoetry/virtualenvs/YOLO-SAR-py3.7
  ➜ which python
  /Users/timon/.pyenv/shims/python
  ➜ source /Users/timon/Library/Caches/pypoetry/virtualenvs/yolo-sar-py3.7/bin/activate
  ➜ which python
  /Users/timon/Library/Caches/pypoetry/virtualenvs/yolo-sar-py3.7/bin/python
  ```
  
  for comparison
  ```console
  ➜ poetry run which python
  /Users/timon/Library/Caches/pypoetry/virtualenvs/yolo-sar-py3.7/bin/python
  ```
  
  
  Am I misunderstanding something and this is expected behaviour or is it a bug? 
  
  Thanks a lot already for your time :)
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `poetry/utils/shell.py`
  Content:
  ```
  1 import os
  2 
  3 from shellingham import detect_shell
  4 from shellingham import ShellDetectionFailure
  5 
  6 
  7 class Shell:
  8     """
  9     Represents the current shell.
  10     """
  11 
  12     _shell = None
  13 
  14     def __init__(self, name, path):  # type: (str, str) -> None
  15         self._name = name
  16         self._path = path
  17 
  18     @property
  19     def name(self):  # type: () -> str
  20         return self._name
  21 
  22     @property
  23     def path(self):  # type: () -> str
  24         return self._path
  25 
  26     @classmethod
  27     def get(cls):  # type: () -> Shell
  28         """
  29         Retrieve the current shell.
  30         """
  31         if cls._shell is not None:
  32             return cls._shell
  33 
  34         try:
  35             name, path = detect_shell(os.getpid())
  36         except (RuntimeError, ShellDetectionFailure):
  37             raise RuntimeError("Unable to detect the current shell.")
  38 
  39         cls._shell = cls(name, path)
  40 
  41         return cls._shell
  42 
  43     def __repr__(self):  # type: () -> str
  44         return '{}("{}", "{}")'.format(self.__class__.__name__, self._name, self._path)
  45
  ```
  
  Path: `poetry/console/commands/shell.py`
  Content:
  ```
  1 import sys
  2 
  3 from os import environ
  4 from distutils.util import strtobool
  5 
  6 from .env_command import EnvCommand
  7 
  8 
  9 class ShellCommand(EnvCommand):
  10 
  11     name = "shell"
  12     description = "Spawns a shell within the virtual environment."
  13 
  14     help = """The <info>shell</> command spawns a shell, according to the
  15 <comment>$SHELL</> environment variable, within the virtual environment.
  16 If one doesn't exist yet, it will be created.
  17 """
  18 
  19     def handle(self):
  20         from poetry.utils.shell import Shell
  21 
  22         # Check if it's already activated or doesn't exist and won't be created
  23         venv_activated = strtobool(environ.get("POETRY_ACTIVE", "0")) or getattr(
  24             sys, "real_prefix", sys.prefix
  25         ) == str(self.env.path)
  26         if venv_activated:
  27             self.line(
  28                 "Virtual environment already activated: "
  29                 "<info>{}</>".format(self.env.path)
  30             )
  31 
  32             return
  33 
  34         self.line("Spawning shell within <info>{}</>".format(self.env.path))
  35 
  36         # Setting this to avoid spawning unnecessary nested shells
  37         environ["POETRY_ACTIVE"] = "1"
  38         shell = Shell.get()
  39         self.env.execute(shell.path)
  40         environ.pop("POETRY_ACTIVE")
  41
  ```
  --- 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/poetry/console/commands/shell.py b/poetry/console/commands/shell.py
  --- a/poetry/console/commands/shell.py
  +++ b/poetry/console/commands/shell.py
  @@ -36,5 +36,5 @@
           # Setting this to avoid spawning unnecessary nested shells
           environ["POETRY_ACTIVE"] = "1"
           shell = Shell.get()
  -        self.env.execute(shell.path)
  +        shell.activate(self.env)
           environ.pop("POETRY_ACTIVE")
  diff --git a/poetry/utils/shell.py b/poetry/utils/shell.py
  --- a/poetry/utils/shell.py
  +++ b/poetry/utils/shell.py
  @@ -1,8 +1,16 @@
   import os
  +import signal
  +import sys
   
  +import pexpect
  +
  +from clikit.utils.terminal import Terminal
   from shellingham import detect_shell
   from shellingham import ShellDetectionFailure
   
  +from ._compat import WINDOWS
  +from .env import VirtualEnv
  +
   
   class Shell:
       """
  @@ -40,5 +48,51 @@
   
           return cls._shell
   
  +    def activate(self, env):  # type: (VirtualEnv) -> None
  +        if WINDOWS:
  +            return env.execute(self.path)
  +
  +        terminal = Terminal()
  +        with env.temp_environ():
  +            c = pexpect.spawn(
  +                self._path, ["-i"], dimensions=(terminal.height, terminal.width)
  +            )
  +
  +        c.setecho(False)
  +        activate_script = self._get_activate_script()
  +        bin_dir = "Scripts" if WINDOWS else "bin"
  +        activate_path = env.path / bin_dir / activate_script
  +        c.sendline("{} {}".format(self._get_source_command(), activate_path))
  +
  +        def resize(sig, data):
  +            terminal = Terminal()
  +            c.setwinsize(terminal.height, terminal.width)
  +
  +        signal.signal(signal.SIGWINCH, resize)
  +
  +        # Interact with the new shell.
  +        c.interact(escape_character=None)
  +        c.close()
  +
  +        sys.exit(c.exitstatus)
  +
  +    def _get_activate_script(self):
  +        if "fish" == self._name:
  +            suffix = ".fish"
  +        elif "csh" == self._name:
  +            suffix = ".csh"
  +        else:
  +            suffix = ""
  +
  +        return "activate" + suffix
  +
  +    def _get_source_command(self):
  +        if "fish" == self._name:
  +            return "source"
  +        elif "csh" == self._name:
  +            return "source"
  +
  +        return "."
  +
       def __repr__(self):  # type: () -> str
           return '{}("{}", "{}")'.format(self.__class__.__name__, self._name, self._path)
  
verification_info: {"golden_diff": "diff --git a/poetry/console/commands/shell.py b/poetry/console/commands/shell.py\n--- a/poetry/console/commands/shell.py\n+++ b/poetry/console/commands/shell.py\n@@ -36,5 +36,5 @@\n         # Setting this to avoid spawning unnecessary nested shells\n         environ[\"POETRY_ACTIVE\"] = \"1\"\n         shell = Shell.get()\n-        self.env.execute(shell.path)\n+        shell.activate(self.env)\n         environ.pop(\"POETRY_ACTIVE\")\ndiff --git a/poetry/utils/shell.py b/poetry/utils/shell.py\n--- a/poetry/utils/shell.py\n+++ b/poetry/utils/shell.py\n@@ -1,8 +1,16 @@\n import os\n+import signal\n+import sys\n \n+import pexpect\n+\n+from clikit.utils.terminal import Terminal\n from shellingham import detect_shell\n from shellingham import ShellDetectionFailure\n \n+from ._compat import WINDOWS\n+from .env import VirtualEnv\n+\n \n class Shell:\n     \"\"\"\n@@ -40,5 +48,51 @@\n \n         return cls._shell\n \n+    def activate(self, env):  # type: (VirtualEnv) -> None\n+        if WINDOWS:\n+            return env.execute(self.path)\n+\n+        terminal = Terminal()\n+        with env.temp_environ():\n+            c = pexpect.spawn(\n+                self._path, [\"-i\"], dimensions=(terminal.height, terminal.width)\n+            )\n+\n+        c.setecho(False)\n+        activate_script = self._get_activate_script()\n+        bin_dir = \"Scripts\" if WINDOWS else \"bin\"\n+        activate_path = env.path / bin_dir / activate_script\n+        c.sendline(\"{} {}\".format(self._get_source_command(), activate_path))\n+\n+        def resize(sig, data):\n+            terminal = Terminal()\n+            c.setwinsize(terminal.height, terminal.width)\n+\n+        signal.signal(signal.SIGWINCH, resize)\n+\n+        # Interact with the new shell.\n+        c.interact(escape_character=None)\n+        c.close()\n+\n+        sys.exit(c.exitstatus)\n+\n+    def _get_activate_script(self):\n+        if \"fish\" == self._name:\n+            suffix = \".fish\"\n+        elif \"csh\" == self._name:\n+            suffix = \".csh\"\n+        else:\n+            suffix = \"\"\n+\n+        return \"activate\" + suffix\n+\n+    def _get_source_command(self):\n+        if \"fish\" == self._name:\n+            return \"source\"\n+        elif \"csh\" == self._name:\n+            return \"source\"\n+\n+        return \".\"\n+\n     def __repr__(self):  # type: () -> str\n         return '{}(\"{}\", \"{}\")'.format(self.__class__.__name__, self._name, self._path)\n", "issue": "poetry shell does not activate virtualenv \n<!-- Checked checkbox should look like this: [x] -->\r\n- [x] I am on the [latest](https://github.com/sdispater/poetry/releases/latest) Poetry version.\r\n- [x] I have searched the [issues](https://github.com/sdispater/poetry/issues) of this repo and believe that this is not a duplicate.\r\n\r\n<!--\r\n  Once those are done, if you're able to fill in the following list with your information,\r\n  it'd be very helpful to whoever handles the issue.\r\n-->\r\n\r\n- **OS version and name**: Mac OS X, High Sierra\r\n- **Poetry version**: 0.12.5\r\n\r\n## Issue\r\nSimilar to ```pipenv shell```, I would have expected that when running ```poetry shell``` the virtualenv gets activated, but apparently this is not the case...\r\n\r\n\r\n```console\r\n\u279c which python\r\n/Users/timon/.pyenv/shims/python\r\n\u279c poetry shell\r\nSpawning shell within /Users/timon/Library/Caches/pypoetry/virtualenvs/YOLO-SAR-py3.7\r\n\u279c which python\r\n/Users/timon/.pyenv/shims/python\r\n\u279c source /Users/timon/Library/Caches/pypoetry/virtualenvs/yolo-sar-py3.7/bin/activate\r\n\u279c which python\r\n/Users/timon/Library/Caches/pypoetry/virtualenvs/yolo-sar-py3.7/bin/python\r\n```\r\n\r\nfor comparison\r\n```console\r\n\u279c poetry run which python\r\n/Users/timon/Library/Caches/pypoetry/virtualenvs/yolo-sar-py3.7/bin/python\r\n```\r\n\r\n\r\nAm I misunderstanding something and this is expected behaviour or is it a bug? \r\n\r\nThanks a lot already for your time :)\n", "before_files": [{"content": "import os\n\nfrom shellingham import detect_shell\nfrom shellingham import ShellDetectionFailure\n\n\nclass Shell:\n    \"\"\"\n    Represents the current shell.\n    \"\"\"\n\n    _shell = None\n\n    def __init__(self, name, path):  # type: (str, str) -> None\n        self._name = name\n        self._path = path\n\n    @property\n    def name(self):  # type: () -> str\n        return self._name\n\n    @property\n    def path(self):  # type: () -> str\n        return self._path\n\n    @classmethod\n    def get(cls):  # type: () -> Shell\n        \"\"\"\n        Retrieve the current shell.\n        \"\"\"\n        if cls._shell is not None:\n            return cls._shell\n\n        try:\n            name, path = detect_shell(os.getpid())\n        except (RuntimeError, ShellDetectionFailure):\n            raise RuntimeError(\"Unable to detect the current shell.\")\n\n        cls._shell = cls(name, path)\n\n        return cls._shell\n\n    def __repr__(self):  # type: () -> str\n        return '{}(\"{}\", \"{}\")'.format(self.__class__.__name__, self._name, self._path)\n", "path": "poetry/utils/shell.py"}, {"content": "import sys\n\nfrom os import environ\nfrom distutils.util import strtobool\n\nfrom .env_command import EnvCommand\n\n\nclass ShellCommand(EnvCommand):\n\n    name = \"shell\"\n    description = \"Spawns a shell within the virtual environment.\"\n\n    help = \"\"\"The <info>shell</> command spawns a shell, according to the\n<comment>$SHELL</> environment variable, within the virtual environment.\nIf one doesn't exist yet, it will be created.\n\"\"\"\n\n    def handle(self):\n        from poetry.utils.shell import Shell\n\n        # Check if it's already activated or doesn't exist and won't be created\n        venv_activated = strtobool(environ.get(\"POETRY_ACTIVE\", \"0\")) or getattr(\n            sys, \"real_prefix\", sys.prefix\n        ) == str(self.env.path)\n        if venv_activated:\n            self.line(\n                \"Virtual environment already activated: \"\n                \"<info>{}</>\".format(self.env.path)\n            )\n\n            return\n\n        self.line(\"Spawning shell within <info>{}</>\".format(self.env.path))\n\n        # Setting this to avoid spawning unnecessary nested shells\n        environ[\"POETRY_ACTIVE\"] = \"1\"\n        shell = Shell.get()\n        self.env.execute(shell.path)\n        environ.pop(\"POETRY_ACTIVE\")\n", "path": "poetry/console/commands/shell.py"}], "after_files": [{"content": "import os\nimport signal\nimport sys\n\nimport pexpect\n\nfrom clikit.utils.terminal import Terminal\nfrom shellingham import detect_shell\nfrom shellingham import ShellDetectionFailure\n\nfrom ._compat import WINDOWS\nfrom .env import VirtualEnv\n\n\nclass Shell:\n    \"\"\"\n    Represents the current shell.\n    \"\"\"\n\n    _shell = None\n\n    def __init__(self, name, path):  # type: (str, str) -> None\n        self._name = name\n        self._path = path\n\n    @property\n    def name(self):  # type: () -> str\n        return self._name\n\n    @property\n    def path(self):  # type: () -> str\n        return self._path\n\n    @classmethod\n    def get(cls):  # type: () -> Shell\n        \"\"\"\n        Retrieve the current shell.\n        \"\"\"\n        if cls._shell is not None:\n            return cls._shell\n\n        try:\n            name, path = detect_shell(os.getpid())\n        except (RuntimeError, ShellDetectionFailure):\n            raise RuntimeError(\"Unable to detect the current shell.\")\n\n        cls._shell = cls(name, path)\n\n        return cls._shell\n\n    def activate(self, env):  # type: (VirtualEnv) -> None\n        if WINDOWS:\n            return env.execute(self.path)\n\n        terminal = Terminal()\n        with env.temp_environ():\n            c = pexpect.spawn(\n                self._path, [\"-i\"], dimensions=(terminal.height, terminal.width)\n            )\n\n        c.setecho(False)\n        activate_script = self._get_activate_script()\n        bin_dir = \"Scripts\" if WINDOWS else \"bin\"\n        activate_path = env.path / bin_dir / activate_script\n        c.sendline(\"{} {}\".format(self._get_source_command(), activate_path))\n\n        def resize(sig, data):\n            terminal = Terminal()\n            c.setwinsize(terminal.height, terminal.width)\n\n        signal.signal(signal.SIGWINCH, resize)\n\n        # Interact with the new shell.\n        c.interact(escape_character=None)\n        c.close()\n\n        sys.exit(c.exitstatus)\n\n    def _get_activate_script(self):\n        if \"fish\" == self._name:\n            suffix = \".fish\"\n        elif \"csh\" == self._name:\n            suffix = \".csh\"\n        else:\n            suffix = \"\"\n\n        return \"activate\" + suffix\n\n    def _get_source_command(self):\n        if \"fish\" == self._name:\n            return \"source\"\n        elif \"csh\" == self._name:\n            return \"source\"\n\n        return \".\"\n\n    def __repr__(self):  # type: () -> str\n        return '{}(\"{}\", \"{}\")'.format(self.__class__.__name__, self._name, self._path)\n", "path": "poetry/utils/shell.py"}, {"content": "import sys\n\nfrom os import environ\nfrom distutils.util import strtobool\n\nfrom .env_command import EnvCommand\n\n\nclass ShellCommand(EnvCommand):\n\n    name = \"shell\"\n    description = \"Spawns a shell within the virtual environment.\"\n\n    help = \"\"\"The <info>shell</> command spawns a shell, according to the\n<comment>$SHELL</> environment variable, within the virtual environment.\nIf one doesn't exist yet, it will be created.\n\"\"\"\n\n    def handle(self):\n        from poetry.utils.shell import Shell\n\n        # Check if it's already activated or doesn't exist and won't be created\n        venv_activated = strtobool(environ.get(\"POETRY_ACTIVE\", \"0\")) or getattr(\n            sys, \"real_prefix\", sys.prefix\n        ) == str(self.env.path)\n        if venv_activated:\n            self.line(\n                \"Virtual environment already activated: \"\n                \"<info>{}</>\".format(self.env.path)\n            )\n\n            return\n\n        self.line(\"Spawning shell within <info>{}</>\".format(self.env.path))\n\n        # Setting this to avoid spawning unnecessary nested shells\n        environ[\"POETRY_ACTIVE\"] = \"1\"\n        shell = Shell.get()\n        shell.activate(self.env)\n        environ.pop(\"POETRY_ACTIVE\")\n", "path": "poetry/console/commands/shell.py"}]}