sloisel / streamcapture Goto Github PK
View Code? Open in Web Editor NEWCapture the outputs of Python streams, such as sys.stdout and sys.stderr
License: MIT License
Capture the outputs of Python streams, such as sys.stdout and sys.stderr
License: MIT License
Prior to version 1.2 of the streamcapture package, the following code worked:
from streamcapture import StreamCapture
from sys import stdout
with StreamCapture(stdout, open('stdout.log', 'wb')):
print('test')
After upgrading to streamcapture 1.2, it fails with the following error:
Exception in thread Thread-1 (printer):
>>> Traceback (most recent call last):
File "/usr/local/lib/python3.11/threading.py", line 1038, in _bootstrap_inner
self.run()
File "/usr/local/lib/python3.11/threading.py", line 975, in run
self._target(*self._args, **self._kwargs)
File "/usr/local/lib/python3.11/site-packages/streamcapture/__init__.py", line 159, in printer
self.writer.write_from(data,self)
^^^^^^^^^^^^^^^^^^^^^^
AttributeError: '_io.BufferedWriter' object has no attribute 'write_from'
I saw that StreamCapture now can also be used with the streamcapture.Writer
object to have more control when two streams should be captured to the same target stream. But it seems that you accidentally made using Writer
necessary.
I chose the above example to be very minimal to make the issue nicely visible. But another example is the one from the Readme:
import streamcapture, sys, os
print("This does not get saved to the log file")
with streamcapture.StreamCapture(sys.stdout,open('logfile.txt','wb')):
os.write(sys.stdout.fileno(),b"Hello, captured world!\n")
os.system('echo Hello from the shell')
print("More capturing")
print("This also does not get saved to the log file")
will result in:
This does not get saved to the log file
Exception in thread Thread-1 (printer):
Traceback (most recent call last):
File "/usr/local/lib/python3.11/threading.py", line 1038, in _bootstrap_inner
sh: 1: echo: echo: I/O error
self.run()
File "/usr/local/lib/python3.11/threading.py", line 975, in run
Traceback (most recent call last):
File "//test.py", line 6, in <module>
self._target(*self._args, **self._kwargs)
File "/usr/local/lib/python3.11/site-packages/streamcapture/__init__.py", line 159, in printer
self.writer.write_from(data,self)
^^^^^^^^^^^^^^^^^^^^^^
AttributeError: '_io.BufferedWriter' object has no attribute 'write_from'
print("More capturing")
BrokenPipeError: [Errno 32] Broken pipe
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "//test.py", line 3, in <module>
with streamcapture.StreamCapture(sys.stdout,open('logfile.txt','wb')):
File "/usr/local/lib/python3.11/site-packages/streamcapture/__init__.py", line 196, in __exit__
self.close()
File "/usr/local/lib/python3.11/site-packages/streamcapture/__init__.py", line 189, in close
self.stream.flush()
BrokenPipeError: [Errno 32] Broken pipe
Exception ignored in: <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>
BrokenPipeError: [Errno 32] Broken pipe
I tested the above with Python 3.11 in docker, but other Python versions behave very similar.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.