Commit 135660ea authored by Charles Ferguson's avatar Charles Ferguson
Browse files

Improved support for Python 3.

Python 3 is now better supported by the tests, and should be nearly
working with these changes. There are also a few changes to try to make
it easier to work on Windows, but this is still not working properly.
parent c4e16bea
...@@ -38,14 +38,32 @@ Example usage ...@@ -38,14 +38,32 @@ Example usage
print "Time: %s, Output: %s" % (chunk[0], chunk[1]) print "Time: %s, Output: %s" % (chunk[0], chunk[1])
""" """
import fcntl
import os import os
import Queue
import select import select
import subprocess import subprocess
import time import time
import threading import threading
try:
import fcntl
except ImportError:
# No fcntl module; might be running on non-POSIX system
fcntl = None
pass
try:
# Python 2 has Queue with a capital
import Queue as queue
except ImportError:
# Python 3 does not have a capital
import queue # pylint: disable=import-error
# Ensure that we can check basestring and unicode on Python 2 and 3.
try:
basestring
except NameError:
basestring = str # pylint: disable=redefined-builtin
class StreamedInputError(Exception): class StreamedInputError(Exception):
pass pass
...@@ -105,7 +123,7 @@ class StreamedInput(object): ...@@ -105,7 +123,7 @@ class StreamedInput(object):
self.proc = None self.proc = None
self.buffer_size = buffer_size self.buffer_size = buffer_size
self.stdoutfd = None self.stdoutfd = None
self.data_buffer = Queue.Queue() self.data_buffer = queue.Queue()
self.stop_timeout = stop_timeout self.stop_timeout = stop_timeout
self.stop_issued = False self.stop_issued = False
self.keep_stderr = keep_stderr self.keep_stderr = keep_stderr
...@@ -146,8 +164,11 @@ class StreamedInput(object): ...@@ -146,8 +164,11 @@ class StreamedInput(object):
Internal: Set the handle as non-blocking. Internal: Set the handle as non-blocking.
""" """
fd = self.stdoutfd fd = self.stdoutfd
flags = fcntl.fcntl(fd, fcntl.F_GETFL) if fcntl:
fcntl.fcntl(fd, fcntl.F_SETFL, flags | os.O_NONBLOCK) flags = fcntl.fcntl(fd, fcntl.F_GETFL)
fcntl.fcntl(fd, fcntl.F_SETFL, flags | os.O_NONBLOCK)
else:
print("Warning: Cannot mark subprocess output as non-blocking")
def _is_simple_buffer(self): def _is_simple_buffer(self):
""" """
...@@ -409,7 +430,7 @@ class ThreadedStreamedInput(StreamedInput): ...@@ -409,7 +430,7 @@ class ThreadedStreamedInput(StreamedInput):
def threadrun(): def threadrun():
try: try:
startfunc() startfunc()
except Exception as ex: except Exception as ex: # pylint: disable=broad-except
self.thread_exception = ex self.thread_exception = ex
if self.proc and self.proc.poll() is None: if self.proc and self.proc.poll() is None:
self._terminate() self._terminate()
......
...@@ -13,6 +13,7 @@ import os ...@@ -13,6 +13,7 @@ import os
import psutil import psutil
import sys import sys
import time import time
import types
import unittest import unittest
import nose import nose
...@@ -46,7 +47,15 @@ class Test01Basics(BaseStreamedInput): ...@@ -46,7 +47,15 @@ class Test01Basics(BaseStreamedInput):
def test_000_import(self): def test_000_import(self):
global streamedinput global streamedinput
import streamedinput # pylint: disable=redefined-outer-name try:
import streamedinput.streamedinput as streamedinput # pylint: disable=redefined-outer-name,relative-import,no-name-in-module
except ImportError:
import streamedinput # pylint: disable=redefined-outer-name,relative-import
self.assertIsInstance(streamedinput, types.ModuleType,
"'streamedinput' should have been imported as a module")
self.assertTrue(hasattr(streamedinput, 'StreamedInput'),
"'streamedinput' should have a 'StreamedInput' object")
def test_001_construct_array(self): def test_001_construct_array(self):
_ = streamedinput.StreamedInput(['echo', 'foo']) _ = streamedinput.StreamedInput(['echo', 'foo'])
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment