-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
490 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ python: | |
- 3.4 | ||
- 3.5 | ||
- 3.6 | ||
- 3.7-dev | ||
- pypy3.5 | ||
|
||
install: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
""" | ||
This module contains all the code that uses `async`/`await` | ||
and gets imported by other modules only if the running Python version | ||
is greater or equal to 3.5. | ||
""" | ||
|
||
|
||
def func_wrapper(func, cache, key): | ||
""" | ||
Creates an async wrapper for `cachetools.cached`. | ||
""" | ||
async def wrapper(*args, **kwargs): | ||
k = key(*args, **kwargs) | ||
try: | ||
return cache[k] | ||
except KeyError: | ||
pass # key not found | ||
v = await func(*args, **kwargs) | ||
try: | ||
cache[k] = v | ||
except ValueError: | ||
pass # value too large | ||
return v | ||
|
||
return wrapper | ||
|
||
|
||
def func_wrapper_lock(func, cache, key, lock): | ||
""" | ||
Creates an async wrapper with locking for `cachetools.cached`. | ||
""" | ||
async def wrapper(*args, **kwargs): | ||
k = key(*args, **kwargs) | ||
try: | ||
with lock: | ||
return cache[k] | ||
except KeyError: | ||
pass # key not found | ||
v = await func(*args, **kwargs) | ||
try: | ||
with lock: | ||
cache[k] = v | ||
except ValueError: | ||
pass # value too large | ||
return v | ||
|
||
return wrapper | ||
|
||
|
||
def method_wrapper(method, cache, key): | ||
""" | ||
Creates an async wrapper for `cachetools.cachedmethod`. | ||
""" | ||
async def wrapper(self, *args, **kwargs): | ||
c = cache(self) | ||
if c is None: | ||
v = await method(self, *args, **kwargs) | ||
return v | ||
k = key(self, *args, **kwargs) | ||
try: | ||
return c[k] | ||
except KeyError: | ||
pass # key not found | ||
v = await method(self, *args, **kwargs) | ||
try: | ||
c[k] = v | ||
except ValueError: | ||
pass # value too large | ||
return v | ||
|
||
return wrapper | ||
|
||
|
||
def method_wrapper_lock(method, cache, key, lock): | ||
""" | ||
Creates an async wrapper with locking for `cachetools.cachedmethod`. | ||
""" | ||
async def wrapper(self, *args, **kwargs): | ||
c = cache(self) | ||
if c is None: | ||
v = await method(self, *args, **kwargs) | ||
return v | ||
k = key(self, *args, **kwargs) | ||
try: | ||
with lock(self): | ||
return c[k] | ||
except KeyError: | ||
pass # key not found | ||
v = await method(self, *args, **kwargs) | ||
try: | ||
with lock(self): | ||
c[k] = v | ||
except ValueError: | ||
pass # value too large | ||
return v | ||
|
||
return wrapper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import os.path | ||
import sys | ||
|
||
|
||
def pytest_ignore_collect(path): | ||
if sys.version_info >= (3, 5): | ||
# do not skip any file when running tests with 3.5+ | ||
return False | ||
|
||
else: | ||
# skip async tests when running tests with <3.5 | ||
return 'async' in os.path.basename(str(path)) |
Oops, something went wrong.