# Path: ranger/ext/popen23.py
# @contextmanager
# def Popen23(*args, **kwargs):  # pylint: disable=invalid-name
#     if PY3:
#         yield Popen(*args, **kwargs)
#         return
#     else:
#         popen2 = Popen(*args, **kwargs)
#     try:
#         yield popen2
#     finally:
#         # From Lib/subprocess.py Popen.__exit__:
#         if popen2.stdout:
#             popen2.stdout.close()
#         if popen2.stderr:
#             popen2.stderr.close()
#         try:  # Flushing a BufferedWriter may raise an error
#             if popen2.stdin:
#                 popen2.stdin.close()
#         finally:
#             # Wait for the process to terminate, to avoid zombies.
#             popen2.wait()


from io import open
from os import devnull
from subprocess import PIPE, CalledProcessError
from ranger.ext.popen23 import Popen23



ENCODING = 'utf-8'


def check_output(popenargs, **kwargs):
    """Runs a program, waits for its termination and returns its output

    This function is functionally identical to python 2.7's subprocess.check_output,
    but is favored due to python 2.6 compatibility.

    Will be run through a shell if `popenargs` is a string, otherwise the command
    is executed directly.

    The keyword argument `decode` determines if the output shall be decoded
    with the encoding UTF-8.

    Further keyword arguments are passed to Popen.
    """

    do_decode = kwargs.pop('decode', True)
    kwargs.setdefault('stdout', PIPE)
    kwargs.setdefault('shell', isinstance(popenargs, str))

    if 'stderr' in kwargs:
        with Popen23(popenargs, **kwargs) as process:
            stdout, _ = process.communicate()
    else:
        with open(devnull, mode='w', encoding="utf-8") as fd_devnull:

# Path: ranger/ext/popen23.py
# @contextmanager
# def Popen23(*args, **kwargs):  # pylint: disable=invalid-name
#     if PY3:
#         yield Popen(*args, **kwargs)
#         return
#     else:
#         popen2 = Popen(*args, **kwargs)
#     try:
#         yield popen2
#     finally:
#         # From Lib/subprocess.py Popen.__exit__:
#         if popen2.stdout:
#             popen2.stdout.close()
#         if popen2.stderr:
#             popen2.stderr.close()
#         try:  # Flushing a BufferedWriter may raise an error
#             if popen2.stdin:
#                 popen2.stdin.close()
#         finally:
#             # Wait for the process to terminate, to avoid zombies.
#             popen2.wait()


# Path: ranger/ext/spawn.py
from io import open
from os import devnull
from subprocess import PIPE, CalledProcessError
from ranger.ext.popen23 import Popen23




ENCODING = 'utf-8'


def check_output(popenargs, **kwargs):
    """Runs a program, waits for its termination and returns its output

    This function is functionally identical to python 2.7's subprocess.check_output,
    but is favored due to python 2.6 compatibility.

    Will be run through a shell if `popenargs` is a string, otherwise the command
    is executed directly.

    The keyword argument `decode` determines if the output shall be decoded
    with the encoding UTF-8.

    Further keyword arguments are passed to Popen.
    """

    do_decode = kwargs.pop('decode', True)
    kwargs.setdefault('stdout', PIPE)
    kwargs.setdefault('shell', isinstance(popenargs, str))

    if 'stderr' in kwargs:
        with Popen23(popenargs, **kwargs) as process:
            stdout, _ = process.communicate()
    else:
        with open(devnull, mode='w', encoding="utf-8") as fd_devnull:

            with Popen23(popenargs, stderr=fd_devnull, **kwargs) as process: