prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Error 500 when pasting JSON with single surrogate characters
  Example:
  ```json
  {
    "equal_width": false,
    "title": "Which fruit do *you* like?",
    "options": [
      "Apple",
      "Banana",
      "Orange",
      "Papaya", "\ud800"
    ]
  }
  ```
  Error 500 when pasting JSON with single surrogate characters
  Example:
  ```json
  {
    "equal_width": false,
    "title": "Which fruit do *you* like?",
    "options": [
      "Apple",
      "Banana",
      "Orange",
      "Papaya", "\ud800"
    ]
  }
  ```
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `jawanndenn/views.py`
  Content:
  ```
  1 # Copyright (C) 2019 Sebastian Pipping <sebastian@pipping.org>
  2 # Licensed under GNU Affero GPL v3 or later
  3 
  4 import json  # lgtm [py/import-and-import-from]
  5 from functools import wraps
  6 from json import JSONDecodeError
  7 
  8 from django.conf import settings
  9 from django.db import transaction
  10 from django.http import (Http404, HttpResponseBadRequest, HttpResponseNotFound,
  11                          JsonResponse)
  12 from django.shortcuts import redirect
  13 from django.template.response import TemplateResponse
  14 from django.utils.module_loading import import_string
  15 from django.views.decorators.http import require_GET, require_POST
  16 from django.views.defaults import bad_request
  17 from django.views.static import serve
  18 from jawanndenn.markup import safe_html
  19 from jawanndenn.models import Ballot, Poll, Vote
  20 from jawanndenn.serializers import PollConfigSerializer
  21 from rest_framework.exceptions import ValidationError
  22 
  23 _staticfile_finders = [import_string(cls_string)()
  24                        for cls_string in settings.STATICFILES_FINDERS]
  25 
  26 
  27 def _except_poll_does_not_exist(wrappee):
  28     """Decorator that turns Poll.DoesNotExist into 404 Not Found"""
  29     @wraps(wrappee)
  30     def wrapper(*args, **kwargs):
  31         try:
  32             return wrappee(*args, **kwargs)
  33         except Poll.DoesNotExist:
  34             return HttpResponseNotFound('No such poll')
  35 
  36     return wrapper
  37 
  38 
  39 def _except_validation_error(wrappee):
  40     """Decorator that turns ValidationError into 400 Bad Request"""
  41     @wraps(wrappee)
  42     def wrapper(request, *args, **kwargs):
  43         try:
  44             return wrappee(request, *args, **kwargs)
  45         except ValidationError as exception:
  46             return bad_request(request, exception)
  47 
  48     return wrapper
  49 
  50 
  51 @require_GET
  52 def index_get_view(request):
  53     return TemplateResponse(request,
  54                             template='html/setup.xhtml',
  55                             content_type='application/xhtml+xml')
  56 
  57 
  58 @require_POST
  59 @_except_validation_error
  60 def poll_post_view(request):
  61     config_json = request.POST.get('config', '{}')
  62     try:
  63         config = json.loads(config_json)
  64     except JSONDecodeError:
  65         raise ValidationError('Poll configuration is not well-formed JSON.')
  66 
  67     serializer = PollConfigSerializer(data=config)
  68     serializer.is_valid(raise_exception=True)
  69 
  70     with transaction.atomic():
  71         if Poll.objects.count() >= settings.JAWANNDENN_MAX_POLLS:
  72             return HttpResponseBadRequest(
  73                 f'Maximum number of {settings.JAWANNDENN_MAX_POLLS} polls '
  74                 'reached, please contact the administrator.')
  75 
  76         poll = serializer.save()
  77 
  78     return redirect(poll)
  79 
  80 
  81 @require_GET
  82 @_except_poll_does_not_exist
  83 def poll_data_get_view(request, poll_id):
  84     with transaction.atomic():
  85         poll = Poll.objects.get(slug=poll_id)
  86         poll_config = {
  87             'equal_width': poll.equal_width,
  88             'title': poll.title,
  89             'options': list(poll.options.order_by('position')
  90                             .values_list('name', flat=True)),
  91         }
  92         votes = [
  93             [ballot.voter_name, [vote.yes for vote
  94                                  in ballot.votes.order_by('option__position')]]
  95             for ballot
  96             in poll.ballots.order_by('created', 'id')
  97         ]
  98 
  99     data = {
  100         'config': poll_config,
  101         'votes': votes,
  102     }
  103 
  104     return JsonResponse(data)
  105 
  106 
  107 @require_GET
  108 @_except_poll_does_not_exist
  109 def poll_get_view(request, poll_id):
  110     Poll.objects.get(slug=poll_id)
  111 
  112     return TemplateResponse(request,
  113                             template='html/poll.xhtml',
  114                             content_type='application/xhtml+xml')
  115 
  116 
  117 @require_POST
  118 @_except_poll_does_not_exist
  119 def vote_post_view(request, poll_id):
  120     with transaction.atomic():
  121         poll = Poll.objects.get(slug=poll_id)
  122 
  123         if poll.ballots.count() >= settings.JAWANNDENN_MAX_VOTES_PER_POLL:
  124             return HttpResponseBadRequest(
  125                 f'Maximum number of {settings.JAWANNDENN_MAX_VOTES_PER_POLL} '
  126                 'votes reached for this poll'
  127                 ', please contact the administrator.')
  128 
  129         voter_name = safe_html(request.POST.get('voterName'))
  130         votes = [
  131             request.POST.get(f'option{i}', 'off') == 'on'
  132             for i
  133             in range(poll.options.count())
  134         ]
  135 
  136         ballot = Ballot.objects.create(poll=poll, voter_name=voter_name)
  137         for option, vote in zip(poll.options.order_by('position'), votes):
  138             Vote.objects.create(ballot=ballot, option=option, yes=vote)
  139 
  140     return redirect(poll)
  141 
  142 
  143 @require_GET
  144 def serve_using_finders(request, path, show_indexes=False):
  145     """
  146     Wrapper around django.views.static.serve that uses
  147     settings.STATICFILES_FINDERS rather than a single document_root
  148     """
  149     for finder in _staticfile_finders:
  150         fullpath = finder.find(path)
  151         if fullpath:
  152             document_root = fullpath[:-len(path)] if path else fullpath
  153             return serve(request, path, document_root=document_root,
  154                          show_indexes=show_indexes)
  155     else:
  156         raise Http404
  157
  ```
  --- 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/jawanndenn/views.py b/jawanndenn/views.py
  --- a/jawanndenn/views.py
  +++ b/jawanndenn/views.py
  @@ -1,10 +1,9 @@
   # Copyright (C) 2019 Sebastian Pipping <sebastian@pipping.org>
   # Licensed under GNU Affero GPL v3 or later
   
  -import json  # lgtm [py/import-and-import-from]
   from functools import wraps
  -from json import JSONDecodeError
   
  +import rapidjson as json  # lgtm [py/import-and-import-from]
   from django.conf import settings
   from django.db import transaction
   from django.http import (Http404, HttpResponseBadRequest, HttpResponseNotFound,
  @@ -18,6 +17,7 @@
   from jawanndenn.markup import safe_html
   from jawanndenn.models import Ballot, Poll, Vote
   from jawanndenn.serializers import PollConfigSerializer
  +from rapidjson import JSONDecodeError
   from rest_framework.exceptions import ValidationError
   
   _staticfile_finders = [import_string(cls_string)()
  
verification_info: {"golden_diff": "diff --git a/jawanndenn/views.py b/jawanndenn/views.py\n--- a/jawanndenn/views.py\n+++ b/jawanndenn/views.py\n@@ -1,10 +1,9 @@\n # Copyright (C) 2019 Sebastian Pipping <sebastian@pipping.org>\n # Licensed under GNU Affero GPL v3 or later\n \n-import json  # lgtm [py/import-and-import-from]\n from functools import wraps\n-from json import JSONDecodeError\n \n+import rapidjson as json  # lgtm [py/import-and-import-from]\n from django.conf import settings\n from django.db import transaction\n from django.http import (Http404, HttpResponseBadRequest, HttpResponseNotFound,\n@@ -18,6 +17,7 @@\n from jawanndenn.markup import safe_html\n from jawanndenn.models import Ballot, Poll, Vote\n from jawanndenn.serializers import PollConfigSerializer\n+from rapidjson import JSONDecodeError\n from rest_framework.exceptions import ValidationError\n \n _staticfile_finders = [import_string(cls_string)()\n", "issue": "Error 500 when pasting JSON with single surrogate characters\nExample:\r\n```json\r\n{\r\n  \"equal_width\": false,\r\n  \"title\": \"Which fruit do *you* like?\",\r\n  \"options\": [\r\n    \"Apple\",\r\n    \"Banana\",\r\n    \"Orange\",\r\n    \"Papaya\", \"\\ud800\"\r\n  ]\r\n}\r\n```\nError 500 when pasting JSON with single surrogate characters\nExample:\r\n```json\r\n{\r\n  \"equal_width\": false,\r\n  \"title\": \"Which fruit do *you* like?\",\r\n  \"options\": [\r\n    \"Apple\",\r\n    \"Banana\",\r\n    \"Orange\",\r\n    \"Papaya\", \"\\ud800\"\r\n  ]\r\n}\r\n```\n", "before_files": [{"content": "# Copyright (C) 2019 Sebastian Pipping <sebastian@pipping.org>\n# Licensed under GNU Affero GPL v3 or later\n\nimport json  # lgtm [py/import-and-import-from]\nfrom functools import wraps\nfrom json import JSONDecodeError\n\nfrom django.conf import settings\nfrom django.db import transaction\nfrom django.http import (Http404, HttpResponseBadRequest, HttpResponseNotFound,\n                         JsonResponse)\nfrom django.shortcuts import redirect\nfrom django.template.response import TemplateResponse\nfrom django.utils.module_loading import import_string\nfrom django.views.decorators.http import require_GET, require_POST\nfrom django.views.defaults import bad_request\nfrom django.views.static import serve\nfrom jawanndenn.markup import safe_html\nfrom jawanndenn.models import Ballot, Poll, Vote\nfrom jawanndenn.serializers import PollConfigSerializer\nfrom rest_framework.exceptions import ValidationError\n\n_staticfile_finders = [import_string(cls_string)()\n                       for cls_string in settings.STATICFILES_FINDERS]\n\n\ndef _except_poll_does_not_exist(wrappee):\n    \"\"\"Decorator that turns Poll.DoesNotExist into 404 Not Found\"\"\"\n    @wraps(wrappee)\n    def wrapper(*args, **kwargs):\n        try:\n            return wrappee(*args, **kwargs)\n        except Poll.DoesNotExist:\n            return HttpResponseNotFound('No such poll')\n\n    return wrapper\n\n\ndef _except_validation_error(wrappee):\n    \"\"\"Decorator that turns ValidationError into 400 Bad Request\"\"\"\n    @wraps(wrappee)\n    def wrapper(request, *args, **kwargs):\n        try:\n            return wrappee(request, *args, **kwargs)\n        except ValidationError as exception:\n            return bad_request(request, exception)\n\n    return wrapper\n\n\n@require_GET\ndef index_get_view(request):\n    return TemplateResponse(request,\n                            template='html/setup.xhtml',\n                            content_type='application/xhtml+xml')\n\n\n@require_POST\n@_except_validation_error\ndef poll_post_view(request):\n    config_json = request.POST.get('config', '{}')\n    try:\n        config = json.loads(config_json)\n    except JSONDecodeError:\n        raise ValidationError('Poll configuration is not well-formed JSON.')\n\n    serializer = PollConfigSerializer(data=config)\n    serializer.is_valid(raise_exception=True)\n\n    with transaction.atomic():\n        if Poll.objects.count() >= settings.JAWANNDENN_MAX_POLLS:\n            return HttpResponseBadRequest(\n                f'Maximum number of {settings.JAWANNDENN_MAX_POLLS} polls '\n                'reached, please contact the administrator.')\n\n        poll = serializer.save()\n\n    return redirect(poll)\n\n\n@require_GET\n@_except_poll_does_not_exist\ndef poll_data_get_view(request, poll_id):\n    with transaction.atomic():\n        poll = Poll.objects.get(slug=poll_id)\n        poll_config = {\n            'equal_width': poll.equal_width,\n            'title': poll.title,\n            'options': list(poll.options.order_by('position')\n                            .values_list('name', flat=True)),\n        }\n        votes = [\n            [ballot.voter_name, [vote.yes for vote\n                                 in ballot.votes.order_by('option__position')]]\n            for ballot\n            in poll.ballots.order_by('created', 'id')\n        ]\n\n    data = {\n        'config': poll_config,\n        'votes': votes,\n    }\n\n    return JsonResponse(data)\n\n\n@require_GET\n@_except_poll_does_not_exist\ndef poll_get_view(request, poll_id):\n    Poll.objects.get(slug=poll_id)\n\n    return TemplateResponse(request,\n                            template='html/poll.xhtml',\n                            content_type='application/xhtml+xml')\n\n\n@require_POST\n@_except_poll_does_not_exist\ndef vote_post_view(request, poll_id):\n    with transaction.atomic():\n        poll = Poll.objects.get(slug=poll_id)\n\n        if poll.ballots.count() >= settings.JAWANNDENN_MAX_VOTES_PER_POLL:\n            return HttpResponseBadRequest(\n                f'Maximum number of {settings.JAWANNDENN_MAX_VOTES_PER_POLL} '\n                'votes reached for this poll'\n                ', please contact the administrator.')\n\n        voter_name = safe_html(request.POST.get('voterName'))\n        votes = [\n            request.POST.get(f'option{i}', 'off') == 'on'\n            for i\n            in range(poll.options.count())\n        ]\n\n        ballot = Ballot.objects.create(poll=poll, voter_name=voter_name)\n        for option, vote in zip(poll.options.order_by('position'), votes):\n            Vote.objects.create(ballot=ballot, option=option, yes=vote)\n\n    return redirect(poll)\n\n\n@require_GET\ndef serve_using_finders(request, path, show_indexes=False):\n    \"\"\"\n    Wrapper around django.views.static.serve that uses\n    settings.STATICFILES_FINDERS rather than a single document_root\n    \"\"\"\n    for finder in _staticfile_finders:\n        fullpath = finder.find(path)\n        if fullpath:\n            document_root = fullpath[:-len(path)] if path else fullpath\n            return serve(request, path, document_root=document_root,\n                         show_indexes=show_indexes)\n    else:\n        raise Http404\n", "path": "jawanndenn/views.py"}], "after_files": [{"content": "# Copyright (C) 2019 Sebastian Pipping <sebastian@pipping.org>\n# Licensed under GNU Affero GPL v3 or later\n\nfrom functools import wraps\n\nimport rapidjson as json  # lgtm [py/import-and-import-from]\nfrom django.conf import settings\nfrom django.db import transaction\nfrom django.http import (Http404, HttpResponseBadRequest, HttpResponseNotFound,\n                         JsonResponse)\nfrom django.shortcuts import redirect\nfrom django.template.response import TemplateResponse\nfrom django.utils.module_loading import import_string\nfrom django.views.decorators.http import require_GET, require_POST\nfrom django.views.defaults import bad_request\nfrom django.views.static import serve\nfrom jawanndenn.markup import safe_html\nfrom jawanndenn.models import Ballot, Poll, Vote\nfrom jawanndenn.serializers import PollConfigSerializer\nfrom rapidjson import JSONDecodeError\nfrom rest_framework.exceptions import ValidationError\n\n_staticfile_finders = [import_string(cls_string)()\n                       for cls_string in settings.STATICFILES_FINDERS]\n\n\ndef _except_poll_does_not_exist(wrappee):\n    \"\"\"Decorator that turns Poll.DoesNotExist into 404 Not Found\"\"\"\n    @wraps(wrappee)\n    def wrapper(*args, **kwargs):\n        try:\n            return wrappee(*args, **kwargs)\n        except Poll.DoesNotExist:\n            return HttpResponseNotFound('No such poll')\n\n    return wrapper\n\n\ndef _except_validation_error(wrappee):\n    \"\"\"Decorator that turns ValidationError into 400 Bad Request\"\"\"\n    @wraps(wrappee)\n    def wrapper(request, *args, **kwargs):\n        try:\n            return wrappee(request, *args, **kwargs)\n        except ValidationError as exception:\n            return bad_request(request, exception)\n\n    return wrapper\n\n\n@require_GET\ndef index_get_view(request):\n    return TemplateResponse(request,\n                            template='html/setup.xhtml',\n                            content_type='application/xhtml+xml')\n\n\n@require_POST\n@_except_validation_error\ndef poll_post_view(request):\n    config_json = request.POST.get('config', '{}')\n    try:\n        config = json.loads(config_json)\n    except JSONDecodeError:\n        raise ValidationError('Poll configuration is not well-formed JSON.')\n\n    serializer = PollConfigSerializer(data=config)\n    serializer.is_valid(raise_exception=True)\n\n    with transaction.atomic():\n        if Poll.objects.count() >= settings.JAWANNDENN_MAX_POLLS:\n            return HttpResponseBadRequest(\n                f'Maximum number of {settings.JAWANNDENN_MAX_POLLS} polls '\n                'reached, please contact the administrator.')\n\n        poll = serializer.save()\n\n    return redirect(poll)\n\n\n@require_GET\n@_except_poll_does_not_exist\ndef poll_data_get_view(request, poll_id):\n    with transaction.atomic():\n        poll = Poll.objects.get(slug=poll_id)\n        poll_config = {\n            'equal_width': poll.equal_width,\n            'title': poll.title,\n            'options': list(poll.options.order_by('position')\n                            .values_list('name', flat=True)),\n        }\n        votes = [\n            [ballot.voter_name, [vote.yes for vote\n                                 in ballot.votes.order_by('option__position')]]\n            for ballot\n            in poll.ballots.order_by('created', 'id')\n        ]\n\n    data = {\n        'config': poll_config,\n        'votes': votes,\n    }\n\n    return JsonResponse(data)\n\n\n@require_GET\n@_except_poll_does_not_exist\ndef poll_get_view(request, poll_id):\n    Poll.objects.get(slug=poll_id)\n\n    return TemplateResponse(request,\n                            template='html/poll.xhtml',\n                            content_type='application/xhtml+xml')\n\n\n@require_POST\n@_except_poll_does_not_exist\ndef vote_post_view(request, poll_id):\n    with transaction.atomic():\n        poll = Poll.objects.get(slug=poll_id)\n\n        if poll.ballots.count() >= settings.JAWANNDENN_MAX_VOTES_PER_POLL:\n            return HttpResponseBadRequest(\n                f'Maximum number of {settings.JAWANNDENN_MAX_VOTES_PER_POLL} '\n                'votes reached for this poll'\n                ', please contact the administrator.')\n\n        voter_name = safe_html(request.POST.get('voterName'))\n        votes = [\n            request.POST.get(f'option{i}', 'off') == 'on'\n            for i\n            in range(poll.options.count())\n        ]\n\n        ballot = Ballot.objects.create(poll=poll, voter_name=voter_name)\n        for option, vote in zip(poll.options.order_by('position'), votes):\n            Vote.objects.create(ballot=ballot, option=option, yes=vote)\n\n    return redirect(poll)\n\n\n@require_GET\ndef serve_using_finders(request, path, show_indexes=False):\n    \"\"\"\n    Wrapper around django.views.static.serve that uses\n    settings.STATICFILES_FINDERS rather than a single document_root\n    \"\"\"\n    for finder in _staticfile_finders:\n        fullpath = finder.find(path)\n        if fullpath:\n            document_root = fullpath[:-len(path)] if path else fullpath\n            return serve(request, path, document_root=document_root,\n                         show_indexes=show_indexes)\n    else:\n        raise Http404\n", "path": "jawanndenn/views.py"}]}