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

Issue:
latex_to_png fails on windows
The `latex_to_png` command fails in the method `latex_to_png_dvipng`. The reason is the temporary file that is generated, contains a `~` in the path, which is not accepted by the latex command.

The temporary file generated is ` 'C:\\Users\\EENDEB~1\\AppData\\Local\\Temp\\tmpxefqozm3\\tmp.tex'`
The command that fails is:
```
            subprocess.check_call(
                ["latex", "-halt-on-error", tmpfile,
                cwd=workdir, stdout=devnull, stderr=devnull)
```
Locally I can fix this by changing the command to
```
            subprocess.check_call(
                ["latex", "-halt-on-error",  os.path.split(tmpfile)[-1]],
                cwd=workdir, stdout=devnull, stderr=devnull)
```
(only the last part is required, since the `cwd` is also set).

To make this work some other calls need to be modified as well 


<!-- This is the repository for IPython command line, if you can try to make sure this question/bug/feature belong here and not on one of the Jupyter repositories. 

If it's a generic Python/Jupyter question, try other forums or discourse.jupyter.org.

If you are unsure, it's ok to post here, though, there are few maintainer so you might not get a fast response. 

-->



Discussion:
I think you should do a pull request to fix this.

Resolving patch:
diff --git a/IPython/lib/latextools.py b/IPython/lib/latextools.py
--- a/IPython/lib/latextools.py
+++ b/IPython/lib/latextools.py
@@ -146,11 +146,11 @@ def latex_to_png_dvipng(s, wrap, color='Black', scale=1.0):
         return None
     try:
         workdir = Path(tempfile.mkdtemp())
-        tmpfile = workdir.joinpath("tmp.tex")
-        dvifile = workdir.joinpath("tmp.dvi")
-        outfile = workdir.joinpath("tmp.png")
+        tmpfile = "tmp.tex"
+        dvifile = "tmp.dvi"
+        outfile = "tmp.png"
 
-        with tmpfile.open("w", encoding="utf8") as f:
+        with workdir.joinpath(tmpfile).open("w", encoding="utf8") as f:
             f.writelines(genelatex(s, wrap))
 
         with open(os.devnull, 'wb') as devnull:
@@ -181,7 +181,7 @@ def latex_to_png_dvipng(s, wrap, color='Black', scale=1.0):
                 stderr=devnull,
             )
 
-        with outfile.open("rb") as f:
+        with workdir.joinpath(outfile).open("rb") as f:
             return f.read()
     except subprocess.CalledProcessError:
         return None


Test patch:
Repository: ipython/ipython

Issue:
Given inexistent file X, "%run X" reports that it's unable to open "X.py"
... which confuses users. See #13542.


Discussion:

Resolving patch:
diff --git a/IPython/utils/path.py b/IPython/utils/path.py
--- a/IPython/utils/path.py
+++ b/IPython/utils/path.py
@@ -83,12 +83,13 @@ def get_py_filename(name):
     """
 
     name = os.path.expanduser(name)
-    if not os.path.isfile(name) and not name.endswith('.py'):
-        name += '.py'
     if os.path.isfile(name):
         return name
-    else:
-        raise IOError('File `%r` not found.' % name)
+    if not name.endswith(".py"):
+        py_name = name + ".py"
+        if os.path.isfile(py_name):
+            return py_name
+    raise IOError("File `%r` not found." % name)
 
 
 def filefind(filename: str, path_dirs=None) -> str:


Test patch:
Repository: ipython/ipython

Issue:
`auto_match` slow and hogs CPU in some cases
To reproduce in IPython 8.2, paste `function_with_long_nameeee('arg`, and add the closing quotes at the end.
The slowness increases when the prefix is longer (in this case dominated by the function name `function_with_long_nameeee`), so if you don't notice it, try increasing it).

## Steps to reproduce with docker

1. Build the image and start a container:

```sh
build_oci_img() {
  docker build "$@" - <<EOF
  FROM python:3.8-slim
  RUN pip install ipython==8.2
EOF
}

build_oci_img
docker run --rm --interactive --tty "$(build_oci_img -q)" ipython -i -c 'get_ipython().auto_match = True'

```

2. Paste `function_with_long_nameeee('arg` and then type another `'` (single quote)


Discussion:
Just tested 8.3 and I see the same behavior
Oh my, the lag. It hurts.

For anyone reproducing: make sure to `get_ipython().auto_match = True` first.

Resolving patch:
diff --git a/IPython/terminal/shortcuts.py b/IPython/terminal/shortcuts.py
--- a/IPython/terminal/shortcuts.py
+++ b/IPython/terminal/shortcuts.py
@@ -106,20 +106,40 @@ def reformat_and_execute(event):
     def auto_match():
         return shell.auto_match
 
+    def all_quotes_paired(quote, buf):
+        paired = True
+        i = 0
+        while i < len(buf):
+            c = buf[i]
+            if c == quote:
+                paired = not paired
+            elif c == "\\":
+                i += 1
+            i += 1
+        return paired
+
     focused_insert = (vi_insert_mode | emacs_insert_mode) & has_focus(DEFAULT_BUFFER)
     _preceding_text_cache = {}
     _following_text_cache = {}
 
     def preceding_text(pattern):
-        try:
+        if pattern in _preceding_text_cache:
             return _preceding_text_cache[pattern]
-        except KeyError:
-            pass
-        m = re.compile(pattern)
 
-        def _preceding_text():
-            app = get_app()
-            return bool(m.match(app.current_buffer.document.current_line_before_cursor))
+        if callable(pattern):
+
+            def _preceding_text():
+                app = get_app()
+                before_cursor = app.current_buffer.document.current_line_before_cursor
+                return bool(pattern(before_cursor))
+
+        else:
+            m = re.compile(pattern)
+
+            def _preceding_text():
+                app = get_app()
+                before_cursor = app.current_buffer.document.current_line_before_cursor
+                return bool(m.match(before_cursor))
 
         condition = Condition(_preceding_text)
         _preceding_text_cache[pattern] = condition
@@ -173,6 +193,7 @@ def _(event):
         filter=focused_insert
         & auto_match
         & not_inside_unclosed_string
+        & preceding_text(lambda line: all_quotes_paired('"', line))
         & following_text(r"[,)}\]]|$"),
     )
     def _(event):
@@ -184,6 +205,7 @@ def _(event):
         filter=focused_insert
         & auto_match
         & not_inside_unclosed_string
+        & preceding_text(lambda line: all_quotes_paired("'", line))
         & following_text(r"[,)}\]]|$"),
     )
     def _(event):


Test patch:
Repository: ipython/ipython

Issue:
`auto_match` slow and hogs CPU in some cases
To reproduce in IPython 8.2, paste `function_with_long_nameeee('arg`, and add the closing quotes at the end.
The slowness increases when the prefix is longer (in this case dominated by the function name `function_with_long_nameeee`), so if you don't notice it, try increasing it).

## Steps to reproduce with docker

1. Build the image and start a container:

```sh
build_oci_img() {
  docker build "$@" - <<EOF
  FROM python:3.8-slim
  RUN pip install ipython==8.2
EOF
}

build_oci_img
docker run --rm --interactive --tty "$(build_oci_img -q)" ipython -i -c 'get_ipython().auto_match = True'

```

2. Paste `function_with_long_nameeee('arg` and then type another `'` (single quote)


Discussion:

Resolving patch:
diff --git a/IPython/terminal/shortcuts.py b/IPython/terminal/shortcuts.py
--- a/IPython/terminal/shortcuts.py
+++ b/IPython/terminal/shortcuts.py
@@ -140,6 +140,18 @@ def _following_text():
         _following_text_cache[pattern] = condition
         return condition
 
+    @Condition
+    def not_inside_unclosed_string():
+        app = get_app()
+        s = app.current_buffer.document.text_before_cursor
+        # remove escaped quotes
+        s = s.replace('\\"', "").replace("\\'", "")
+        # remove triple-quoted string literals
+        s = re.sub(r"(?:\"\"\"[\s\S]*\"\"\"|'''[\s\S]*''')", "", s)
+        # remove single-quoted string literals
+        s = re.sub(r"""(?:"[^"]*["\n]|'[^']*['\n])""", "", s)
+        return not ('"' in s or "'" in s)
+
     # auto match
     @kb.add("(", filter=focused_insert & auto_match & following_text(r"[,)}\]]|$"))
     def _(event):
@@ -160,7 +172,7 @@ def _(event):
         '"',
         filter=focused_insert
         & auto_match
-        & preceding_text(r'^([^"]+|"[^"]*")*$')
+        & not_inside_unclosed_string
         & following_text(r"[,)}\]]|$"),
     )
     def _(event):
@@ -171,13 +183,35 @@ def _(event):
         "'",
         filter=focused_insert
         & auto_match
