Skip to content

Commit

Permalink
Merge pull request #10 from pypr/fix-windows-capture
Browse files Browse the repository at this point in the history
Work around bug in capturing output on windows.
  • Loading branch information
prabhuramachandran committed Feb 28, 2019
2 parents 79cba2f + 8b44e9f commit 5511fde
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
8 changes: 8 additions & 0 deletions compyle/capture_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,17 @@ def __init__(self, stream=sys.stderr):
self._cached_output = None

def __enter__(self):
if sys.platform.startswith('win32') and sys.version_info[:2] > (3, 5):
return self
self.orig_stream = os.dup(self.fileno)
self.tmp_path = mktemp()
self.tmp_stream = open(self.tmp_path, 'w+')
os.dup2(self.tmp_stream.fileno(), self.fileno)
return self

def __exit__(self, type, value, tb):
if sys.platform.startswith('win32') and sys.version_info[:2] > (3, 5):
return
if self.orig_stream is not None:
os.dup2(self.orig_stream, self.fileno)
if self.tmp_stream is not None:
Expand All @@ -66,19 +70,23 @@ def get_output(self):
self._cache_output()
return self._cached_output


class CaptureMultipleStreams(object):
"""This lets one capture multiple streams together.
"""
def __init__(self, streams=None):
streams = (sys.stdout, sys.stderr) if streams is None else streams
self.streams = streams
self.captures = [CaptureStream(x) for x in streams]

def __enter__(self):
for capture in self.captures:
capture.__enter__()
return self

def __exit__(self, type, value, tb):
for capture in self.captures:
capture.__exit__(type, value, tb)

def get_output(self):
return tuple(x.get_output() for x in self.captures)
5 changes: 5 additions & 0 deletions compyle/tests/test_capture_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@
import sys
import unittest

import pytest

from ..capture_stream import CaptureMultipleStreams, CaptureStream

if sys.platform.startswith("win32") and sys.version_info[:2] > (3, 5):
pytest.skip("skipping capture tests on windows", allow_module_level=True)


def write_stderr():
subprocess.call(
Expand Down

0 comments on commit 5511fde

Please sign in to comment.