internlm/SWE-Fixer-Train-110K huggingface dataset:
Repository: ipython/ipython

Issue:
`terminal.shortcuts`: DEFAULT_BUFFER is not defined
I'm using IPython as the interactive Flask shell for my daily work, and it usually works like a charm.
For a little while now, I keep getting error messages while I'm jumping around in my command history (`^R` and co.) like the following:
```
Unhandled exception in event loop:
  File "/home/mmoser/.pyenv/versions/3.9.15/lib/python3.9/asyncio/events.py", line 80, in _run
    self._context.run(self._callback, *self._args)
  File "/home/mmoser/.local/share/virtualenvs/my-site-OggevyoG/lib/python3.9/site-packages/prompt_toolkit/input/vt100.py", line 173, in callback_wrapper
    callback()
  File "/home/mmoser/.local/share/virtualenvs/my-site-OggevyoG/lib/python3.9/site-packages/prompt_toolkit/application/application.py", line 698, in read_from_input
    self.key_processor.process_keys()
  File "/home/mmoser/.local/share/virtualenvs/my-site-OggevyoG/lib/python3.9/site-packages/prompt_toolkit/key_binding/key_processor.py", line 272, in process_keys
    self._process_coroutine.send(key_press)
  File "/home/mmoser/.local/share/virtualenvs/my-site-OggevyoG/lib/python3.9/site-packages/prompt_toolkit/key_binding/key_processor.py", line 187, in _process
    self._call_handler(matches[-1], key_sequence=buffer[:])
  File "/home/mmoser/.local/share/virtualenvs/my-site-OggevyoG/lib/python3.9/site-packages/prompt_toolkit/key_binding/key_processor.py", line 322, in _call_handler
    handler.call(event)
  File "/home/mmoser/.local/share/virtualenvs/my-site-OggevyoG/lib/python3.9/site-packages/prompt_toolkit/key_binding/key_bindings.py", line 126, in call
    result = self.handler(event)
  File "/home/mmoser/.local/share/virtualenvs/my-site-OggevyoG/lib/python3.9/site-packages/IPython/terminal/shortcuts/__init__.py", line 468, in reset_search_buffer
    event.app.layout.focus(DEFAULT_BUFFER)

Exception name 'DEFAULT_BUFFER' is not defined
Press ENTER to continue...
```

My currently installed `ipython` version is 8.11.0, and Python is 3.9.15.

