Skip to content

Commit

Permalink
Python 2.7 compatibility fix: subprocess.DEVNULL
Browse files Browse the repository at this point in the history
As reported in #14 (comment),
Python 2.7 does not have subprocess.DEVNULL. Use os.devnull instead.
  • Loading branch information
hcho3 committed Mar 30, 2018
1 parent bb32ec0 commit 17e93cf
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
7 changes: 7 additions & 0 deletions python/treelite/common/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,11 @@ def _str_decode(string):
def _str_encode(string):
return string

# define DEVNULL
if PY3:
from subprocess import DEVNULL
else:
import os
DEVNULL = open(os.devnull, 'r+b')

__all__ = []
5 changes: 2 additions & 3 deletions python/treelite/contrib/gcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import os
import subprocess

from ..common.compat import DEVNULL
from ..common.util import TemporaryDirectory
from .util import _create_shared_base, _libext, _shell

Expand All @@ -22,9 +23,7 @@ def _openmp_supported(toolchain):
retcode = subprocess.call('{} -o {} {} -fopenmp'\
.format(toolchain, output, sfile),
shell=True,
stdin=subprocess.DEVNULL,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL)
stdin=DEVNULL, stdout=DEVNULL, stderr=DEVNULL)
return retcode == 0

def _obj_ext():
Expand Down
6 changes: 2 additions & 4 deletions python/treelite/contrib/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import subprocess
from sys import platform as _platform
from multiprocessing import cpu_count
from ..common.compat import _str_decode, _str_encode
from ..common.compat import _str_decode, _str_encode, DEVNULL
from ..common.util import TreeliteError, lineno, log_info

def _is_windows():
Expand All @@ -15,9 +15,7 @@ def _toolchain_exist_check(toolchain):
if toolchain != 'msvc':
retcode = subprocess.call('{} --version'.format(toolchain),
shell=True,
stdin=subprocess.DEVNULL,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL)
stdin=DEVNULL, stdout=DEVNULL, stderr=DEVNULL)
if retcode != 0:
raise ValueError('Toolchain {} not found. '.format(toolchain) +
'Ensure that it is installed and that it is a variant ' +
Expand Down

0 comments on commit 17e93cf

Please sign in to comment.