prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  `escalation.belongs_to` cache is broken
  The problem is that the cache is checked with the incoming string type, but [the string type changes](https://github.com/HypothesisWorks/hypothesis-python/blob/3.44.1/src/hypothesis/internal/escalation.py#L41) before the value is inserted:
  
  A simple (but not elegant) fix:
  
  ```diff
  --- hypothesis/internal/escalation.py
  +++ hypothesis/internal/escalation.py
  @@ -34,13 +34,14 @@ def belongs_to(package):
       cache = {text_type: {}, binary_type: {}}
   
       def accept(filepath):
  +        ftype = type(filepath)
           try:
  -            return cache[type(filepath)][filepath]
  +            return cache[ftype][filepath]
           except KeyError:
               pass
           filepath = encoded_filepath(filepath)
           result = os.path.abspath(filepath).startswith(root)
  -        cache[type(filepath)][filepath] = result
  +        cache[ftype][filepath] = result
           return result
       accept.__name__ = 'is_%s_file' % (package.__name__,)
       return accept
  ```
  
  --- 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/hypothesis/internal/escalation.py`
  Content:
  ```
  1 # coding=utf-8
  2 #
  3 # This file is part of Hypothesis, which may be found at
  4 # https://github.com/HypothesisWorks/hypothesis-python
  5 #
  6 # Most of this work is copyright (C) 2013-2017 David R. MacIver
  7 # (david@drmaciver.com), but it contains contributions by others. See
  8 # CONTRIBUTING.rst for a full list of people who may hold copyright, and
  9 # consult the git log if you need to determine who owns an individual
  10 # contribution.
  11 #
  12 # This Source Code Form is subject to the terms of the Mozilla Public License,
  13 # v. 2.0. If a copy of the MPL was not distributed with this file, You can
  14 # obtain one at http://mozilla.org/MPL/2.0/.
  15 #
  16 # END HEADER
  17 
  18 from __future__ import division, print_function, absolute_import
  19 
  20 import os
  21 import sys
  22 
  23 import coverage
  24 
  25 import hypothesis
  26 from hypothesis.errors import StopTest, DeadlineExceeded, \
  27     HypothesisException, UnsatisfiedAssumption
  28 from hypothesis.internal.compat import text_type, binary_type, \
  29     encoded_filepath
  30 
  31 
  32 def belongs_to(package):
  33     root = os.path.dirname(package.__file__)
  34     cache = {text_type: {}, binary_type: {}}
  35 
  36     def accept(filepath):
  37         try:
  38             return cache[type(filepath)][filepath]
  39         except KeyError:
  40             pass
  41         filepath = encoded_filepath(filepath)
  42         result = os.path.abspath(filepath).startswith(root)
  43         cache[type(filepath)][filepath] = result
  44         return result
  45     accept.__name__ = 'is_%s_file' % (package.__name__,)
  46     return accept
  47 
  48 
  49 PREVENT_ESCALATION = os.getenv('HYPOTHESIS_DO_NOT_ESCALATE') == 'true'
  50 
  51 FILE_CACHE = {}
  52 
  53 
  54 is_hypothesis_file = belongs_to(hypothesis)
  55 is_coverage_file = belongs_to(coverage)
  56 
  57 HYPOTHESIS_CONTROL_EXCEPTIONS = (
  58     DeadlineExceeded, StopTest, UnsatisfiedAssumption
  59 )
  60 
  61 
  62 def mark_for_escalation(e):
  63     if not isinstance(e, HYPOTHESIS_CONTROL_EXCEPTIONS):
  64         e.hypothesis_internal_always_escalate = True
  65 
  66 
  67 def escalate_hypothesis_internal_error():
  68     if PREVENT_ESCALATION:
  69         return
  70     error_type, e, tb = sys.exc_info()
  71     if getattr(e, 'hypothesis_internal_always_escalate', False):
  72         raise
  73     import traceback
  74     filepath = traceback.extract_tb(tb)[-1][0]
  75     if is_hypothesis_file(filepath) and not isinstance(
  76         e, (HypothesisException,) + HYPOTHESIS_CONTROL_EXCEPTIONS,
  77     ):
  78         raise
  79     # This is so that if we do something wrong and trigger an internal Coverage
  80     # error we don't try to catch it. It should be impossible to trigger, but
  81     # you never know.
  82     if is_coverage_file(filepath):  # pragma: no cover
  83         raise
  84
  ```
  --- 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/hypothesis/internal/escalation.py b/src/hypothesis/internal/escalation.py
  --- a/src/hypothesis/internal/escalation.py
  +++ b/src/hypothesis/internal/escalation.py
  @@ -34,13 +34,15 @@
       cache = {text_type: {}, binary_type: {}}
   
       def accept(filepath):
  +        ftype = type(filepath)
           try:
  -            return cache[type(filepath)][filepath]
  +            return cache[ftype][filepath]
           except KeyError:
               pass
  -        filepath = encoded_filepath(filepath)
  -        result = os.path.abspath(filepath).startswith(root)
  -        cache[type(filepath)][filepath] = result
  +        new_filepath = encoded_filepath(filepath)
  +        result = os.path.abspath(new_filepath).startswith(root)
  +        cache[ftype][filepath] = result
  +        cache[type(new_filepath)][new_filepath] = result
           return result
       accept.__name__ = 'is_%s_file' % (package.__name__,)
       return accept
  
verification_info: {"golden_diff": "diff --git a/src/hypothesis/internal/escalation.py b/src/hypothesis/internal/escalation.py\n--- a/src/hypothesis/internal/escalation.py\n+++ b/src/hypothesis/internal/escalation.py\n@@ -34,13 +34,15 @@\n     cache = {text_type: {}, binary_type: {}}\n \n     def accept(filepath):\n+        ftype = type(filepath)\n         try:\n-            return cache[type(filepath)][filepath]\n+            return cache[ftype][filepath]\n         except KeyError:\n             pass\n-        filepath = encoded_filepath(filepath)\n-        result = os.path.abspath(filepath).startswith(root)\n-        cache[type(filepath)][filepath] = result\n+        new_filepath = encoded_filepath(filepath)\n+        result = os.path.abspath(new_filepath).startswith(root)\n+        cache[ftype][filepath] = result\n+        cache[type(new_filepath)][new_filepath] = result\n         return result\n     accept.__name__ = 'is_%s_file' % (package.__name__,)\n     return accept\n", "issue": "`escalation.belongs_to` cache is broken\nThe problem is that the cache is checked with the incoming string type, but [the string type changes](https://github.com/HypothesisWorks/hypothesis-python/blob/3.44.1/src/hypothesis/internal/escalation.py#L41) before the value is inserted:\r\n\r\nA simple (but not elegant) fix:\r\n\r\n```diff\r\n--- hypothesis/internal/escalation.py\r\n+++ hypothesis/internal/escalation.py\r\n@@ -34,13 +34,14 @@ def belongs_to(package):\r\n     cache = {text_type: {}, binary_type: {}}\r\n \r\n     def accept(filepath):\r\n+        ftype = type(filepath)\r\n         try:\r\n-            return cache[type(filepath)][filepath]\r\n+            return cache[ftype][filepath]\r\n         except KeyError:\r\n             pass\r\n         filepath = encoded_filepath(filepath)\r\n         result = os.path.abspath(filepath).startswith(root)\r\n-        cache[type(filepath)][filepath] = result\r\n+        cache[ftype][filepath] = result\r\n         return result\r\n     accept.__name__ = 'is_%s_file' % (package.__name__,)\r\n     return accept\r\n```\n", "before_files": [{"content": "# coding=utf-8\n#\n# This file is part of Hypothesis, which may be found at\n# https://github.com/HypothesisWorks/hypothesis-python\n#\n# Most of this work is copyright (C) 2013-2017 David R. MacIver\n# (david@drmaciver.com), but it contains contributions by others. See\n# CONTRIBUTING.rst for a full list of people who may hold copyright, and\n# consult the git log if you need to determine who owns an individual\n# contribution.\n#\n# This Source Code Form is subject to the terms of the Mozilla Public License,\n# v. 2.0. If a copy of the MPL was not distributed with this file, You can\n# obtain one at http://mozilla.org/MPL/2.0/.\n#\n# END HEADER\n\nfrom __future__ import division, print_function, absolute_import\n\nimport os\nimport sys\n\nimport coverage\n\nimport hypothesis\nfrom hypothesis.errors import StopTest, DeadlineExceeded, \\\n    HypothesisException, UnsatisfiedAssumption\nfrom hypothesis.internal.compat import text_type, binary_type, \\\n    encoded_filepath\n\n\ndef belongs_to(package):\n    root = os.path.dirname(package.__file__)\n    cache = {text_type: {}, binary_type: {}}\n\n    def accept(filepath):\n        try:\n            return cache[type(filepath)][filepath]\n        except KeyError:\n            pass\n        filepath = encoded_filepath(filepath)\n        result = os.path.abspath(filepath).startswith(root)\n        cache[type(filepath)][filepath] = result\n        return result\n    accept.__name__ = 'is_%s_file' % (package.__name__,)\n    return accept\n\n\nPREVENT_ESCALATION = os.getenv('HYPOTHESIS_DO_NOT_ESCALATE') == 'true'\n\nFILE_CACHE = {}\n\n\nis_hypothesis_file = belongs_to(hypothesis)\nis_coverage_file = belongs_to(coverage)\n\nHYPOTHESIS_CONTROL_EXCEPTIONS = (\n    DeadlineExceeded, StopTest, UnsatisfiedAssumption\n)\n\n\ndef mark_for_escalation(e):\n    if not isinstance(e, HYPOTHESIS_CONTROL_EXCEPTIONS):\n        e.hypothesis_internal_always_escalate = True\n\n\ndef escalate_hypothesis_internal_error():\n    if PREVENT_ESCALATION:\n        return\n    error_type, e, tb = sys.exc_info()\n    if getattr(e, 'hypothesis_internal_always_escalate', False):\n        raise\n    import traceback\n    filepath = traceback.extract_tb(tb)[-1][0]\n    if is_hypothesis_file(filepath) and not isinstance(\n        e, (HypothesisException,) + HYPOTHESIS_CONTROL_EXCEPTIONS,\n    ):\n        raise\n    # This is so that if we do something wrong and trigger an internal Coverage\n    # error we don't try to catch it. It should be impossible to trigger, but\n    # you never know.\n    if is_coverage_file(filepath):  # pragma: no cover\n        raise\n", "path": "src/hypothesis/internal/escalation.py"}], "after_files": [{"content": "# coding=utf-8\n#\n# This file is part of Hypothesis, which may be found at\n# https://github.com/HypothesisWorks/hypothesis-python\n#\n# Most of this work is copyright (C) 2013-2017 David R. MacIver\n# (david@drmaciver.com), but it contains contributions by others. See\n# CONTRIBUTING.rst for a full list of people who may hold copyright, and\n# consult the git log if you need to determine who owns an individual\n# contribution.\n#\n# This Source Code Form is subject to the terms of the Mozilla Public License,\n# v. 2.0. If a copy of the MPL was not distributed with this file, You can\n# obtain one at http://mozilla.org/MPL/2.0/.\n#\n# END HEADER\n\nfrom __future__ import division, print_function, absolute_import\n\nimport os\nimport sys\n\nimport coverage\n\nimport hypothesis\nfrom hypothesis.errors import StopTest, DeadlineExceeded, \\\n    HypothesisException, UnsatisfiedAssumption\nfrom hypothesis.internal.compat import text_type, binary_type, \\\n    encoded_filepath\n\n\ndef belongs_to(package):\n    root = os.path.dirname(package.__file__)\n    cache = {text_type: {}, binary_type: {}}\n\n    def accept(filepath):\n        ftype = type(filepath)\n        try:\n            return cache[ftype][filepath]\n        except KeyError:\n            pass\n        new_filepath = encoded_filepath(filepath)\n        result = os.path.abspath(new_filepath).startswith(root)\n        cache[ftype][filepath] = result\n        cache[type(new_filepath)][new_filepath] = result\n        return result\n    accept.__name__ = 'is_%s_file' % (package.__name__,)\n    return accept\n\n\nPREVENT_ESCALATION = os.getenv('HYPOTHESIS_DO_NOT_ESCALATE') == 'true'\n\nFILE_CACHE = {}\n\n\nis_hypothesis_file = belongs_to(hypothesis)\nis_coverage_file = belongs_to(coverage)\n\nHYPOTHESIS_CONTROL_EXCEPTIONS = (\n    DeadlineExceeded, StopTest, UnsatisfiedAssumption\n)\n\n\ndef mark_for_escalation(e):\n    if not isinstance(e, HYPOTHESIS_CONTROL_EXCEPTIONS):\n        e.hypothesis_internal_always_escalate = True\n\n\ndef escalate_hypothesis_internal_error():\n    if PREVENT_ESCALATION:\n        return\n    error_type, e, tb = sys.exc_info()\n    if getattr(e, 'hypothesis_internal_always_escalate', False):\n        raise\n    import traceback\n    filepath = traceback.extract_tb(tb)[-1][0]\n    if is_hypothesis_file(filepath) and not isinstance(\n        e, (HypothesisException,) + HYPOTHESIS_CONTROL_EXCEPTIONS,\n    ):\n        raise\n    # This is so that if we do something wrong and trigger an internal Coverage\n    # error we don't try to catch it. It should be impossible to trigger, but\n    # you never know.\n    if is_coverage_file(filepath):  # pragma: no cover\n        raise\n", "path": "src/hypothesis/internal/escalation.py"}]}