Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Honor PyOS_InputHook in the new REPL #119842

Closed
pablogsal opened this issue May 31, 2024 · 4 comments
Closed

Honor PyOS_InputHook in the new REPL #119842

pablogsal opened this issue May 31, 2024 · 4 comments
Labels
topic-repl Related to the interactive shell type-bug An unexpected behavior, bug, or error

Comments

@pablogsal
Copy link
Member

pablogsal commented May 31, 2024

Bug report

We are currently not calling PyOS_InputHook if is set before blocking for input in the new REPL

Linked PRs

@pablogsal pablogsal added type-bug An unexpected behavior, bug, or error topic-repl Related to the interactive shell labels May 31, 2024
pablogsal added a commit to pablogsal/cpython that referenced this issue May 31, 2024
pablogsal added a commit to pablogsal/cpython that referenced this issue May 31, 2024
@mdboom
Copy link
Contributor

mdboom commented May 31, 2024

Here's a good reproducer to ensure your changes are working (on Linux):

(make install is required to get pkgconfig working for all the dependencies).

$ pip install git+https://github.com/python-pillow/Pillow@d879f39
$ pip install matplotlib

$ python
>>> import matplotlib
>>> matplotlib.use("tkagg")
>>> from matplotlib import pyplot as plt
>>> plt.plot([1,2], [3,4])
[<matplotlib.lines.Line2D object at 0x7fd3f2be8050>]
>>> plt.show(block=False)
>>>

If working, a window should appear with a plot in it, and it should respond to mouse events etc. It seems that on CPython main, this is broken, but works with PYTHON_BASIC_REPL=1 set.

Cc: @tacaswell, do I have this right? IIUC this works due to Tkinter's built-in use of PyOS_InputHook, matplotlib itself doesn't touch it.

EDIT: See a simpler reproducer below

@mdboom
Copy link
Contributor

mdboom commented May 31, 2024

With #119843 (currently), this fails with:

>>> Fatal Python error: _PyThreadState_Attach: non-NULL old thread state
                                                                        Python runtime state: initialized

                                                                                                         Current thread 0x00007fad7b63c740 (most recent call first):
                                        File "/home/mdboom/py/lib/python3.14/_pyrepl/reader.py", line 713 in call_PyOS_InputHook
    File "/home/mdboom/py/lib/python3.14/_pyrepl/reader.py", line 648 in handle1
                                                                                  File "/home/mdboom/py/lib/python3.14/_pyrepl/reader.py", line 692 in readline
                                   File "/home/mdboom/py/lib/python3.14/_pyrepl/readline.py", line 376 in multiline_input
                                                                                                                           File "/home/mdboom/py/lib/python3.14/_pyrepl/simple_interact.py", line 176 in run_multiline_interactive_console
                                                                                                              File "/home/mdboom/py/lib/python3.14/_pyrepl/__main__.py", line 48 in interactive_console
                                                                           File "/home/mdboom/py/lib/python3.14/_pyrepl/__main__.py", line 51 in <module>
                             File "<frozen runpy>", line 88 in _run_code
                                                                          File "<frozen runpy>", line 198 in _run_module_as_main

  Extension modules: numpy.core._multiarray_umath, numpy.core._multiarray_tests, numpy.linalg._umath_linalg, numpy.fft._pocketfft_internal, numpy.random._common, numpy.random.bit_generator, numpy.random._bounded_integers, numpy.random._mt19937, numpy.random.mtrand, numpy.random._philox, numpy.random._pcg64, numpy.random._sfc64, numpy.random._generator, PIL._imaging, kiwisolver._cext, PIL._imagingmath, PIL._imagingtk (total: 17)
                                                     [1]    3965 IOT instruction  python

@mdboom
Copy link
Contributor

mdboom commented May 31, 2024

Here's a much easier reproducer with no external dependencies:

from tkinter import *
root = Tk()
w = Label(root, text='Foo')
w.pack()

Current behavior I'm seeing:

CPython main: No window
CPython main / PYTHON_BASIC_REPL=1: Window is displayed, updated automatically with each line of entry (matches 3.12)
#119843: Fatal Python error: _PyThreadState_Attach: non-NULL old thread state
#119843 / PYTHON_BASIC_REPL=1: Window is displayed, updated automatically with each line of entry (matches 3.12)

@tacaswell
Copy link
Contributor

@mdboom Correct. PyQt, wxpython, and the gtk bindings all provide their own verision of PyOS_InputHook as well and mpl provides one for macOSX.

To be 100% sure the event loop is running as expected also add a "print on click" callback:

from tkinter import *
root = Tk()
b = Button(root, text='bar', command=lambda: print('bob'))
w = Label(root, text='Foo')
b.pack()
w.pack()

pablogsal added a commit to pablogsal/cpython that referenced this issue Jun 1, 2024
pablogsal added a commit to pablogsal/cpython that referenced this issue Jun 1, 2024
pablogsal added a commit to pablogsal/cpython that referenced this issue Jun 1, 2024
pablogsal added a commit to pablogsal/cpython that referenced this issue Jun 1, 2024
ambv added a commit to pablogsal/cpython that referenced this issue Jun 4, 2024
ambv added a commit to pablogsal/cpython that referenced this issue Jun 4, 2024
ambv added a commit that referenced this issue Jun 4, 2024
Signed-off-by: Pablo Galindo <[email protected]>
Co-authored-by: Łukasz Langa <[email protected]>
Co-authored-by: Michael Droettboom <[email protected]>
ambv added a commit to ambv/cpython that referenced this issue Jun 4, 2024
…H-119843)

(cherry picked from commit d909519)

Co-authored-by: Pablo Galindo Salgado <[email protected]>
Signed-off-by: Pablo Galindo <[email protected]>
Co-authored-by: Łukasz Langa <[email protected]>
Co-authored-by: Michael Droettboom <[email protected]>
ambv added a commit that referenced this issue Jun 4, 2024
…H-120066)

(cherry picked from commit d909519)

Signed-off-by: Pablo Galindo <[email protected]>
Co-authored-by: Pablo Galindo Salgado <[email protected]>
Co-authored-by: Michael Droettboom <[email protected]>
@ambv ambv closed this as completed Jun 4, 2024
barneygale pushed a commit to barneygale/cpython that referenced this issue Jun 5, 2024
Signed-off-by: Pablo Galindo <[email protected]>
Co-authored-by: Łukasz Langa <[email protected]>
Co-authored-by: Michael Droettboom <[email protected]>
noahbkim pushed a commit to hudson-trading/cpython that referenced this issue Jul 11, 2024
Signed-off-by: Pablo Galindo <[email protected]>
Co-authored-by: Łukasz Langa <[email protected]>
Co-authored-by: Michael Droettboom <[email protected]>
estyxx pushed a commit to estyxx/cpython that referenced this issue Jul 17, 2024
Signed-off-by: Pablo Galindo <[email protected]>
Co-authored-by: Łukasz Langa <[email protected]>
Co-authored-by: Michael Droettboom <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-repl Related to the interactive shell type-bug An unexpected behavior, bug, or error
Projects
Development

No branches or pull requests

4 participants