community icon indicating copy to clipboard operation
community copied to clipboard

Resizing in Debug (PyCharm) crashes with "NameError: name 'EventLoop' is not defined" in kivy\core\window\window_sdl2.py

Open xmav000 opened this issue 3 years ago • 3 comments

Software Versions

  • Python: 3.10.4
  • OS: Windows 11
  • Kivy: 2.1.0
  • Kivy installation method: Pycharm (pip?)
  • PyCharm 2021.3.2 (Community Edition)

Describe the bug Even the most basic Kivy App crashes when started as "debug" in PyCharm. When "run" in PyCharm, resizing works, but it's not possible to debug anything when you resize. (most likely at least in Windows because its the windows_sdl2.py)

Traceback (most recent call last): File "[...]\lib\site-packages\kivy\core\window\window_sdl2.py", line 270, in _event_filter EventLoop.idle() NameError: name 'EventLoop' is not defined"

Expected behavior No crash, debugging should just work after resize.

To Reproduce Create any Kivy App in PyCharm, start the program as debug (with or without break point) and resize the window. -> crash

Code and Logs and screenshots

from kivy.app import App


class KivoApp(App):

    def build(self):
        pass


if __name__ == '__main__':
    KivoApp().run()

Additional context

xmav000 avatar May 11 '22 21:05 xmav000

I see the same behavior under Ubuntu 22.04 with PyCharm 2022.1 (Community Edition)

DaytonaJohn avatar May 24 '22 23:05 DaytonaJohn

Kivy 2.1.0 also crashes for me on Ubuntu 22.04 und OS X 12 Montery when either trying to close or resize a window because "EventLoop is None" in more or less every call of _event_filter. I am also using PyCharm.

When running it in Release Mode the Window is resizable but closing the application never finishes.

Alyxion avatar Jul 09 '22 11:07 Alyxion

Same on Ubuntu 22.04 PyCharm 2022.2.1 (Professional Edition)

jleaders avatar Sep 14 '22 16:09 jleaders

I am having the same problem on macOS Monterey 12.6 (chip M1) Python 3.10.6 Kivy 2.1.0

FilipeMarch avatar Sep 30 '22 08:09 FilipeMarch

Have the same problem on Manjaro with PyCharm 2022.2.2 (Community Edition). Python 3.10.7 Kivy 2.1.0

Error:

 Traceback (most recent call last):
   File "/home/doopath/Files/code/Kivy/kivy_venv/lib/python3.10/site-packages/kivy/core/window/window_sdl2.py", line 270, in _event_filter
     EventLoop.idle()
 NameError: name 'EventLoop' is not defined
python-BaseException

doopath avatar Oct 07 '22 21:10 doopath

Same issue here.

PyCharm 2022.2.2 (Professional Edition). Python 3.10.7 Kivy 2.1.0

 Traceback (most recent call last):
   File "[xxxxxxxx]\venv\lib\site-packages\kivy\core\window\window_sdl2.py", line 270, in _event_filter
     EventLoop.idle()
 NameError: name 'EventLoop' is not defined

dwoods avatar Oct 07 '22 21:10 dwoods

It seems i fix this (temporary and it's not relieable). Debugger shows me 2 problems:

  1. window_sdl2.py file, WindowSDL._event_filter method. I rewrite the last condition like this:
elif action == 'windowresized':
    self._size = largs
    self._win.resize_window(*self._size)
    # Force kivy to render the frame now, so that the canvas is drawn.
    try: EventLoop.idle()
    except NameError: pass
  1. window_sdl2.py file, WindowSDL.mainloop method. I rewrite the condition placed at ~570 line like this:
if action == 'quit':
    if self.dispatch('on_request_close'):
        continue
    try: EventLoop.quit = True
    except NameError: exit()
    break

Dunno if these changes will brake the freck out the kivy module, but now it seems to work nicely. Hope this issue will be resolved soon.

doopath avatar Oct 07 '22 22:10 doopath

I looked up a bit. Somehow when a window is resized, mainloop in window_sdl2.py looses its scope on EventLoop. @doopath solution did not work for me, the window kept open.

Importing EventLoop again in the if condition will help, but this is not a nice fix:

if action == 'quit':
    from kivy.base import EventLoop
    if self.dispatch('on_request_close'):
        continue
    EventLoop.quit = True
    break

Edit: The method _event_filter in window_sdl2.py is called when resizing the window. After process EventLoop.idle() (last elif condition in method) it looses its scope on it. I do not now why yet.

MRuszczycki avatar Nov 03 '22 01:11 MRuszczycki

I noticed that if you place a breakpoint just before the place this error occurs, EventLoop is defined and if you step over the code it behaves as expected.

I would imagine it's a PyCharm quirk and I've seen the debugger doing some weird things before.

EDIT: scratch that, EventLoop is defined after one iteration, but the resizing is triggering another iteration and in that iteration it's not defined.

AvihooI avatar Nov 08 '22 13:11 AvihooI

Looking at window_sdl2.py again, I noticed EventLoop is imported within the create_window function in the WindowSDL class:

# auto add input provider
Logger.info('Window: auto add sdl2 input provider')
from kivy.base import EventLoop
SDL2MotionEventProvider.win = self
EventLoop.add_input_provider(SDL2MotionEventProvider('sdl', ''))

I don't really understand why EventLoop is imported again in that module, seeing as it's already imported at the top of the file.

When I remove that import it fixes the problem.

I have no idea if that has any other effects and if that import has any reason to be there.

AvihooI avatar Nov 09 '22 08:11 AvihooI

^ I can confirm the fix mentioned above ^ and I suggest this simple fix: ~~from kivy.base import EventLoop~~

Can we get a PR?

jleaders avatar Dec 06 '22 20:12 jleaders

Yes, removing that line solves the problem for me too

FilipeMarch avatar Dec 06 '22 20:12 FilipeMarch

removing that line fixed the problem for me too (OP)

xmav000 avatar Mar 22 '23 23:03 xmav000