GithubHelp home page GithubHelp logo

sloisel / streamcapture Goto Github PK

View Code? Open in Web Editor NEW
5.0 5.0 2.0 119 KB

Capture the outputs of Python streams, such as sys.stdout and sys.stderr

License: MIT License

Python 100.00%
logging python

streamcapture's People

Contributors

sloisel avatar

Stargazers

 avatar  avatar

Watchers

 avatar

streamcapture's Issues

Error when using StreamCapture directly with a file object

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.