-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Raise exception in tests when blocking code is called in event loop
- Loading branch information
Showing
5 changed files
with
146 additions
and
20 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
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
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,114 @@ | ||
import asyncio | ||
import inspect | ||
import io | ||
import os | ||
import socket | ||
import ssl | ||
import sys | ||
from importlib.abc import FileLoader | ||
|
||
import forbiddenfruit | ||
|
||
|
||
class BlockingError(Exception): ... | ||
|
||
|
||
def _raise_for_function(func): | ||
if inspect.isbuiltin(func): | ||
msg = f"Blocking call to {func.__qualname__} ({func.__self__})" | ||
elif inspect.ismethoddescriptor(func): | ||
msg = f"Blocking call to {func}" | ||
else: | ||
msg = f"Blocking call to {func.__module__}.{func.__qualname__}" | ||
raise BlockingError(msg) | ||
|
||
|
||
def _raise_if_blocking(func): | ||
def wrapper(*args, **kwargs): | ||
try: | ||
asyncio.get_running_loop() | ||
except RuntimeError: | ||
return func(*args, **kwargs) | ||
_raise_for_function(func) | ||
return None | ||
|
||
return wrapper | ||
|
||
|
||
def _wrap_os_blocking(func): | ||
def os_op(fd, *args, **kwargs): | ||
try: | ||
asyncio.get_running_loop() | ||
except RuntimeError: | ||
return func(fd, *args, **kwargs) | ||
if os.get_blocking(fd): | ||
_raise_for_function(func) | ||
return func(fd, *args, **kwargs) | ||
|
||
return os_op | ||
|
||
|
||
def _wrap_socket_blocking(func): | ||
def socket_op(self, *args, **kwargs): | ||
try: | ||
asyncio.get_running_loop() | ||
except RuntimeError: | ||
return func(self, *args, **kwargs) | ||
if self.getblocking(): | ||
msg = f"Blocking call to {func.__module__}.{func.__qualname__}" | ||
raise BlockingError(msg) | ||
return func(self, *args, **kwargs) | ||
|
||
return socket_op | ||
|
||
|
||
def _wrap_file_blocking(func): | ||
def file_op(self, *args, **kwargs): | ||
try: | ||
asyncio.get_running_loop() | ||
except RuntimeError: | ||
return func(self, *args, **kwargs) | ||
# Ignore blocking reads in import file loader | ||
inspect.stack() | ||
for frame_info in inspect.stack(): | ||
if isinstance(frame_info.frame.f_locals.get("self"), FileLoader): | ||
return func(self, *args, **kwargs) | ||
if frame_info.filename.endswith("_pytest/assertion/rewrite.py") and frame_info.function == "_read_pyc": | ||
return func(self, *args, **kwargs) | ||
if self.fileno() not in [sys.stdout.fileno(), sys.stderr.fileno()]: | ||
_raise_for_function(func) | ||
return func(self, *args, **kwargs) | ||
|
||
return file_op | ||
|
||
|
||
def init(): | ||
# time.sleep = _raise_if_blocking(time.sleep) | ||
|
||
os.read = _wrap_os_blocking(os.read) | ||
os.write = _wrap_os_blocking(os.write) | ||
|
||
socket.socket.send = _wrap_socket_blocking(socket.socket.send) | ||
socket.socket.sendall = _wrap_socket_blocking(socket.socket.sendall) | ||
socket.socket.sendto = _wrap_socket_blocking(socket.socket.sendto) | ||
socket.socket.recv = _wrap_socket_blocking(socket.socket.recv) | ||
socket.socket.recv_into = _wrap_socket_blocking(socket.socket.recv_into) | ||
socket.socket.recvfrom = _wrap_socket_blocking(socket.socket.recvfrom) | ||
socket.socket.recvfrom_into = _wrap_socket_blocking(socket.socket.recvfrom_into) | ||
socket.socket.recvmsg = _wrap_socket_blocking(socket.socket.recvmsg) | ||
socket.socket.recvmsg_into = _wrap_socket_blocking(socket.socket.recvmsg_into) | ||
|
||
ssl.SSLSocket.write = _wrap_socket_blocking(ssl.SSLSocket.write) | ||
ssl.SSLSocket.send = _wrap_socket_blocking(ssl.SSLSocket.send) | ||
ssl.SSLSocket.read = _wrap_socket_blocking(ssl.SSLSocket.read) | ||
ssl.SSLSocket.recv = _wrap_socket_blocking(ssl.SSLSocket.recv) | ||
|
||
forbiddenfruit.curse(io.BufferedReader, "read", _wrap_file_blocking(io.BufferedReader.read)) | ||
forbiddenfruit.curse(io.BufferedWriter, "read", _wrap_file_blocking(io.BufferedWriter.read)) | ||
forbiddenfruit.curse(io.BufferedWriter, "write", _wrap_file_blocking(io.BufferedWriter.write)) | ||
forbiddenfruit.curse(io.BufferedRandom, "read", _wrap_file_blocking(io.BufferedRandom.read)) | ||
forbiddenfruit.curse(io.BufferedRandom, "write", _wrap_file_blocking(io.BufferedRandom.write)) | ||
forbiddenfruit.curse(io.TextIOWrapper, "read", _wrap_file_blocking(io.TextIOWrapper.read)) | ||
forbiddenfruit.curse(io.TextIOWrapper, "write", _wrap_file_blocking(io.TextIOWrapper.write)) | ||
forbiddenfruit.curse(io.FileIO, "read", _wrap_file_blocking(io.FileIO.read)) | ||
forbiddenfruit.curse(io.FileIO, "write", _wrap_file_blocking(io.FileIO.write)) |
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