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

Added detection for Twisted #10

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions newsfragments/10.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Sniffio can now detect Twisted_.

.. _Twisted: https://twistedmatrix.com/trac/
7 changes: 7 additions & 0 deletions sniffio/_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ async def generic_sleep(seconds):
from curio.meta import curio_running
if curio_running():
return 'curio'

# Sniff for Twisted
if 'twisted' in sys.modules:
from twisted.internet import reactor
from twisted.python.threadable import isInIOThread
if reactor.running and isInIOThread():
return "twisted"

raise AsyncLibraryNotFoundError(
"unknown async library, or not in async context"
Expand Down
44 changes: 44 additions & 0 deletions sniffio/_tests/test_sniffio.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import sys
import warnings

import pytest

Expand Down Expand Up @@ -82,3 +83,46 @@ async def this_is_curio():

with pytest.raises(AsyncLibraryNotFoundError):
current_async_library()


@pytest.fixture(params=['select', 'asyncio'])
def reactor(request):
with warnings.catch_warnings():
warnings.simplefilter('ignore', DeprecationWarning)
from twisted.internet import asyncioreactor, selectreactor

if request.param == 'asyncio':
import asyncio
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
asyncioreactor.install(loop)
yield
loop.close()
else:
selectreactor.install()
yield

del sys.modules['twisted.internet.reactor']


def test_twisted(reactor):
from twisted.internet import reactor

with pytest.raises(AsyncLibraryNotFoundError):
current_async_library()

ran = []

def this_is_twisted():
try:
assert current_async_library() == "twisted"
ran.append(True)
finally:
reactor.stop()

reactor.callWhenRunning(this_is_twisted)
reactor.run()
assert ran == [True]

with pytest.raises(AsyncLibraryNotFoundError):
current_async_library()
1 change: 1 addition & 0 deletions test-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pytest
pytest-cov
curio
twisted