-        & preceding_text(r"^([^']+|'[^']*')*$")
+        & not_inside_unclosed_string
         & following_text(r"[,)}\]]|$"),
     )
     def _(event):
         event.current_buffer.insert_text("''")
         event.current_buffer.cursor_left()
 
+    @kb.add(
+        '"',
+        filter=focused_insert
+        & auto_match
+        & not_inside_unclosed_string
+        & preceding_text(r'^.*""$'),
+    )
+    def _(event):
+        event.current_buffer.insert_text('""""')
+        event.current_buffer.cursor_left(3)
+
+    @kb.add(
+        "'",
+        filter=focused_insert
+        & auto_match
+        & not_inside_unclosed_string
+        & preceding_text(r"^.*''$"),
+    )
+    def _(event):
+        event.current_buffer.insert_text("''''")
+        event.current_buffer.cursor_left(3)
+
     # raw string
     @kb.add(
         "(", filter=focused_insert & auto_match & preceding_text(r".*(r|R)[\"'](-*)$")


Test patch:
Repository: ipython/ipython

Issue:
Rename `master` branch `main`
### Description

You currently use the branch name `master` for this repository for historical reasons. However, the open source community has moved on from this, the GitHub default is now `main`, and `main` is 2-fewer letters to type :-)    

### Guidance

Renaming a branch will automatically update all open PRs, so this shouldn't be too disruptive as long as your dev community knows to update their local branches: https://github.com/github/renaming

We've done it for several projects and it went very smoothly.


Discussion:

Resolving patch:
diff --git a/IPython/__init__.py b/IPython/__init__.py
--- a/IPython/__init__.py
+++ b/IPython/__init__.py
@@ -38,7 +38,7 @@
 
 See IPython `README.rst` file for more information:
 
-    https://github.com/ipython/ipython/blob/master/README.rst
+    https://github.com/ipython/ipython/blob/main/README.rst
 
 """
     )
diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -48,7 +48,7 @@
 
 See IPython `README.rst` file for more information:
 
-    https://github.com/ipython/ipython/blob/master/README.rst
+    https://github.com/ipython/ipython/blob/main/README.rst
 
 Python {py} detected.
 {pip}
diff --git a/tools/github_stats.py b/tools/github_stats.py
--- a/tools/github_stats.py
+++ b/tools/github_stats.py
@@ -79,8 +79,8 @@ def issues_closed_since(period=timedelta(days=365), project="ipython/ipython", p
     filtered = [ i for i in allclosed if _parse_datetime(i['closed_at']) > since ]
     if pulls:
         filtered = [ i for i in filtered if _parse_datetime(i['merged_at']) > since ]
-        # filter out PRs not against master (backports)
-        filtered = [ i for i in filtered if i['base']['ref'] == 'master' ]
+        # filter out PRs not against main (backports)
+        filtered = [ i for i in filtered if i['base']['ref'] == 'main' ]
     else:
         filtered = [ i for i in filtered if not is_pull_request(i) ]
     


Test patch:
diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml
--- a/.github/workflows/test.yml
+++ b/.github/workflows/test.yml
@@ -4,7 +4,6 @@ on:
   push:
     branches:
       - main
-      - master
       - '*.x'
   pull_request:
   # Run weekly on Monday at 1:23 UTC
Repository: ipython/ipython

Issue:
Enable webagg in %matplotlib magic
It seems somehow `%matplotlib webagg` no longer works. 

```
File ipy/lib/python3.10/site-packages/IPython/core/pylabtools.py:321, in find_gui_and_backend(gui, gui_select)
    317 import matplotlib
    319 if gui and gui != 'auto':
    320     # select backend based on requested gui
--> 321     backend = backends[gui]
    322     if gui == 'agg':
    323         gui = None

KeyError: 'webagg'
```

Could we add webagg backend for selection? It needs just adding the key to the dictionary `backends` in `pylabtools.py`.


Discussion:

Resolving patch:
diff --git a/IPython/core/pylabtools.py b/IPython/core/pylabtools.py
--- a/IPython/core/pylabtools.py
+++ b/IPython/core/pylabtools.py
@@ -26,6 +26,7 @@
     "qt": "Qt5Agg",
     "osx": "MacOSX",
     "nbagg": "nbAgg",
+    "webagg": "WebAgg",
     "notebook": "nbAgg",
     "agg": "agg",
     "svg": "svg",
diff --git a/IPython/terminal/interactiveshell.py b/IPython/terminal/interactiveshell.py
--- a/IPython/terminal/interactiveshell.py
+++ b/IPython/terminal/interactiveshell.py
@@ -711,9 +711,8 @@ def inputhook(self, context):
 
     active_eventloop = None
     def enable_gui(self, gui=None):
-        if gui and (gui != 'inline') :
-            self.active_eventloop, self._inputhook =\
-                get_inputhook_name_and_func(gui)
+        if gui and (gui not in {"inline", "webagg"}):
+            self.active_eventloop, self._inputhook = get_inputhook_name_and_func(gui)
         else:
             self.active_eventloop = self._inputhook = None
 


Test patch:
Repository: ipython/ipython

Issue:
%cpaste still stores itself in history (unlike %paste)
As noted in [this comment](https://github.com/ipython/ipython/issues/13622#issuecomment-1091721895), `%paste` was corrected to store its _contents_, and not its command equivalent itself, in history.  But `cpaste` still does the latter:

```python
Python 3.10.8 (main, Oct 13 2022, 10:17:43) [Clang 14.0.0 (clang-1400.0.29.102)]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.6.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: %cpaste -q
print('test')
print('Will this be stored?')
--
test
Will this be stored?

In [2]: In[1]
Out[2]: "get_ipython().run_line_magic('cpaste', '-q')"
```


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
@@ -3138,8 +3138,12 @@ def error_before_exec(value):
             else:
                 cell = raw_cell
 
+        # Do NOT store paste/cpaste magic history
+        if "get_ipython().run_line_magic(" in cell and "paste" in cell:
+            store_history = False
+
         # Store raw and processed history
-        if store_history and raw_cell.strip(" %") != "paste":
+        if store_history:
             self.history_manager.store_inputs(self.execution_count, cell, raw_cell)
         if not silent:
             self.logger.log(cell, raw_cell)
diff --git a/IPython/terminal/magics.py b/IPython/terminal/magics.py
--- a/IPython/terminal/magics.py
+++ b/IPython/terminal/magics.py
@@ -147,7 +147,7 @@ def cpaste(self, parameter_s=''):
 
         sentinel = opts.get('s', u'--')
         block = '\n'.join(get_pasted_lines(sentinel, quiet=quiet))
-        self.store_or_execute(block, name, store_history=False)
+        self.store_or_execute(block, name, store_history=True)
 
     @line_magic
     def paste(self, parameter_s=''):


Test patch:
Repository: ipython/ipython

Issue:
Problems with the new auto suggestion keybindings in #13888
I'm using a git build of IPython and found two problems with autosuggestion related to #13888.

1. In Vi mode, when there is a suggestion displayed, `escape` will only discard the suggestion, but will not enter the Vi normal mode like it usually does. I find it more reasonable if both actions are performed.
2. If I type some words so a suggestion pops up, and then press `left` followed by `right`, there will be one spurious character from the suggestion inserted at the wrong position.

Example (using `|` to indicate cursor position)
```
("123" is the suggestion)
In [1]: a=|123

(after pressing left, the suggestion disappears)
In [1]: a|=

(after pressing right, an extra character 1 is inserted)
In [1]: a1|=
```


Discussion:
Thanks for testing and the quick bug report ! We'll try to fix that quickly. 

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
@@ -363,9 +363,9 @@ def navigable_suggestions():
     kb.add("c-right", filter=has_suggestion & has_focus(DEFAULT_BUFFER))(
         auto_suggest.accept_token
     )
-    kb.add("escape", filter=has_suggestion & has_focus(DEFAULT_BUFFER))(
-        auto_suggest.discard
-    )
+    kb.add(
+        "escape", filter=has_suggestion & has_focus(DEFAULT_BUFFER) & emacs_insert_mode
+    )(auto_suggest.discard)
     kb.add(
         "up",
         filter=navigable_suggestions
@@ -451,7 +451,6 @@ def set_input_mode(self, mode):
     if shell.editing_mode == "vi" and shell.modal_cursor:
         ViState._input_mode = InputMode.INSERT  # type: ignore
         ViState.input_mode = property(get_input_mode, set_input_mode)  # type: ignore
-
     return kb
 
 
diff --git a/IPython/terminal/shortcuts/auto_suggest.py b/IPython/terminal/shortcuts/auto_suggest.py
--- a/IPython/terminal/shortcuts/auto_suggest.py
+++ b/IPython/terminal/shortcuts/auto_suggest.py
@@ -75,6 +75,7 @@ def connect(self, pt_app: PromptSession):
         # note: `on_text_changed` could be used for a bit different behaviour
         # on character deletion (i.e. reseting history position on backspace)
         pt_app.default_buffer.on_text_insert.add_handler(self.reset_history_position)
+        pt_app.default_buffer.on_cursor_position_changed.add_handler(self._dismiss)
 
     def get_suggestion(
         self, buffer: Buffer, document: Document
@@ -89,6 +90,9 @@ def get_suggestion(
 
         return None
 
+    def _dismiss(self, buffer, *args, **kwargs):
+        buffer.suggestion = None
+
     def _find_match(
         self, text: str, skip_lines: float, history: History, previous: bool
     ) -> Generator[Tuple[str, float], None, None]:


Test patch:
Repository: ipython/ipython

Issue:
'Bad file descriptor' error logged in atexit call back 
**Issue:**  Following log lines are observed in stderr on daemonizing a process with `IPython.utils.io` as an import.
> Exception ignored in atexit callback: <built-in method close of _io.TextIOWrapper object at 0x7f19e8aa9b10>
> OSError: [Errno 9] Bad file descriptor


**Reproducer:**
```python
import daemon
import sys
import IPython.utils.io

file_descriptors_to_preserve = []
with daemon.DaemonContext(
    prevent_core=False,
    signal_map={},
    stderr=sys.stderr,
    stdout=sys.stdout,
    files_preserve=file_descriptors_to_preserve,
):
    pass 
```

**Root cause:**
This is due to the following lines in I**Python.utils.io module**
```python
# setup stdin/stdout/stderr to sys.stdin/sys.stdout/sys.stderr
devnull = open(os.devnull, "w", encoding="utf-8")
atexit.register(devnull.close)
```
After the child process is launched, all the files opened by the parent process are closed(except for stderr, stdout). While closing all the files, devnull opened in the IPython.utils.io module is also closed. On process exit atexit module runs all the functions that were registered. Since devnull close is registered, atexit handler is trying to close the already closed file which is generating ' Bad file descriptor' log in stderr


Discussion:

Resolving patch:
diff --git a/IPython/utils/io.py b/IPython/utils/io.py
--- a/IPython/utils/io.py
+++ b/IPython/utils/io.py
@@ -18,11 +18,6 @@
 from IPython.utils.decorators import undoc
 from .capture import CapturedIO, capture_output
 
-# setup stdin/stdout/stderr to sys.stdin/sys.stdout/sys.stderr
-devnull = open(os.devnull, "w", encoding="utf-8")
-atexit.register(devnull.close)
-
-
 class Tee(object):
     """A class to duplicate an output stream to stdout/err.
 


