Skip to content

native/textern.py: fix for Python 3.14#97

Open
jtluka wants to merge 1 commit intojlebon:masterfrom
jtluka:fix-for-py3.14
Open

native/textern.py: fix for Python 3.14#97
jtluka wants to merge 1 commit intojlebon:masterfrom
jtluka:fix-for-py3.14

Conversation

@jtluka
Copy link

@jtluka jtluka commented Jan 8, 2026

Hit following traceback on system with Python 3.14:

stderr output from native app textern: Traceback (most recent call last):
stderr output from native app textern:   File "/home/igyn/.local/libexec/textern/textern.py", line 246, in <module>
stderr output from native app textern:     sys.exit(main())
stderr output from native app textern:              ~~~~^^
stderr output from native app textern:   File "/home/igyn/.local/libexec/textern/textern.py", line 103, in main
stderr output from native app textern:     loop = asyncio.get_event_loop()
stderr output from native app textern:   File "/usr/lib64/python3.14/asyncio/events.py", line 715, in get_event_loop
stderr output from native app textern:     raise RuntimeError('There is no current event loop in thread %r.'
stderr output from native app textern:                        % threading.current_thread().name)
stderr output from native app textern: RuntimeError: There is no current event loop in thread 'MainThread'.

This error is caused by a significant change in Python 3.14. In this version, asyncio.get_event_loop() no longer creates a new event loop automatically if one doesn't exist; instead, it raises the RuntimeError.

The code was updated to catch the exception and create the event loop if necessary.

Assisted-by: Gemini3 (Fast)

Copy link
Owner

@jlebon jlebon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution!

loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)

loop = asyncio.get_event_loop()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is duplicate code now.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah. my bad, will fix and repush.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reworked the patch to check the python version instead of catching exception.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAICT, the new_event_loop/set_event_loop APIs have been around for a long time now. So I think instead of doing a version comparison, we could just unconditionally always create an event loop and set it. We should also then be able to get rid of the get_event_loop call below.

Hit following traceback on system with Python 3.14:
```
stderr output from native app textern: Traceback (most recent call last):
stderr output from native app textern:   File "/home/igyn/.local/libexec/textern/textern.py", line 246, in <module>
stderr output from native app textern:     sys.exit(main())
stderr output from native app textern:              ~~~~^^
stderr output from native app textern:   File "/home/igyn/.local/libexec/textern/textern.py", line 103, in main
stderr output from native app textern:     loop = asyncio.get_event_loop()
stderr output from native app textern:   File "/usr/lib64/python3.14/asyncio/events.py", line 715, in get_event_loop
stderr output from native app textern:     raise RuntimeError('There is no current event loop in thread %r.'
stderr output from native app textern:                        % threading.current_thread().name)
stderr output from native app textern: RuntimeError: There is no current event loop in thread 'MainThread'.
```

This error is caused by a significant change in Python 3.14. In this version,
asyncio.get_event_loop() no longer creates a new event loop automatically if
one doesn't exist; instead, it raises the RuntimeError.

The code was updated to create an event loop if running on Python 3.14 or higher.

Assisted-by: Gemini3 (Fast)

Signed-off-by: Jan Tluka <[email protected]>
@MaxGyver83
Copy link
Contributor

I have tested this. It fixes textern for me. Thank you!

Copy link
Owner

@jlebon jlebon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the long delays here. Your reply slipped by.

In truth, I haven't been using Textern myself in a while because I switched to flatpak for my Firefox and it's a pain right now to set up native apps for it in a safe way. I'm working in the background on tweaking Textern to make it more palatable.

loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)

loop = asyncio.get_event_loop()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAICT, the new_event_loop/set_event_loop APIs have been around for a long time now. So I think instead of doing a version comparison, we could just unconditionally always create an event loop and set it. We should also then be able to get rid of the get_event_loop call below.

Rogach added a commit to Rogach/textern that referenced this pull request Feb 5, 2026
In newer Python versions asyncio does not create the loop automatically,
we have to update it manually.
jlebon#97
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants