Bug 2165570

Summary: python-prompt-toolkit fails to build with Python 3.12: RuntimeError: There is no current event loop in thread 'MainThread'.
Product: [Fedora] Fedora Reporter: Tomáš Hrnčiar <thrnciar>
Component: python-prompt-toolkitAssignee: Carl George 🤠 <carl>
Status: CLOSED ERRATA QA Contact: Fedora Extras Quality Assurance <extras-qa>
Severity: unspecified Docs Contact:
Priority: unspecified    
Version: rawhideCC: carl, me, mhroncok, python-packagers-sig, thrnciar
Target Milestone: ---   
Target Release: ---   
Hardware: Unspecified   
OS: Unspecified   
Whiteboard:
Fixed In Version: python-prompt-toolkit-3.0.36^1.5eb6efd-1.fc39 Doc Type: If docs needed, set a value
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2023-02-18 06:43:03 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:
Bug Depends On:    
Bug Blocks: 2135404    

Description Tomáš Hrnčiar 2023-01-30 12:41:57 UTC
python-prompt-toolkit fails to build with Python 3.12.0a4.

=================================== FAILURES ===================================
______________________ test_generator_to_async_generator _______________________

    def get_event_loop() -> asyncio.AbstractEventLoop:
        """Backward compatible way to get the event loop"""
        # Python 3.6 doesn't have get_running_loop
        # Python 3.10 deprecated get_event_loop
        if sys.version_info >= (3, 7):
            getloop = asyncio.get_running_loop
        else:
            getloop = asyncio.get_event_loop
    
        try:
>           return getloop()
E           RuntimeError: no running event loop

../../BUILDROOT/python-prompt-toolkit-3.0.29-4.fc38.x86_64/usr/lib/python3.12/site-packages/prompt_toolkit/eventloop/utils.py:116: RuntimeError

During handling of the above exception, another exception occurred:

    def test_generator_to_async_generator():
        """
        Test conversion of sync to asycn generator.
        This should run the synchronous parts in a background thread.
        """
        async_gen = generator_to_async_generator(_sync_generator)
    
        items = []
    
        async def consume_async_generator():
            async for item in async_gen:
                items.append(item)
    
        # Run the event loop until all items are collected.
>       get_event_loop().run_until_complete(consume_async_generator())

tests/test_async_generator.py:23: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
../../BUILDROOT/python-prompt-toolkit-3.0.29-4.fc38.x86_64/usr/lib/python3.12/site-packages/prompt_toolkit/eventloop/utils.py:118: in get_event_loop
    return asyncio.get_event_loop_policy().get_event_loop()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <asyncio.unix_events._UnixDefaultEventLoopPolicy object at 0x7fad931a0e30>

    def get_event_loop(self):
        """Get the event loop for the current context.
    
        Returns an instance of EventLoop or raises an exception.
        """
        if self._local._loop is None:
>           raise RuntimeError('There is no current event loop in thread %r.'
                               % threading.current_thread().name)
E           RuntimeError: There is no current event loop in thread 'MainThread'.

/usr/lib64/python3.12/asyncio/events.py:676: RuntimeError
=========================== short test summary info ============================
FAILED tests/test_async_generator.py::test_generator_to_async_generator - Run...
======================== 1 failed, 144 passed in 14.83s ========================

The get_event_loop() method of the default event loop policy now emits a
DeprecationWarning if there is no current event loop set and it decides to
create one. (Contributed by Serhiy Storchaka and Guido van Rossum in
gh-100160.)

asyncio.get_event_loop() and many other asyncio
functions like ensure_future(), shield() or gather(), and also the
get_event_loop() method of BaseDefaultEventLoopPolicy now raise a RuntimeError
if called when there is no running event loop and the current event loop was
not set. Previously they implicitly created and set a new current event loop.
DeprecationWarning is no longer emitted if there is no running event loop but
the current event loop is set in the policy. (Contributed by Serhiy Storchaka
in gh-93453.)

https://github.com/python/cpython/issues/100160
https://github.com/python/cpython/issues/93453

https://docs.python.org/3.12/whatsnew/3.12.html

For the build logs, see:
https://copr-be.cloud.fedoraproject.org/results/@python/python3.12/fedora-rawhide-x86_64/05266525-python-prompt-toolkit/

For all our attempts to build python-prompt-toolkit with Python 3.12, see:
https://copr.fedorainfracloud.org/coprs/g/python/python3.12/package/python-prompt-toolkit/

Testing and mass rebuild of packages is happening in copr. You can follow these instructions to test locally in mock if your package builds with Python 3.12:
https://copr.fedorainfracloud.org/coprs/g/python/python3.12/

Let us know here if you have any questions.

Python 3.12 is planned to be included in Fedora 39. To make that update smoother, we're building Fedora packages with all pre-releases of Python 3.12.
A build failure prevents us from testing all dependent packages (transitive [Build]Requires), so if this package is required a lot, it's important for us to get it fixed soon.
We'd appreciate help from the people who know this package best, but if you don't want to work on this now, let us know so we can try to work around it on our side.

Comment 1 Carl George 🤠 2023-01-30 21:04:06 UTC
https://github.com/prompt-toolkit/python-prompt-toolkit/pull/1714 looks promising but incomplete.

Comment 2 Ben Cotton 2023-02-07 15:07:25 UTC
This bug appears to have been reported against 'rawhide' during the Fedora Linux 38 development cycle.
Changing version to 38.

Comment 3 Carl George 🤠 2023-02-18 06:36:34 UTC
https://github.com/prompt-toolkit/python-prompt-toolkit/pull/1714 and https://github.com/prompt-toolkit/python-prompt-toolkit/pull/1721 together seem to have fixed this.  The test suite now passes on the master branch when run with Python 3.12.  I'm building a snapshot of that to resolve this bug.

Comment 4 Fedora Update System 2023-02-18 06:41:12 UTC
FEDORA-2023-85b1805516 has been submitted as an update to Fedora 39. https://bodhi.fedoraproject.org/updates/FEDORA-2023-85b1805516

Comment 5 Fedora Update System 2023-02-18 06:43:03 UTC
FEDORA-2023-85b1805516 has been pushed to the Fedora 39 stable repository.
If problem still persists, please make note of it in this bug report.

Comment 6 Carl George 🤠 2023-02-18 07:16:31 UTC
Excellent, it appears to have worked.

https://copr.fedorainfracloud.org/coprs/g/python/python3.12/build/5539664/