prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  `?` may generate hundreds of cell 
  By mistake I have executed a cell like 
  
  ```
  for i in range(3):
      x= range?
  ```
  
  but with ~70 instead of 3
  which generated 70 code cell with just `x= range` in it...
  it was _really_ painfull to clean, it would be nice to prevent something like that
  
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `IPython/core/payload.py`
  Content:
  ```
  1 # -*- coding: utf-8 -*-
  2 """Payload system for IPython.
  3 
  4 Authors:
  5 
  6 * Fernando Perez
  7 * Brian Granger
  8 """
  9 
  10 #-----------------------------------------------------------------------------
  11 #       Copyright (C) 2008-2011 The IPython Development Team
  12 #
  13 #  Distributed under the terms of the BSD License.  The full license is in
  14 #  the file COPYING, distributed as part of this software.
  15 #-----------------------------------------------------------------------------
  16 
  17 #-----------------------------------------------------------------------------
  18 # Imports
  19 #-----------------------------------------------------------------------------
  20 
  21 from IPython.config.configurable import Configurable
  22 from IPython.utils.traitlets import List
  23 
  24 #-----------------------------------------------------------------------------
  25 # Main payload class
  26 #-----------------------------------------------------------------------------
  27 
  28 class PayloadManager(Configurable):
  29 
  30     _payload = List([])
  31 
  32     def write_payload(self, data):
  33         if not isinstance(data, dict):
  34             raise TypeError('Each payload write must be a dict, got: %r' % data)
  35         self._payload.append(data)
  36 
  37     def read_payload(self):
  38         return self._payload
  39 
  40     def clear_payload(self):
  41         self._payload = []
  42
  ```
  --- 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/IPython/core/payload.py b/IPython/core/payload.py
  --- a/IPython/core/payload.py
  +++ b/IPython/core/payload.py
  @@ -29,9 +29,23 @@
   
       _payload = List([])
   
  -    def write_payload(self, data):
  +    def write_payload(self, data, single=True):
  +        """Include or update the specified `data` payload in the PayloadManager.
  +
  +        If a previous payload with the same source exists and `single` is True,
  +        it will be overwritten with the new one.
  +        """
  +
           if not isinstance(data, dict):
               raise TypeError('Each payload write must be a dict, got: %r' % data)
  +
  +        if single and 'source' in data:
  +            source = data['source']
  +            for i, pl in enumerate(self._payload):
  +                if 'source' in pl and pl['source'] == source:
  +                    self._payload[i] = data
  +                    return
  +
           self._payload.append(data)
   
       def read_payload(self):
  
verification_info: {"golden_diff": "diff --git a/IPython/core/payload.py b/IPython/core/payload.py\n--- a/IPython/core/payload.py\n+++ b/IPython/core/payload.py\n@@ -29,9 +29,23 @@\n \n     _payload = List([])\n \n-    def write_payload(self, data):\n+    def write_payload(self, data, single=True):\n+        \"\"\"Include or update the specified `data` payload in the PayloadManager.\n+\n+        If a previous payload with the same source exists and `single` is True,\n+        it will be overwritten with the new one.\n+        \"\"\"\n+\n         if not isinstance(data, dict):\n             raise TypeError('Each payload write must be a dict, got: %r' % data)\n+\n+        if single and 'source' in data:\n+            source = data['source']\n+            for i, pl in enumerate(self._payload):\n+                if 'source' in pl and pl['source'] == source:\n+                    self._payload[i] = data\n+                    return\n+\n         self._payload.append(data)\n \n     def read_payload(self):\n", "issue": "`?` may generate hundreds of cell \nBy mistake I have executed a cell like \r\n\r\n```\r\nfor i in range(3):\r\n    x= range?\r\n```\r\n\r\nbut with ~70 instead of 3\r\nwhich generated 70 code cell with just `x= range` in it...\r\nit was _really_ painfull to clean, it would be nice to prevent something like that\r\n\n", "before_files": [{"content": "# -*- coding: utf-8 -*-\n\"\"\"Payload system for IPython.\n\nAuthors:\n\n* Fernando Perez\n* Brian Granger\n\"\"\"\n\n#-----------------------------------------------------------------------------\n#       Copyright (C) 2008-2011 The IPython Development Team\n#\n#  Distributed under the terms of the BSD License.  The full license is in\n#  the file COPYING, distributed as part of this software.\n#-----------------------------------------------------------------------------\n\n#-----------------------------------------------------------------------------\n# Imports\n#-----------------------------------------------------------------------------\n\nfrom IPython.config.configurable import Configurable\nfrom IPython.utils.traitlets import List\n\n#-----------------------------------------------------------------------------\n# Main payload class\n#-----------------------------------------------------------------------------\n\nclass PayloadManager(Configurable):\n\n    _payload = List([])\n\n    def write_payload(self, data):\n        if not isinstance(data, dict):\n            raise TypeError('Each payload write must be a dict, got: %r' % data)\n        self._payload.append(data)\n\n    def read_payload(self):\n        return self._payload\n\n    def clear_payload(self):\n        self._payload = []\n", "path": "IPython/core/payload.py"}], "after_files": [{"content": "# -*- coding: utf-8 -*-\n\"\"\"Payload system for IPython.\n\nAuthors:\n\n* Fernando Perez\n* Brian Granger\n\"\"\"\n\n#-----------------------------------------------------------------------------\n#       Copyright (C) 2008-2011 The IPython Development Team\n#\n#  Distributed under the terms of the BSD License.  The full license is in\n#  the file COPYING, distributed as part of this software.\n#-----------------------------------------------------------------------------\n\n#-----------------------------------------------------------------------------\n# Imports\n#-----------------------------------------------------------------------------\n\nfrom IPython.config.configurable import Configurable\nfrom IPython.utils.traitlets import List\n\n#-----------------------------------------------------------------------------\n# Main payload class\n#-----------------------------------------------------------------------------\n\nclass PayloadManager(Configurable):\n\n    _payload = List([])\n\n    def write_payload(self, data, single=True):\n        \"\"\"Include or update the specified `data` payload in the PayloadManager.\n\n        If a previous payload with the same source exists and `single` is True,\n        it will be overwritten with the new one.\n        \"\"\"\n\n        if not isinstance(data, dict):\n            raise TypeError('Each payload write must be a dict, got: %r' % data)\n\n        if single and 'source' in data:\n            source = data['source']\n            for i, pl in enumerate(self._payload):\n                if 'source' in pl and pl['source'] == source:\n                    self._payload[i] = data\n                    return\n\n        self._payload.append(data)\n\n    def read_payload(self):\n        return self._payload\n\n    def clear_payload(self):\n        self._payload = []\n", "path": "IPython/core/payload.py"}]}