Test patch:
Repository: ipython/ipython

Issue:
Remove newline_with_copy_margin.
It has been deprecated for at least 2 versions. We can remove this.

_Originally posted by @Carreau in https://github.com/ipython/ipython/pull/13888#discussion_r1071293652_
            


Discussion:
@Yann-P want to give it a shot ? 

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
@@ -492,37 +492,10 @@ def indent_buffer(event):
     event.current_buffer.insert_text(" " * 4)
 
 
-@undoc
-def newline_with_copy_margin(event):
-    """
-    DEPRECATED since IPython 6.0
-
-    See :any:`newline_autoindent_outer` for a replacement.
-
-    Preserve margin and cursor position when using
-    Control-O to insert a newline in EMACS mode
-    """
-    warnings.warn(
-        "`newline_with_copy_margin(event)` is deprecated since IPython 6.0. "
-        "see `newline_autoindent_outer(shell)(event)` for a replacement.",
-        DeprecationWarning,
-        stacklevel=2,
-    )
-
-    b = event.current_buffer
-    cursor_start_pos = b.document.cursor_position_col
-    b.newline(copy_margin=True)
-    b.cursor_up(count=1)
-    cursor_end_pos = b.document.cursor_position_col
-    if cursor_start_pos != cursor_end_pos:
-        pos_diff = cursor_start_pos - cursor_end_pos
-        b.cursor_right(count=pos_diff)
-
-
 def newline_autoindent(event):
     """Insert a newline after the cursor indented appropriately.
 
-    Fancier version of deprecated ``newline_with_copy_margin`` which should
+    Fancier version of former ``newline_with_copy_margin`` which should
     compute the correct indentation of the inserted line. That is to say, indent
     by 4 extra space after a function definition, class definition, context
     manager... And dedent by 4 space after ``pass``, ``return``, ``raise ...``.


Test patch: