Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Testing #2

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

Closed
agronholm wants to merge 2 commits into python-trio:master from agronholm:testing
Closed
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: 0 additions & 3 deletions .coveragerc
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,4 @@ omit=
setup.py

[report]
exclude_lines =
pragma: no cover
^def test_
precision = 1
16 changes: 3 additions & 13 deletions .travis.yml
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,8 @@ python:
- 3.6-dev
sudo: false

before_install:
- pip install -Ur test-requirements.txt
- pip install codecov
install: pip install tox-travis codecov

install:
- pip install .
script: tox

script:
- mkdir empty
- cd empty
- py.test --pyargs async_generator --cov=async_generator --cov-config=.coveragerc


after_success:
- codecov
after_success: codecov
2 changes: 1 addition & 1 deletion MANIFEST.in
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
include LICENSE.txt README.rst CODE_OF_CONDUCT.md
include test-requirements.txt .coveragerc
include .coveragerc
21 changes: 17 additions & 4 deletions async_generator/impl.py
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import collections.abc
import inspect
import sys
import warnings
from functools import wraps
from types import coroutine
import inspect
from inspect import (
getcoroutinestate, CORO_CREATED, CORO_CLOSED, CORO_SUSPENDED)
import collections.abc


class YieldWrapper:
def __init__(self, payload):
self.payload = payload


if sys.version_info < (3, 6):
def _wrap(value):
return YieldWrapper(value)
Expand All @@ -24,15 +25,17 @@ def _unwrap(box):
# Use the same box type that the interpreter uses internally. This allows
# yield_ and (more importantly!) yield_from_ to work in built-in
# generators.
import ctypes # mua ha ha.
import ctypes # mua ha ha.
dll = ctypes.CDLL(None)

dll._PyAsyncGenValueWrapperNew.restype = ctypes.py_object
dll._PyAsyncGenValueWrapperNew.argtypes = (ctypes.py_object,)

def _wrap(value):
return dll._PyAsyncGenValueWrapperNew(value)

_PyAsyncGenWrappedValue = type(_wrap(1))

def _is_wrapped(box):
return isinstance(box, _PyAsyncGenWrappedValue)

Expand All @@ -41,25 +44,29 @@ class _ctypes_PyAsyncGenWrappedValue(ctypes.Structure):
('PyObject_HEAD', ctypes.c_byte * object().__sizeof__()),
('agw_val', ctypes.py_object),
]

def _unwrap(box):
assert _is_wrapped(box)
raw = ctypes.cast(ctypes.c_void_p(id(box)),
ctypes.POINTER(_ctypes_PyAsyncGenWrappedValue))
return raw.contents.agw_val


# The magic @coroutine decorator is how you write the bottom level of
# coroutine stacks -- 'async def' can only use 'await' = yield from; but
# eventually we must bottom out in a @coroutine that calls plain 'yield'.
@coroutine
def _yield_(value):
return (yield _wrap(value))


# But we wrap the bare @coroutine version in an async def, because async def
# has the magic feature that users can get warnings messages if they forget to
# use 'await'.
async def yield_(value):
return await _yield_(value)


async def yield_from_(delegate):
# Transcribed with adaptations from:
#
Expand Down Expand Up @@ -119,6 +126,7 @@ def unpack_StopAsyncIteration(e):
break
return _r


# This is the awaitable / iterator that implements asynciter.__anext__() and
# friends.
#
Expand Down Expand Up @@ -168,6 +176,7 @@ def _invoke(self, fn, *args):
else:
return result


class AsyncGenerator:
def __init__(self, coroutine):
self._coroutine = coroutine
Expand Down Expand Up @@ -240,22 +249,26 @@ def __del__(self):
raise RuntimeError(
"partially-exhausted async_generator garbage collected")


if hasattr(collections.abc, "AsyncGenerator"):
collections.abc.AsyncGenerator.register(AsyncGenerator)


def async_generator(coroutine_maker):
@wraps(coroutine_maker)
def async_generator_maker(*args, **kwargs):
return AsyncGenerator(coroutine_maker(*args, **kwargs))
async_generator_maker._is_async_gen_function = True
return async_generator_maker


def isasyncgen(obj):
if hasattr(inspect, "isasyncgen"):
if inspect.isasyncgen(obj):
return True
return isinstance(obj, AsyncGenerator)


def isasyncgenfunction(obj):
if hasattr(inspect, "isasyncgenfunction"):
if inspect.isasyncgenfunction(obj):
Expand Down
3 changes: 3 additions & 0 deletions setup.cfg
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[tool:pytest]
addopts = -rsx --cov --tb=short
testpaths = tests
10 changes: 9 additions & 1 deletion setup.py
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,12 @@
"License :: OSI Approved :: MIT License",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.5",
])
],
extras_require={
'testing': [
'pytest',
'pytest-cov',
'pytest-asyncio'
]
}
)
3 changes: 0 additions & 3 deletions test-requirements.txt
View file Open in desktop

This file was deleted.

View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pytest
import collections.abc

from . import (
from async_generator import (
async_generator, yield_, yield_from_, isasyncgen, isasyncgenfunction,
)

Expand Down
2 changes: 1 addition & 1 deletion async_generator/test_util.py → tests/test_util.py
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest

from . import aclosing, async_generator, yield_
from async_generator import aclosing, async_generator, yield_

@async_generator
async def async_range(count, closed_slot):
Expand Down
15 changes: 15 additions & 0 deletions tox.ini
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[tox]
envlist = py35, py36, flake8

[tox:travis]
3.5 = py35, flake8
3.6-dev = py36

[testenv]
extras = testing
commands = python -m pytest {posargs}

[testenv:flake8]
deps = flake8
commands = flake8 async_generator
skip_install = true

AltStyle によって変換されたページ (->オリジナル) /