Indeed, on the latest commit on the `main` branch, it looks like [`DEFAULT_BUFFER` is used but never defined](https://github.com/ipython/ipython/blob/main/IPython/terminal/shortcuts/__init__.py#L468).


Discussion:

Resolving patch:
diff --git a/IPython/terminal/shortcuts/__init__.py b/IPython/terminal/shortcuts/__init__.py
--- a/IPython/terminal/shortcuts/__init__.py
+++ b/IPython/terminal/shortcuts/__init__.py
@@ -29,6 +29,8 @@
 from IPython.terminal.shortcuts.filters import filter_from_string
 from IPython.utils.decorators import undoc
 
+from prompt_toolkit.enums import DEFAULT_BUFFER
+
 __all__ = ["create_ipython_shortcuts"]
 
 


Test patch:
Repository: ipython/ipython

Issue:
Proposal: discard autosuggestion with `delete` key
Auto-suggestions can be discarded using <kbd>Esc</kbd> key (which works slow as there is a time delay allowing to press a combination of <kbd>Esc</kbd> with another key):

![existing-workaround](https://user-images.githubusercontent.com/5832902/227661482-39d72887-2620-44c2-83ab-74ac12e2b54a.gif)

When an auto-suggestion is partially accepted e.g. using <kbd>Ctrl</kbd> + <kbd>Right</kbd> it would appear intuitive to use <kbd>delete</kbd> key to clear the remaining part of the suggestion, but currently this does not work:

![current-behaviour](https://user-images.githubusercontent.com/5832902/227661502-d722a613-1368-42a5-9715-690ae9df8e6c.gif)

the proposed behaviour is depicted on the GIF below:

![proposed-behaviour](https://user-images.githubusercontent.com/5832902/227661534-38083923-57a1-4d7d-965d-598f8f0656c1.gif)

IPython 3.11 users can opt-in for the proposed behaviour by defining a custom shortcut:

```python
custom_shortcuts = [
    {
        "command": "IPython:auto_suggest.discard",
        "new_keys": ["delete"],
        "new_filter": "navigable_suggestions & has_suggestion & default_buffer_focused"
    }
]
%config TerminalInteractiveShell.shortcuts = custom_shortcuts
```

but there may be a value in making this a default behaviour (opt-out rather than opt-in).

Can I ask anyone interested to indicate if they would be happy with an addition of such a shortcut by default (:+1: or :-1: )?


Discussion:

Resolving patch:
diff --git a/IPython/terminal/shortcuts/__init__.py b/IPython/terminal/shortcuts/__init__.py
--- a/IPython/terminal/shortcuts/__init__.py
+++ b/IPython/terminal/shortcuts/__init__.py
@@ -207,6 +207,11 @@ def create_identifier(handler: Callable):
         ["escape"],
         "has_suggestion & default_buffer_focused & emacs_insert_mode",
     ),
+    Binding(
+        auto_suggest.discard,
+        ["delete"],
+        "has_suggestion & default_buffer_focused & emacs_insert_mode",
+    ),
     Binding(
         auto_suggest.swap_autosuggestion_up,
         ["up"],


Test patch:
Repository: ipython/ipython

Issue:
%page stops working due to OInfo change 
`import pandas as pd
df = pd.DataFrame(data={'a':[1,2,3],'b':list('abc')})
%page df

File lib/python3.11/site-packages/IPython/core/interactiveshell.py:2414, in InteractiveShell.run_line_magic(self, magic_name, line, _stack_depth)
   2412     kwargs['local_ns'] = self.get_local_scope(stack_depth)
   2413 with self.builtin_trap:
-> 2414     result = fn(*args, **kwargs)
   2416 # The code below prevents the output from being displayed
   2417 # when using magics with decodator @output_can_be_silenced
   2418 # when the last Python token in the expression is a ';'.
   2419 if getattr(fn, magic.MAGIC_OUTPUT_CAN_BE_SILENCED, False):

File lib/python3.11/site-packages/IPython/core/magics/basic.py:300, in BasicMagics.page(self, parameter_s)
    298 info = self.shell._ofind(oname)
    299 print(info)
--> 300 if info['found']:
    301     if raw:
    302         txt = str(info["obj"])

TypeError: 'OInfo' object is not subscriptable`



Discussion:

Resolving patch:
diff --git a/IPython/core/magics/basic.py b/IPython/core/magics/basic.py
--- a/IPython/core/magics/basic.py
+++ b/IPython/core/magics/basic.py
@@ -296,11 +296,11 @@ def page(self, parameter_s=''):
 
         oname = args and args or '_'
         info = self.shell._ofind(oname)
-        if info['found']:
+        if info.found:
             if raw:
-                txt = str(info["obj"])
+                txt = str(info.obj)
             else:
-                txt = pformat(info["obj"])
+                txt = pformat(info.obj)
             page.page(txt)
         else:
             print('Object `%s` not found' % oname)


Test patch:
Repository: ipython/ipython

Issue:
Proposal: re-trigger autosuggestion with `right` key (and similar)
Once auto-suggestion was discarded it is not currently possible to re-trigger hinting in place (user needs to to type another character and press backspace, or to delete last character and type it again). This is a proposal to re-trigger the suggestions with the same set of keys as is used to accept them (<kbd>right</kbd>, <kbd>ctrl</kbd> + <kbd>f</kbd> etc).

To avoid accidental acceptance of newly shown suggestion a configurable debouncer of 200ms could be activated when suggestion is re-triggered using the proposed mechanism.

It is not currently possible to opt-in for the proposed behaviour by defining a custom shortcut.

Can I ask anyone interested to indicate if they would be happy with an addition of such a shortcut by default (:+1: or :-1: )?


Discussion:

Resolving patch:
diff --git a/IPython/terminal/shortcuts/__init__.py b/IPython/terminal/shortcuts/__init__.py
--- a/IPython/terminal/shortcuts/__init__.py
+++ b/IPython/terminal/shortcuts/__init__.py
@@ -277,10 +277,7 @@ def create_identifier(handler: Callable):
     Binding(
         auto_suggest.resume_hinting,
         ["right"],
-        # For now this binding is inactive (the filter includes `never`).
-        # TODO: remove `never` if we reach a consensus in #13991
-        # TODO: use `emacs_like_insert_mode` once #13991 is in
-        "never & default_buffer_focused & ((vi_insert_mode & ebivim) | emacs_insert_mode)",
+        "default_buffer_focused & emacs_like_insert_mode",
     ),
 ]
 


Test patch:
Repository: ipython/ipython

Issue:
IPython can't provide completions for second level modules when Jedi completions are disabled
This problem was reported in [Spyder](https://github.com/spyder-ide/spyder/issues/20381#issuecomment-1419243534) and I was able to reproduce it locally. Basically, if you run

```python
In [1]: import numpy as np

In [2]: np.linalg.<Tab>
```

you won't get completions for the `linalg` module.

After a bit of testing, I found that this problem doesn't show up if Jedi completions are enabled. But we have them disabled by default in Spyder because they introduce serious lags when working with large dataframes.

It'd be nice if this could be fixed for the next IPython release and I'm able to put some time to do it. @krassowski, could you let me know where to start looking for a possible solution? Thanks!


Discussion:
Quick answer, since:

```python
%config IPCompleter.use_jedi=False
```

does not work, but:

```python
%config IPCompleter.use_jedi=False
%config IPCompleter.evaluation='unsafe'
```

seems to work ok (please confirm), I would say that we may need to relax the guards in default evaluation mode.

In the default limited evaluation mode we allow:

https://github.com/ipython/ipython/blob/47d043e1dd051294d85f41b18f8e3c1f8434fd5c/IPython/core/guarded_eval.py#L705-L719

and in unsafe:

https://github.com/ipython/ipython/blob/47d043e1dd051294d85f41b18f8e3c1f8434fd5c/IPython/core/guarded_eval.py#L720-L728

This looks related to some `getattr` rule, but needs debugging as I don't immediately see why it does not work on second level.

> seems to work ok (please confirm)

Yep, I confirm that's the case.

> This looks related to some getattr rule, but needs debugging as I don't immediately see why it does not work on second level.

Ok, I'll take a look at it. Thanks for the pointer.

Resolving patch:
diff --git a/IPython/core/guarded_eval.py b/IPython/core/guarded_eval.py
--- a/IPython/core/guarded_eval.py
+++ b/IPython/core/guarded_eval.py
@@ -637,6 +637,7 @@ def _list_methods(cls, source=None):
 
 dict_keys: Type[collections.abc.KeysView] = type({}.keys())
 method_descriptor: Any = type(list.copy)
+module = type(builtins)
 
 NUMERICS = {int, float, complex}
 
@@ -686,6 +687,7 @@ def _list_methods(cls, source=None):
     *NUMERICS,
     dict_keys,
     method_descriptor,
+    module,
 }
 
 


Test patch:
diff --git a/IPython/core/tests/test_guarded_eval.py b/IPython/core/tests/test_guarded_eval.py
--- a/IPython/core/tests/test_guarded_eval.py
+++ b/IPython/core/tests/test_guarded_eval.py
@@ -568,3 +568,15 @@ class B(NamedTuple):
         pass
 
     assert A.__getitem__ == B.__getitem__
+
+
+@dec.skip_without("numpy")
+def test_module_access():
+    import numpy
+
+    context = limited(numpy=numpy)
+    assert guarded_eval("numpy.linalg.norm", context) == numpy.linalg.norm
+
+    context = minimal(numpy=numpy)
+    with pytest.raises(GuardRejection):
+        guarded_eval("np.linalg.norm", context)
Repository: ipython/ipython

Issue:
Right arrow broken in 8.12.1
The -> arrow doesn't work. When I press the <- arrow to go back and edit the current command, Ipython ignores it.

I don't think my explanation is very clear, so here is an example. If I type:

```
In [1]: print(A
```
And realise I forgot to open the quotes, I go back,

```
In [1]: print("|A
```
Now I am stuck, and cannot "hop" over A. I can keep moving backwards, though. Pressing `end` does work.
 
I have replicated it in an empty virtualenv under both Python 3.9 and 3.10.  The problem disappears downgrading to Ipython 8.12.0.
I am using Fedora Linux, with Qterminal 1.1.0.


Discussion:

Resolving patch:
diff --git a/IPython/terminal/shortcuts/__init__.py b/IPython/terminal/shortcuts/__init__.py
--- a/IPython/terminal/shortcuts/__init__.py
+++ b/IPython/terminal/shortcuts/__init__.py
@@ -277,7 +277,9 @@ def create_identifier(handler: Callable):
     Binding(
         auto_suggest.resume_hinting,
         ["right"],
-        "default_buffer_focused & emacs_like_insert_mode",
+        "is_cursor_at_the_end_of_line"
+        " & default_buffer_focused"
+        " & emacs_like_insert_mode",
     ),
 ]
 
diff --git a/IPython/terminal/shortcuts/filters.py b/IPython/terminal/shortcuts/filters.py
--- a/IPython/terminal/shortcuts/filters.py
+++ b/IPython/terminal/shortcuts/filters.py
@@ -52,6 +52,13 @@ def has_line_below() -> bool:
     return document.cursor_position_row < len(document.lines) - 1
 
 
+@undoc
+@Condition
+def is_cursor_at_the_end_of_line() -> bool:
+    document = get_app().current_buffer.document
+    return document.is_cursor_at_the_end_of_line
+
+
 @undoc
 @Condition
 def has_line_above() -> bool:
@@ -179,6 +186,7 @@ def is_windows_os():
     "never": Never(),
     "has_line_below": has_line_below,
     "has_line_above": has_line_above,
+    "is_cursor_at_the_end_of_line": is_cursor_at_the_end_of_line,
     "has_selection": has_selection,
     "has_suggestion": has_suggestion,
     "vi_mode": vi_mode,


Test patch:
Repository: ipython/ipython

Issue:
Update type(...) to import the Type from it's canonical location
I missed that in the previous PR but those should likely be :

```
from types import MethodDescriptorType
from types import ModuleType
```



_Originally posted by @Carreau in https://github.com/ipython/ipython/pull/14029#discussion_r1180062854_
            


Discussion:

Resolving patch:
diff --git a/IPython/core/guarded_eval.py b/IPython/core/guarded_eval.py
--- a/IPython/core/guarded_eval.py
+++ b/IPython/core/guarded_eval.py
@@ -18,6 +18,7 @@
 import sys
 from functools import cached_property
 from dataclasses import dataclass, field
+from types import MethodDescriptorType, ModuleType
 
 from IPython.utils.docs import GENERATING_DOCUMENTATION
 from IPython.utils.decorators import undoc
@@ -630,8 +631,6 @@ def _list_methods(cls, source=None):
 
 
 dict_keys: Type[collections.abc.KeysView] = type({}.keys())
-method_descriptor: Any = type(list.copy)
-module = type(builtins)
 
 NUMERICS = {int, float, complex}
 
@@ -680,8 +679,8 @@ def _list_methods(cls, source=None):
     type,  # `type` handles a lot of generic cases, e.g. numbers as in `int.real`.
     *NUMERICS,
     dict_keys,
-    method_descriptor,
-    module,
+    MethodDescriptorType,
+    ModuleType,
 }
 
 


Test patch:
Repository: ipython/ipython

Issue:
Suppress output with semicolon does not work with store_history=False
Steps to reproduce:
```
In [4]: ip.run_cell('1;')
Out[4]: 1
Out[4]: <ExecutionResult object at ...
```



Discussion:

Resolving patch:
diff --git a/IPython/core/displayhook.py b/IPython/core/displayhook.py
--- a/IPython/core/displayhook.py
+++ b/IPython/core/displayhook.py
@@ -51,7 +51,7 @@ def __init__(self, shell=None, cache_size=1000, **kwargs):
 
         # we need a reference to the user-level namespace
         self.shell = shell
-        
+
         self._,self.__,self.___ = '','',''
 
         # these are deliberately global:
@@ -83,15 +83,9 @@ def check_for_underscore(self):
 
     def quiet(self):
         """Should we silence the display hook because of ';'?"""
-        # do not print output if input ends in ';'
-        
-        try:
-            cell = self.shell.history_manager.input_hist_parsed[-1]
-        except IndexError:
-            # some uses of ipshellembed may fail here
-            return False
-        
-        return self.semicolon_at_end_of_expression(cell)
+        if self.exec_result is not None:
+            return self.semicolon_at_end_of_expression(self.exec_result.info.raw_cell)
+        return False
 
     @staticmethod
     def semicolon_at_end_of_expression(expression):
@@ -280,13 +274,12 @@ def cull_cache(self):
         cull_count = max(int(sz * self.cull_fraction), 2)
         warn('Output cache limit (currently {sz} entries) hit.\n'
              'Flushing oldest {cull_count} entries.'.format(sz=sz, cull_count=cull_count))
-        
+
         for i, n in enumerate(sorted(oh)):
             if i >= cull_count:
                 break
             self.shell.user_ns.pop('_%i' % n, None)
             oh.pop(n, None)
-        
 
     def flush(self):
         if not self.do_full_cache:


Test patch:
Repository: ipython/ipython

Issue:
Unable to run `ipapp.py`
There is a problem with the `ipython/terminal/ipapp.py` file.

On the first line is this:
```python
#!/usr/bin/env python
```
this is an mistake, because further down in the code there is a relative import that makes the file not executable with `./ipapp.py`.
So please remove `#!/usr/bin/env python`.

Steps to reproduce:

```
# download repo
cd /tmp/
git clone https://github.com/ipython/ipython.git

# run file
cd ipython/IPython/terminal/
./ipapp.py
```

Example of error:
```
Traceback (most recent call last):
  File "/tmp/ipython/IPython/terminal/./ipapp.py", line 36, in <module>
    from .interactiveshell import TerminalInteractiveShell
ImportError: attempted relative import with no known parent package
```


Discussion:
Oh, yeah, that's 10+ year old code. 

Do you want to send a PR that remove the `#!`, and the the `if __name__ ==` ... at the end of the file ? 

Resolving patch:
diff --git a/IPython/terminal/ipapp.py b/IPython/terminal/ipapp.py
--- a/IPython/terminal/ipapp.py
+++ b/IPython/terminal/ipapp.py
@@ -1,4 +1,3 @@
-#!/usr/bin/env python
 # encoding: utf-8
 """
 The :class:`~traitlets.config.application.Application` object for the command
@@ -337,7 +336,3 @@ def load_default_config(ipython_dir=None):
     return app.config
 
 launch_new_instance = TerminalIPythonApp.launch_instance
-
-
-if __name__ == '__main__':
-    launch_new_instance()


Test patch:
Repository: ipython/ipython

Issue:
atexit_operations fails to delete tempdir due to vim undofile leftovers (`*.un~`)
Steps to reproduce:

0. Set EDITOR to `vim`  
1. define function `def foo(): return 1`
3. edit it by `%edit foo`, do some stuff in file while vim set to use undofile `:set undofile` (this is the default setting on Vim 8.2 for windows) and save
5. exit IPython

```
In [1]: def foo():
   ...:     return 1
   ...: 
In [2]: edit foo
Editing In[1]
IPython will make a temporary file named: C:\Users\idank\AppData\Local\Temp\ipython_edit_lfo_g8nb\ipython_edit_4yzyx_uz.py
done. Executing edited code...
Out[2]: 'def foo():\n\n    return 2\n'
In [3]: exit
Exception ignored in atexit callback: <bound method InteractiveShell.atexit_operations of <IPython.terminal.interactiveshell.TerminalInteractiveShell object at 0x0000024205566010>>
Traceback (most recent call last):
  File "C:\Users\idank\mambaforge\envs\py311\Lib\site-packages\IPython\core\interactiveshell.py", line 3887, in atexit_operations
    tdir.rmdir()
  File "C:\Users\idank\mambaforge\envs\py311\Lib\pathlib.py", line 1156, in rmdir
    os.rmdir(self)
OSError: [WinError 145] The directory is not empty: 'C:\\Users\\idank\\AppData\\Local\\Temp\\ipython_edit_lfo_g8nb'
```

Windows 10, IPython 8.14.0

The problem is that vim would leave a `*.un~` file with the undo history in the temp directory which won't get deleted by IPython, then pathlib.Path.rmdir would fail in [interactiveshell.py:3887](https://github.com/ipython/ipython/blob/cf7c266435424f43ed48c91239f895e8ad739266/IPython/core/interactiveshell.py#LL3887C24-L3887C29), (`shutil.rmtree` the entire directory?)


Discussion:
Hi,

I would like to take care of it for this hacktober fest.

Resolving patch:
diff --git a/IPython/core/interactiveshell.py b/IPython/core/interactiveshell.py
--- a/IPython/core/interactiveshell.py
+++ b/IPython/core/interactiveshell.py
@@ -21,6 +21,7 @@
 import os
 import re
 import runpy
+import shutil
 import subprocess
 import sys
 import tempfile
@@ -3902,7 +3903,7 @@ def atexit_operations(self):
         del self.tempfiles
         for tdir in self.tempdirs:
             try:
-                tdir.rmdir()
+                shutil.rmtree(tdir)
                 self.tempdirs.remove(tdir)
             except FileNotFoundError:
                 pass


Test patch:
Repository: ipython/ipython

Issue:
Memory leak with %matplotlib qt, with simple fix
When using matplotlib event loop integration (e.g. %matplotlib) with pyqt, there is a memory leak caused by a misunderstanding of the lifespan of QObjects.

I found it because I'm trying to debug slowdowns in long-running IPython sessions with autoreload. I'm not sure that this is actually the cause, but it's certainly ugly. Within a session which has been open for a couple of weeks I found:
```
>>> collections.Counter(map(type, gc.get_objects())).most_common(5)
[(PyQt5.QtCore.QEventLoop, 1718061),
 (dict, 1006864),
 (list, 702602),
 (ast.Name, 267460),
 (ast.Attribute, 96929)]
```
There are 1.7m QEventLoop instances kicking around. They are being created by `IPython.terminal.pt_inputhooks.qt.inputhook` on line 58, with `event_loop = QtCore.QEventLoop(app)`. But QEventLoop is a QObject subclass, which means that passing `app` in the constructor parents the instance to `app`, so even when it exits the scope it won't be deleted, even though it's no longer needed by that point.

The fix is simple: just add a line at the end of that function containing `event_loop.setParent(None)`.

Hopefully it's faster for you to just add that single line of code rather than me forking, making a pull request etc, but please tell me if you need me to do so.


Discussion:
> The fix is simple: just add a line at the end of that function containing event_loop.setParent(None).

Thanks for finding a fix @pag!

> Hopefully it's faster for you to just add that single line of code rather than me forking, making a pull request etc, but please tell me if you need me to do so.

Unfortunately, it's not because we're busy with other things. So, please submit a pull request to fix this issue.

Resolving patch:
diff --git a/IPython/terminal/pt_inputhooks/qt.py b/IPython/terminal/pt_inputhooks/qt.py
--- a/IPython/terminal/pt_inputhooks/qt.py
+++ b/IPython/terminal/pt_inputhooks/qt.py
@@ -84,3 +84,7 @@ def inputhook(context):
                 _exec(event_loop)
         finally:
             notifier.setEnabled(False)
+
+    # This makes sure that the event loop is garbage collected.
+    # See issue 14240.
+    event_loop.setParent(None)


Test patch:
Repository: ipython/ipython

Issue:
IndexError with empty system commands
This is an simple issue. When typing `!`, the following IndexError will be thrown

```
 File IPython/core/interactiveshell.py:2603, in InteractiveShell.system_raw(self, cmd)

   2601 cmd = self.var_expand(cmd, depth=1)
   2602 # warn if there is an IPython magic alternative.
-> 2603 main_cmd = cmd.split()[0]
   2604 has_magic_alternatives = ("pip", "conda", "cd")
   2606 if main_cmd in has_magic_alternatives:

IndexError: list index out of range
```
due to an empty `cmd`. 

I am not sure if that's an bug, since the fix could be trivial.


Discussion:

Resolving patch:
diff --git a/IPython/core/interactiveshell.py b/IPython/core/interactiveshell.py
--- a/IPython/core/interactiveshell.py
+++ b/IPython/core/interactiveshell.py
@@ -2639,7 +2639,10 @@ def system_raw(self, cmd):
         """
         cmd = self.var_expand(cmd, depth=1)
         # warn if there is an IPython magic alternative.
-        main_cmd = cmd.split()[0]
+        if cmd == "":
+            main_cmd = ""
+        else:
+            main_cmd = cmd.split()[0]
         has_magic_alternatives = ("pip", "conda", "cd")
 
         if main_cmd in has_magic_alternatives:


Test patch: