mgedmin / mgedmin/profilehooks

@profile does not play well with pytest

Open
#27 0 comments 2 reactions 0 assignees View on GitHub
help wanted
Dominant language
Python
Stars
338
Forks
29
PR merge metrics
No merged PRs in 30d

Description

if I drop a

```

from profilehooks import profile
@profile
def test_something():
...
```

in a pytest test file and run pytest, I get this failure:

```
*** PROFILER RESULTS ***
test_search_by_title (/home/mg/Private/tilaajavastuu/bol/server/test/views/test_companies.py:200)
function called 1 times

Error in atexit._run_exitfuncs:
Traceback (most recent call last):
File ".../env/lib/python3.8/site-packages/profilehooks.py", line 411, in atexit
self.print_stats()
File ".../env/lib/python3.8/site-packages/profilehooks.py", line 394, in print_stats
stats.print_stats(self.entries)
File "/usr/lib/python3.8/pstats.py", line 369, in print_stats
print(indent, self.total_calls, "function calls", end=' ', file=self.stream)
File ".../env/lib/python3.8/site-packages/_pytest/capture.py", line 415, in write
self.buffer.write(obj)
ValueError: I/O operation on closed file
```

And if I do `@profile(immediate=True)`, I see nothing at all (because pytest captures it), unless I also use `pytest -s`.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start in profilehooks.py around print_stats and the atexit path shown in the traceback, then reproduce the decorated test under pytest. Compare normal and immediate profiling with and without pytest -s. Done means pytest no longer reports a closed-file error and the profiling output behaves as expected under capture.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.