Skip to content

Commit b0b8d5e

Browse files
committed
Merge branch 'master' into also_test_pyside2
2 parents ffa11fc + ae01922 commit b0b8d5e

File tree

5 files changed

+254
-72
lines changed

5 files changed

+254
-72
lines changed

README.rst

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ pytest-twisted - test twisted code with pytest
77
|PyPI| |Pythons| |Travis| |AppVeyor| |Black|
88

99
:Authors: Ralf Schmitt, Kyle Altendorf, Victor Titor
10-
:Version: 1.12
11-
:Date: 2019-09-26
10+
:Version: 1.13.1
11+
:Date: 2020-09-09
1212
:Download: https://pypi.python.org/pypi/pytest-twisted#downloads
1313
:Code: https://github.com/pytest-dev/pytest-twisted
1414

@@ -111,11 +111,6 @@ be explicitly installed earlier by calling
111111
``pytest_twisted.init_default_reactor()`` or the corresponding function
112112
for the desired alternate reactor.
113113

114-
Beware that in situations such as
115-
a ``conftest.py`` file that the name ``pytest_twisted`` may be
116-
undesirably detected by ``pytest`` as an unknown hook. One alternative
117-
is to ``import pytest_twisted as pt``.
118-
119114

120115
inlineCallbacks
121116
===============
@@ -164,10 +159,6 @@ async/await fixtures
164159
pytest fixture semantics of setup, value, and teardown. At present only
165160
function and module scope are supported.
166161

167-
Note: You must *call* ``pytest_twisted.async_fixture()`` and
168-
``pytest_twisted.async_yield_fixture()``.
169-
This requirement may be removed in a future release.
170-
171162
.. code-block:: python
172163
173164
# No yield (coroutine function)

pytest_twisted.py

Lines changed: 60 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import functools
22
import inspect
3+
import itertools
34
import sys
45
import warnings
56

67
import decorator
78
import greenlet
89
import pytest
910

10-
from twisted.internet import defer
11-
# from twisted.internet import error
11+
from twisted.internet import defer, error
1212
from twisted.internet.threads import blockingCallFromThread
1313
from twisted.python import failure
1414

@@ -120,6 +120,56 @@ def decorator_apply(dec, func):
120120
dict(decfunc=dec(func)), __wrapped__=func)
121121

122122

123+
class DecoratorArgumentsError(Exception):
124+
pass
125+
126+
127+
def repr_args_kwargs(*args, **kwargs):
128+
arguments = ', '.join(itertools.chain(
129+
(repr(x) for x in args),
130+
('{}={}'.format(k, repr(v)) for k, v in kwargs.items())
131+
))
132+
133+
return '({})'.format(arguments)
134+
135+
136+
def positional_not_allowed_exception(*args, **kwargs):
137+
arguments = repr_args_kwargs(*args, **kwargs)
138+
139+
return DecoratorArgumentsError(
140+
'Positional decorator arguments not allowed: {}'.format(arguments),
141+
)
142+
143+
144+
def _optional_arguments():
145+
def decorator_decorator(d):
146+
# TODO: this should get the signature of d minus the f or something
147+
def decorator_wrapper(*args, **decorator_arguments):
148+
"""this is decorator_wrapper"""
149+
if len(args) > 1:
150+
raise positional_not_allowed_exception()
151+
152+
if len(args) == 1:
153+
maybe_f = args[0]
154+
155+
if len(decorator_arguments) > 0 or not callable(maybe_f):
156+
raise positional_not_allowed_exception()
157+
158+
f = maybe_f
159+
return d(f)
160+
161+
# TODO: this should get the signature of d minus the kwargs
162+
def decorator_closure_on_arguments(f):
163+
return d(f, **decorator_arguments)
164+
165+
return decorator_closure_on_arguments
166+
167+
return decorator_wrapper
168+
169+
return decorator_decorator
170+
171+
172+
@_optional_arguments()
123173
def inlineCallbacks(f):
124174
"""
125175
Mark as inline callbacks test for pytest-twisted processing and apply
@@ -136,6 +186,7 @@ def inlineCallbacks(f):
136186
return decorated
137187

138188

189+
@_optional_arguments()
139190
def ensureDeferred(f):
140191
"""
141192
Mark as async test for pytest-twisted processing.
@@ -178,7 +229,8 @@ def _set_mark(o, mark):
178229

179230
def _marked_async_fixture(mark):
180231
@functools.wraps(pytest.fixture)
181-
def fixture(*args, **kwargs):
232+
@_optional_arguments()
233+
def fixture(f, *args, **kwargs):
182234
try:
183235
scope = args[0]
184236
except IndexError:
@@ -198,13 +250,10 @@ def fixture(*args, **kwargs):
198250
# https://github.com/pytest-dev/pytest-twisted/issues/56
199251
raise AsyncFixtureUnsupportedScopeError.from_scope(scope=scope)
200252

201-
def decorator(f):
202-
_set_mark(f, mark)
203-
result = pytest.fixture(*args, **kwargs)(f)
204-
205-
return result
253+
_set_mark(f, mark)
254+
result = pytest.fixture(*args, **kwargs)(f)
206255

207-
return decorator
256+
return result
208257

209258
return fixture
210259

@@ -274,12 +323,6 @@ def tear_it_down(deferred):
274323
yield deferred
275324
except StopAsyncIteration:
276325
return
277-
except Exception: # as e:
278-
pass
279-
# e = e
280-
else:
281-
pass
282-
# e = None
283326

284327
# TODO: six.raise_from()
285328
raise AsyncGeneratorFixtureDidNotStopError.from_generator(
@@ -401,14 +444,9 @@ def init_asyncio_reactor():
401444

402445
def _install_reactor(reactor_installer, reactor_type):
403446
"""Install the specified reactor and create the greenlet."""
404-
# TODO: maybe fix this in qt5reactor? btw, it avoids creating a second
405-
# qt5reactor.core.QtEventReactor and this somehow fixes the hang
406-
# that showed up in 5.15.0.
407-
# try:
408-
if 'twisted.internet.reactor' not in sys.modules:
447+
try:
409448
reactor_installer()
410-
# except error.ReactorAlreadyInstalledError:
411-
else:
449+
except error.ReactorAlreadyInstalledError:
412450
import twisted.internet.reactor
413451

414452
if not isinstance(twisted.internet.reactor, reactor_type):

setup.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
setup(
77
name="pytest-twisted",
8-
version="1.12",
9-
description="A twisted plugin for py.test.",
8+
version="1.13.1",
9+
description="A twisted plugin for pytest.",
1010
long_description=long_description,
1111
long_description_content_type="text/x-rst",
1212
author="Ralf Schmitt, Kyle Altendorf, Victor Titor",
@@ -15,7 +15,11 @@
1515
py_modules=["pytest_twisted"],
1616
python_requires='>=2.7,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*',
1717
install_requires=["greenlet", "pytest>=2.3", "decorator"],
18-
extras_require={"dev": ["pre-commit", "black"]},
18+
extras_require={
19+
"dev": ["pre-commit", "black"],
20+
"pyside2": ["pyside2", "qt5reactor"],
21+
"pyqt5": ["pyqt5", "qt5reactor>=0.6.2"],
22+
},
1923
classifiers=[
2024
"Development Status :: 5 - Production/Stable",
2125
"Intended Audience :: Developers",

0 commit comments

Comments
 (0)