Example #1
0
File: io.py Project: cjerdonek/molt
def read(path, encoding, errors):
    """
    Read and return the contents of a text file as a unicode string.

    """
    # This function implementation was chosen to be compatible across Python 2/3.
    with open(path, 'rb') as f:
        b = f.read()

    try:
        return b.decode(encoding, errors)
    except UnicodeDecodeError, err:
        reraise("path: %s" % path)
Example #2
0
def make_doctest_test_suites(module_names):
    """
    Return a list of TestSuite instances for the doctests in the given modules.

    """
    suites = []
    for module_name in module_names:
        try:
            suite = doctest.DocTestSuite(module_name)
        except ImportError, err:
            extra_info = "Error creating doctests for: %s" % module_name
            reraise(extra_info)

        suites.append(suite)
Example #3
0
def call_script(args, b=None, shell=False):
    """
    Call the script with the given bytes sent to stdin.

    Returns a triple (stdout, stderr, returncode).

    """
    # See this page:
    #   http://stackoverflow.com/questions/163542/python-how-do-i-pass-a-string-into-subprocess-popen-using-the-stdin-argument

    try:
        proc = Popen(args, stdout=PIPE, stdin=PIPE, stderr=PIPE, shell=shell,
                     universal_newlines=False)
    except Exception as err:
        reraise("Error opening process: %s" % repr(args))

    stdout_data, stderr_data = proc.communicate(input=b)
    return_code = proc.returncode

    return stdout_data, stderr_data, return_code