Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
cpython/Lib/unittest/async_case.py at main · python/cpython · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Uh oh!
There was an error while loading.
Please reload this page
.
python
/
cpython
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
35.4k
Star
77.2k
Code
Issues
5k+
Pull requests
2.6k
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Files
Expand file tree
main
Breadcrumbs
cpython
/
Lib
/
unittest
/
async_case.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
156 lines (134 loc) · 5.73 KB
main
Breadcrumbs
cpython
/
Lib
/
unittest
/
async_case.py
Copy path
Top
File metadata and controls
Code
Blame
156 lines (134 loc) · 5.73 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import
asyncio
import
contextvars
import
inspect
import
warnings
from
.
case
import
TestCase
__unittest
=
True
class
IsolatedAsyncioTestCase
(
TestCase
):
# Names intentionally have a long prefix
# to reduce a chance of clashing with user-defined attributes
# from inherited test case
#
# The class doesn't call loop.run_until_complete(self.setUp()) and family
# but uses a different approach:
# 1. create a long-running task that reads self.setUp()
# awaitable from queue along with a future
# 2. await the awaitable object passing in and set the result
# into the future object
# 3. Outer code puts the awaitable and the future object into a queue
# with waiting for the future
# The trick is necessary because every run_until_complete() call
# creates a new task with embedded ContextVar context.
# To share contextvars between setUp(), test and tearDown() we need to execute
# them inside the same task.
# Note: the test case sets the per-thread event loop unless
# loop_factory=asyncio.EventLoop is set.
# I believe this is not an issue in user level tests but python itself for testing
# should reset the event loop in every test module
# by calling asyncio.set_event_loop(None) in tearDownModule()
# or set loop_factory=asyncio.EventLoop
loop_factory
=
None
def
__init__
(
self
,
methodName
=
'runTest'
):
super
().
__init__
(
methodName
)
self
.
_asyncioRunner
=
None
self
.
_asyncioTestContext
=
contextvars
.
copy_context
()
async
def
asyncSetUp
(
self
):
pass
async
def
asyncTearDown
(
self
):
pass
def
addAsyncCleanup
(
self
,
func
,
/
,
*
args
,
**
kwargs
):
# A trivial trampoline to addCleanup()
# the function exists because it has a different semantics
# and signature:
# addCleanup() accepts regular functions
# but addAsyncCleanup() accepts coroutines
#
# We intentionally don't add inspect.iscoroutinefunction() check
# for func argument because there is no way
# to check for async function reliably:
# 1. It can be "async def func()" itself
# 2. Class can implement "async def __call__()" method
# 3. Regular "def func()" that returns awaitable object
self
.
addCleanup
(
*
(
func
,
*
args
),
**
kwargs
)
async
def
enterAsyncContext
(
self
,
cm
):
"""Enters the supplied asynchronous context manager.
If successful, also adds its __aexit__ method as a cleanup
function and returns the result of the __aenter__ method.
"""
# We look up the special methods on the type to match the with
# statement.
cls
=
type
(
cm
)
try
:
enter
=
cls
.
__aenter__
exit
=
cls
.
__aexit__
except
AttributeError
:
msg
=
(
f"'
{
cls
.
__module__
}
.
{
cls
.
__qualname__
}
' object does "
"not support the asynchronous context manager protocol"
)
try
:
cls
.
__enter__
cls
.
__exit__
except
AttributeError
:
pass
else
:
msg
+=
(
" but it supports the context manager protocol. "
"Did you mean to use enterContext()?"
)
raise
TypeError
(
msg
)
from
None
result
=
await
enter
(
cm
)
self
.
addAsyncCleanup
(
exit
,
cm
,
None
,
None
,
None
)
return
result
def
_callSetUp
(
self
):
# Force loop to be initialized and set as the current loop
# so that setUp functions can use get_event_loop() and get the
# correct loop instance.
self
.
_asyncioRunner
.
get_loop
()
self
.
_asyncioTestContext
.
run
(
self
.
setUp
)
self
.
_callAsync
(
self
.
asyncSetUp
)
def
_callTestMethod
(
self
,
method
):
result
=
self
.
_callMaybeAsync
(
method
)
if
result
is
not
None
:
msg
=
(
f'It is deprecated to return a value that is not None '
f'from a test case (
{
method
}
returned
{
type
(
result
).
__name__
!r
}
)'
,
)
warnings
.
warn
(
msg
,
DeprecationWarning
,
stacklevel
=
4
)
def
_callTearDown
(
self
):
self
.
_callAsync
(
self
.
asyncTearDown
)
self
.
_asyncioTestContext
.
run
(
self
.
tearDown
)
def
_callCleanup
(
self
,
function
,
*
args
,
**
kwargs
):
self
.
_callMaybeAsync
(
function
,
*
args
,
**
kwargs
)
def
_callAsync
(
self
,
func
,
/
,
*
args
,
**
kwargs
):
assert
self
.
_asyncioRunner
is
not
None
,
'asyncio runner is not initialized'
assert
inspect
.
iscoroutinefunction
(
func
),
f'
{
func
!r
}
is not an async function'
return
self
.
_asyncioRunner
.
run
(
func
(
*
args
,
**
kwargs
),
context
=
self
.
_asyncioTestContext
)
def
_callMaybeAsync
(
self
,
func
,
/
,
*
args
,
**
kwargs
):
assert
self
.
_asyncioRunner
is
not
None
,
'asyncio runner is not initialized'
if
inspect
.
iscoroutinefunction
(
func
):
return
self
.
_asyncioRunner
.
run
(
func
(
*
args
,
**
kwargs
),
context
=
self
.
_asyncioTestContext
,
)
else
:
return
self
.
_asyncioTestContext
.
run
(
func
,
*
args
,
**
kwargs
)
def
_setupAsyncioRunner
(
self
):
assert
self
.
_asyncioRunner
is
None
,
'asyncio runner is already initialized'
runner
=
asyncio
.
Runner
(
debug
=
True
,
loop_factory
=
self
.
loop_factory
)
self
.
_asyncioRunner
=
runner
def
_tearDownAsyncioRunner
(
self
):
runner
=
self
.
_asyncioRunner
runner
.
close
()
def
run
(
self
,
result
=
None
):
self
.
_setupAsyncioRunner
()
try
:
return
super
().
run
(
result
)
finally
:
self
.
_tearDownAsyncioRunner
()
def
debug
(
self
):
self
.
_setupAsyncioRunner
()
super
().
debug
()
self
.
_tearDownAsyncioRunner
()
def
__del__
(
self
):
if
self
.
_asyncioRunner
is
not
None
:
self
.
_tearDownAsyncioRunner
()
You can’t perform that action at this time.