`print`/`PrintCollector` not behaving as expected
## PROBLEM REPORT
The `PrintCollector` does not seem to collect prints that happen inside function definitions.
### What I did:
When you execute this code:
```
source_code = """def do_something():
print("function called")
print("foo")
return math.sqrt(2)
print(do_something())
print(math.sqrt(4))
print("hello")
"""
```
and capture the value of `printed` in the end, it's just
```
1.4142135623730951
2.0
hello
```
and not
```
function called
foo
1.4142135623730951
2.0
hello
```
as I would expect.
When I modify the code to check the `printed` value after each `print`, it seems the collector does indeed collect the values, but then forgets them again (maybe it's re-instantiated?):
```
['function called\n', 'function called\nfoo\n', '1.4142135623730951\n', '1.4142135623730951\n2.0\n',
'1.4142135623730951\n2.0\nhello\n']
```
You can find my entire sample code here: https://gist.github.com/epistoteles/f09311702fa06679a4ff64aca2790081
### What I expect to happen:
Prints get collected as they would when you execute the Python code normally and don't randomly "reset" midway.
### What version of Python and Zope/Addons I am using:
Python 3.11
关闭于 2025-03-03 1 条评论