Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
singlestore-labs
GitHub Repository: singlestore-labs/singlestoredb-python
Path: blob/main/singlestoredb/apps/_stdout_supress.py
469 views
1
import io
2
import sys
3
from typing import Optional
4
5
6
class StdoutSuppressor:
7
"""
8
Supresses the stdout for code executed within the context.
9
This should not be used for asynchronous or threaded executions.
10
11
```py
12
with StdoutSupressor():
13
print("This won't be printed")
14
```
15
16
"""
17
18
def __enter__(self) -> None:
19
self.stdout = sys.stdout
20
self.buffer = io.StringIO()
21
sys.stdout = self.buffer
22
23
def __exit__(
24
self,
25
exc_type: Optional[object],
26
exc_value: Optional[Exception],
27
exc_traceback: Optional[str],
28
) -> None:
29
del self.buffer
30
sys.stdout = self.stdout
31
32