##// END OF EJS Templates
Fix use of pyside6 >= 6.7.0 (#14510)...
Fix use of pyside6 >= 6.7.0 (#14510) Fixes #14463. Using `pyside6 >= 6.7.0` as the `qt6` gui loop gives the following error: ``` In [1]: %gui qt6 In [2]: Traceback (most recent call last): File "/Users/iant/micromamba/envs/temp/bin/ipython", line 8, in <module> sys.exit(start_ipython()) ^^^^^^^^^^^^^^^ File "/Users/iant/github/ipython/IPython/__init__.py", line 130, in start_ipython return launch_new_instance(argv=argv, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/iant/micromamba/envs/temp/lib/python3.12/site-packages/traitlets/config/application.py", line 1075, in launch_instance app.start() File "/Users/iant/github/ipython/IPython/terminal/ipapp.py", line 317, in start self.shell.mainloop() File "/Users/iant/github/ipython/IPython/terminal/interactiveshell.py", line 917, in mainloop self.interact() File "/Users/iant/github/ipython/IPython/terminal/interactiveshell.py", line 902, in interact code = self.prompt_for_code() ^^^^^^^^^^^^^^^^^^^^^^ File "/Users/iant/github/ipython/IPython/terminal/interactiveshell.py", line 845, in prompt_for_code text = self.pt_app.prompt( ^^^^^^^^^^^^^^^^^^^ File "/Users/iant/micromamba/envs/temp/lib/python3.12/site-packages/prompt_toolkit/shortcuts/prompt.py", line 1035, in prompt return self.app.run( ^^^^^^^^^^^^^ File "/Users/iant/micromamba/envs/temp/lib/python3.12/site-packages/prompt_toolkit/application/application.py", line 978, in run result = loop.run_until_complete(coro) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/iant/micromamba/envs/temp/lib/python3.12/asyncio/base_events.py", line 674, in run_until_complete self.run_forever() File "/Users/iant/micromamba/envs/temp/lib/python3.12/asyncio/base_events.py", line 641, in run_forever self._run_once() File "/Users/iant/micromamba/envs/temp/lib/python3.12/asyncio/base_events.py", line 1948, in _run_once event_list = self._selector.select(timeout) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/iant/micromamba/envs/temp/lib/python3.12/site-packages/prompt_toolkit/eventloop/inputhook.py", line 150, in select self.inputhook(InputHookContext(self._r, input_is_ready)) File "/Users/iant/github/ipython/IPython/terminal/pt_inputhooks/qt.py", line 50, in inputhook _appref = app = QtGui.QApplication([" "]) ^^^^^^^^^^^^^^^^^^ AttributeError: module 'PySide6.QtPrintSupport' has no attribute 'QApplication' If you suspect this is an IPython 8.28.0.dev bug, please report it at: https://github.com/ipython/ipython/issues or send an email to the mailing list at ipython-dev@python.org You can print a more detailed traceback right now with "%tb", or use "%debug" to interactively debug it. Extra-detailed tracebacks for bug-reporting purposes can be enabled via: %config Application.verbose_crash=True ``` This is because we use the imported module's `__dict__` to get the classes and functions available in the module here: https://github.com/ipython/ipython/blob/9b8cd4a397e5894ffeadad52477bb53e0fb664fc/IPython/external/qt_loaders.py#L309-L311 This no longer works as not all the classes and functions are in the `__dict__`. The solution in this PR is to use `dir(module)` instead. I have tested this locally using `pyside6` 6.6.3.1, 6.7.0, 6.7.1 and 6.7.2 and it works for me. It also successfully creates Matplotlib plots using for example ``` In [1]: %matplotlib qt6 In [2]: import matplotlib.pyplot as plt In [3]: plt.plot([1,3,2]) ``` It would be good to get independent confirmation that this fixes other downstream libraries as I tend to work directly with IPython and IPyKernel.

File last commit:

r26504:a9c5e02a
r28842:e5d1a069 merge
Show More
editorhooks.py
127 lines | 3.9 KiB | text/x-python | PythonLexer
""" 'editor' hooks for common editors that work well with ipython
They should honor the line number argument, at least.
Contributions are *very* welcome.
"""
import os
import shlex
import subprocess
import sys
from IPython import get_ipython
from IPython.core.error import TryNext
from IPython.utils import py3compat
def install_editor(template, wait=False):
"""Installs the editor that is called by IPython for the %edit magic.
This overrides the default editor, which is generally set by your EDITOR
environment variable or is notepad (windows) or vi (linux). By supplying a
template string `run_template`, you can control how the editor is invoked
by IPython -- (e.g. the format in which it accepts command line options)
Parameters
----------
template : basestring
run_template acts as a template for how your editor is invoked by
the shell. It should contain '{filename}', which will be replaced on
invocation with the file name, and '{line}', $line by line number
(or 0) to invoke the file with.
wait : bool
If `wait` is true, wait until the user presses enter before returning,
to facilitate non-blocking editors that exit immediately after
the call.
"""
# not all editors support $line, so we'll leave out this check
# for substitution in ['$file', '$line']:
# if not substitution in run_template:
# raise ValueError(('run_template should contain %s'
# ' for string substitution. You supplied "%s"' % (substitution,
# run_template)))
def call_editor(self, filename, line=0):
if line is None:
line = 0
cmd = template.format(filename=shlex.quote(filename), line=line)
print(">", cmd)
# shlex.quote doesn't work right on Windows, but it does after splitting
if sys.platform.startswith('win'):
cmd = shlex.split(cmd)
proc = subprocess.Popen(cmd, shell=True)
if proc.wait() != 0:
raise TryNext()
if wait:
py3compat.input("Press Enter when done editing:")
get_ipython().set_hook('editor', call_editor)
get_ipython().editor = template
# in these, exe is always the path/name of the executable. Useful
# if you don't have the editor directory in your path
def komodo(exe=u'komodo'):
""" Activestate Komodo [Edit] """
install_editor(exe + u' -l {line} {filename}', wait=True)
def scite(exe=u"scite"):
""" SciTE or Sc1 """
install_editor(exe + u' {filename} -goto:{line}')
def notepadplusplus(exe=u'notepad++'):
""" Notepad++ http://notepad-plus.sourceforge.net """
install_editor(exe + u' -n{line} {filename}')
def jed(exe=u'jed'):
""" JED, the lightweight emacsish editor """
install_editor(exe + u' +{line} {filename}')
def idle(exe=u'idle'):
""" Idle, the editor bundled with python
Parameters
----------
exe : str, None
If none, should be pretty smart about finding the executable.
"""
if exe is None:
import idlelib
p = os.path.dirname(idlelib.__filename__)
# i'm not sure if this actually works. Is this idle.py script
# guaranteed to be executable?
exe = os.path.join(p, 'idle.py')
install_editor(exe + u' {filename}')
def mate(exe=u'mate'):
""" TextMate, the missing editor"""
# wait=True is not required since we're using the -w flag to mate
install_editor(exe + u' -w -l {line} {filename}')
# ##########################################
# these are untested, report any problems
# ##########################################
def emacs(exe=u'emacs'):
install_editor(exe + u' +{line} {filename}')
def gnuclient(exe=u'gnuclient'):
install_editor(exe + u' -nw +{line} {filename}')
def crimson_editor(exe=u'cedt.exe'):
install_editor(exe + u' /L:{line} {filename}')
def kate(exe=u'kate'):
install_editor(exe + u' -u -l {line} {filename}')