Skip to content

Commit

Permalink
Don't trigger exception if file doesn't have a fileno in read ops (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
cbornet authored Feb 1, 2025
1 parent db28cb4 commit 684023b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
14 changes: 13 additions & 1 deletion blockbuster/blockbuster.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,13 @@ def _get_io_wrapped_functions(
stdout = sys.stdout
stderr = sys.stderr

def file_read_exclude(file: io.IOBase, *_: Any, **__: Any) -> bool:
try:
file.fileno()
except io.UnsupportedOperation:
return not file.isatty()
return False

def file_write_exclude(file: io.IOBase, *_: Any, **__: Any) -> bool:
if file in {stdout, stderr, sys.stdout, sys.stderr} or file.isatty():
return True
Expand All @@ -320,6 +327,7 @@ def file_write_exclude(file: io.IOBase, *_: Any, **__: Any) -> bool:
("<frozen importlib._bootstrap_external>", {"get_data"}),
("_pytest/assertion/rewrite.py", {"_rewrite_test", "_read_pyc"}),
],
can_block_predicate=file_read_exclude,
scanned_modules=modules,
),
"io.BufferedWriter.write": BlockBusterFunction(
Expand All @@ -330,7 +338,10 @@ def file_write_exclude(file: io.IOBase, *_: Any, **__: Any) -> bool:
scanned_modules=modules,
),
"io.BufferedRandom.read": BlockBusterFunction(
io.BufferedRandom, "read", scanned_modules=modules
io.BufferedRandom,
"read",
can_block_predicate=file_read_exclude,
scanned_modules=modules,
),
"io.BufferedRandom.write": BlockBusterFunction(
io.BufferedRandom,
Expand All @@ -342,6 +353,7 @@ def file_write_exclude(file: io.IOBase, *_: Any, **__: Any) -> bool:
io.TextIOWrapper,
"read",
can_block_functions=[("aiofile/version.py", {"<module>"})],
can_block_predicate=file_read_exclude,
scanned_modules=modules,
),
"io.TextIOWrapper.write": BlockBusterFunction(
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "blockbuster"
version = "1.5.13"
version = "1.5.14"
description = "Utility to detect blocking calls in the async event loop"
readme = "README.md"
keywords = ["async", "block", "detect", "event loop", "asyncio"]
Expand Down
2 changes: 1 addition & 1 deletion uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 684023b

Please sign in to comment.