Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
176baa32
Kaydet (Commit)
176baa32
authored
Ara 14, 2017
tarafından
Jelle Zijlstra
Kaydeden (comit)
Yury Selivanov
Ara 14, 2017
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
bpo-30241: implement contextlib.AbstractAsyncContextManager (#1412)
üst
bfbf04ef
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
88 additions
and
5 deletions
+88
-5
contextlib.rst
Doc/library/contextlib.rst
+11
-0
3.7.rst
Doc/whatsnew/3.7.rst
+3
-2
contextlib.py
Lib/contextlib.py
+25
-2
test_contextlib_async.py
Lib/test/test_contextlib_async.py
+48
-1
2017-10-10-18-56-46.bpo-30241.F_go20.rst
...S.d/next/Library/2017-10-10-18-56-46.bpo-30241.F_go20.rst
+1
-0
No files found.
Doc/library/contextlib.rst
Dosyayı görüntüle @
176baa32
...
@@ -29,6 +29,17 @@ Functions and classes provided:
...
@@ -29,6 +29,17 @@ Functions and classes provided:
.. versionadded:: 3.6
.. versionadded:: 3.6
.. class:: AbstractAsyncContextManager
An :term:`abstract base class` for classes that implement
:meth:`object.__aenter__` and :meth:`object.__aexit__`. A default
implementation for :meth:`object.__aenter__` is provided which returns
``self`` while :meth:`object.__aexit__` is an abstract method which by default
returns ``None``. See also the definition of
:ref:`async-context-managers`.
.. versionadded:: 3.7
.. decorator:: contextmanager
.. decorator:: contextmanager
...
...
Doc/whatsnew/3.7.rst
Dosyayı görüntüle @
176baa32
...
@@ -306,8 +306,9 @@ is a list of strings, not bytes.
...
@@ -306,8 +306,9 @@ is a list of strings, not bytes.
contextlib
contextlib
----------
----------
:func:`contextlib.asynccontextmanager` has been added. (Contributed by
:func:`~contextlib.asynccontextmanager` and
Jelle Zijlstra in :issue:`29679`.)
:class:`~contextlib.AbstractAsyncContextManager` have been added. (Contributed
by Jelle Zijlstra in :issue:`29679` and :issue:`30241`.)
cProfile
cProfile
--------
--------
...
...
Lib/contextlib.py
Dosyayı görüntüle @
176baa32
...
@@ -6,7 +6,8 @@ from collections import deque
...
@@ -6,7 +6,8 @@ from collections import deque
from
functools
import
wraps
from
functools
import
wraps
__all__
=
[
"asynccontextmanager"
,
"contextmanager"
,
"closing"
,
"nullcontext"
,
__all__
=
[
"asynccontextmanager"
,
"contextmanager"
,
"closing"
,
"nullcontext"
,
"AbstractContextManager"
,
"ContextDecorator"
,
"ExitStack"
,
"AbstractContextManager"
,
"AbstractAsyncContextManager"
,
"ContextDecorator"
,
"ExitStack"
,
"redirect_stdout"
,
"redirect_stderr"
,
"suppress"
]
"redirect_stdout"
,
"redirect_stderr"
,
"suppress"
]
...
@@ -30,6 +31,27 @@ class AbstractContextManager(abc.ABC):
...
@@ -30,6 +31,27 @@ class AbstractContextManager(abc.ABC):
return
NotImplemented
return
NotImplemented
class
AbstractAsyncContextManager
(
abc
.
ABC
):
"""An abstract base class for asynchronous context managers."""
async
def
__aenter__
(
self
):
"""Return `self` upon entering the runtime context."""
return
self
@abc.abstractmethod
async
def
__aexit__
(
self
,
exc_type
,
exc_value
,
traceback
):
"""Raise any exception triggered within the runtime context."""
return
None
@classmethod
def
__subclasshook__
(
cls
,
C
):
if
cls
is
AbstractAsyncContextManager
:
return
_collections_abc
.
_check_methods
(
C
,
"__aenter__"
,
"__aexit__"
)
return
NotImplemented
class
ContextDecorator
(
object
):
class
ContextDecorator
(
object
):
"A base class or mixin that enables context managers to work as decorators."
"A base class or mixin that enables context managers to work as decorators."
...
@@ -136,7 +158,8 @@ class _GeneratorContextManager(_GeneratorContextManagerBase,
...
@@ -136,7 +158,8 @@ class _GeneratorContextManager(_GeneratorContextManagerBase,
raise
RuntimeError
(
"generator didn't stop after throw()"
)
raise
RuntimeError
(
"generator didn't stop after throw()"
)
class
_AsyncGeneratorContextManager
(
_GeneratorContextManagerBase
):
class
_AsyncGeneratorContextManager
(
_GeneratorContextManagerBase
,
AbstractAsyncContextManager
):
"""Helper for @asynccontextmanager."""
"""Helper for @asynccontextmanager."""
async
def
__aenter__
(
self
):
async
def
__aenter__
(
self
):
...
...
Lib/test/test_contextlib_async.py
Dosyayı görüntüle @
176baa32
import
asyncio
import
asyncio
from
contextlib
import
asynccontextmanager
from
contextlib
import
asynccontextmanager
,
AbstractAsyncContextManager
import
functools
import
functools
from
test
import
support
from
test
import
support
import
unittest
import
unittest
...
@@ -20,6 +20,53 @@ def _async_test(func):
...
@@ -20,6 +20,53 @@ def _async_test(func):
return
wrapper
return
wrapper
class
TestAbstractAsyncContextManager
(
unittest
.
TestCase
):
@_async_test
async
def
test_enter
(
self
):
class
DefaultEnter
(
AbstractAsyncContextManager
):
async
def
__aexit__
(
self
,
*
args
):
await
super
()
.
__aexit__
(
*
args
)
manager
=
DefaultEnter
()
self
.
assertIs
(
await
manager
.
__aenter__
(),
manager
)
async
with
manager
as
context
:
self
.
assertIs
(
manager
,
context
)
def
test_exit_is_abstract
(
self
):
class
MissingAexit
(
AbstractAsyncContextManager
):
pass
with
self
.
assertRaises
(
TypeError
):
MissingAexit
()
def
test_structural_subclassing
(
self
):
class
ManagerFromScratch
:
async
def
__aenter__
(
self
):
return
self
async
def
__aexit__
(
self
,
exc_type
,
exc_value
,
traceback
):
return
None
self
.
assertTrue
(
issubclass
(
ManagerFromScratch
,
AbstractAsyncContextManager
))
class
DefaultEnter
(
AbstractAsyncContextManager
):
async
def
__aexit__
(
self
,
*
args
):
await
super
()
.
__aexit__
(
*
args
)
self
.
assertTrue
(
issubclass
(
DefaultEnter
,
AbstractAsyncContextManager
))
class
NoneAenter
(
ManagerFromScratch
):
__aenter__
=
None
self
.
assertFalse
(
issubclass
(
NoneAenter
,
AbstractAsyncContextManager
))
class
NoneAexit
(
ManagerFromScratch
):
__aexit__
=
None
self
.
assertFalse
(
issubclass
(
NoneAexit
,
AbstractAsyncContextManager
))
class
AsyncContextManagerTestCase
(
unittest
.
TestCase
):
class
AsyncContextManagerTestCase
(
unittest
.
TestCase
):
@_async_test
@_async_test
...
...
Misc/NEWS.d/next/Library/2017-10-10-18-56-46.bpo-30241.F_go20.rst
0 → 100644
Dosyayı görüntüle @
176baa32
Add contextlib.AbstractAsyncContextManager. Patch by Jelle Zijlstra.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment