Example #1
0
    def test_file_opening_hook(self):
        try:
            # cannot use openhook and inplace mode
            fi = FileInput(inplace=1, openhook=lambda f, m: None)
            self.fail("FileInput should raise if both inplace "
                             "and openhook arguments are given")
        except ValueError:
            pass
        try:
            fi = FileInput(openhook=1)
            self.fail("FileInput should check openhook for being callable")
        except ValueError:
            pass

        class CustomOpenHook:
            def __init__(self):
                self.invoked = False
            def __call__(self, *args):
                self.invoked = True
                return open(*args)

        t = writeTmp(1, ["\n"])
        self.addCleanup(remove_tempfiles, t)
        custom_open_hook = CustomOpenHook()
        with FileInput([t], openhook=custom_open_hook) as fi:
            fi.readline()
        self.assertTrue(custom_open_hook.invoked, "openhook not invoked")
Example #2
0
    def parse_inout_dict(self, f: FileInput) -> None:
        """
        Parses Input and Output
        """
        input_storage_matcher = self.parse_inout_line.match(f.readline())
        output_storage_matcher = self.parse_inout_line.match(f.readline())

        self.inout_dict = {
            "input": {
                "from": int(input_storage_matcher.group(2)),
            },
            "output": {
                "from": int(output_storage_matcher.group(2)),
            }
        }

        if input_storage_matcher.group(4):
            self.inout_dict["input"]["to"] = int(
                input_storage_matcher.group(4))
        else:
            self.inout_dict["input"]["to"] = None

        if output_storage_matcher.group(4):
            self.inout_dict["output"]["to"] = int(
                output_storage_matcher.group(4))
        else:
            self.inout_dict["output"]["to"] = None
Example #3
0
    def test_file_opening_hook(self):
        try:
            # cannot use openhook and inplace mode
            fi = FileInput(inplace=1, openhook=lambda f, m: None)
            self.fail("FileInput should raise if both inplace "
                      "and openhook arguments are given")
        except ValueError:
            pass
        try:
            fi = FileInput(openhook=1)
            self.fail("FileInput should check openhook for being callable")
        except ValueError:
            pass

        class CustomOpenHook:
            def __init__(self):
                self.invoked = False

            def __call__(self, *args):
                self.invoked = True
                return open(*args)

        t = writeTmp(1, ["\n"])
        self.addCleanup(remove_tempfiles, t)
        custom_open_hook = CustomOpenHook()
        with FileInput([t], openhook=custom_open_hook) as fi:
            fi.readline()
        self.assertTrue(custom_open_hook.invoked, "openhook not invoked")
Example #4
0
 def test_readline_buffering(self):
     src = LineReader()
     fi = FileInput(files=['line1\nline2', 'line3\n'], openhook=src.openhook)
     self.assertEqual(src.linesread, [])
     self.assertEqual(fi.readline(), 'line1\n')
     self.assertEqual(src.linesread, ['line1\n'])
     self.assertEqual(fi.readline(), 'line2')
     self.assertEqual(src.linesread, ['line2'])
     self.assertEqual(fi.readline(), 'line3\n')
     self.assertEqual(src.linesread, ['', 'line3\n'])
     self.assertEqual(fi.readline(), '')
     self.assertEqual(src.linesread, [''])
     self.assertEqual(fi.readline(), '')
     self.assertEqual(src.linesread, [])
     fi.close()
 def test_readline_buffering(self):
     src = LineReader()
     fi = FileInput(files=['line1\nline2', 'line3\n'], openhook=src.openhook)
     self.assertEqual(src.linesread, [])
     self.assertEqual(fi.readline(), 'line1\n')
     self.assertEqual(src.linesread, ['line1\n'])
     self.assertEqual(fi.readline(), 'line2')
     self.assertEqual(src.linesread, ['line2'])
     self.assertEqual(fi.readline(), 'line3\n')
     self.assertEqual(src.linesread, ['', 'line3\n'])
     self.assertEqual(fi.readline(), '')
     self.assertEqual(src.linesread, [''])
     self.assertEqual(fi.readline(), '')
     self.assertEqual(src.linesread, [])
     fi.close()
Example #6
0
    def test_zero_byte_files(self):
        t1 = self.writeTmp("")
        t2 = self.writeTmp("")
        t3 = self.writeTmp("The only line there is.\n")
        t4 = self.writeTmp("")
        fi = FileInput(files=(t1, t2, t3, t4))

        line = fi.readline()
        self.assertEqual(line, 'The only line there is.\n')
        self.assertEqual(fi.lineno(), 1)
        self.assertEqual(fi.filelineno(), 1)
        self.assertEqual(fi.filename(), t3)

        line = fi.readline()
        self.assertFalse(line)
        self.assertEqual(fi.lineno(), 1)
        self.assertEqual(fi.filelineno(), 0)
        self.assertEqual(fi.filename(), t4)
        fi.close()
Example #7
0
    def test_readline(self):
        with open(TESTFN, 'wb') as f:
            f.write('A\nB\r\nC\r')
            # Fill TextIOWrapper buffer.
            f.write('123456789\n' * 1000)
            # Issue #20501: readline() shouldn't read whole file.
            f.write('\x80')
        self.addCleanup(safe_unlink, TESTFN)

        fi = FileInput(files=TESTFN, openhook=hook_encoded('ascii'), bufsize=8)
        # The most likely failure is a UnicodeDecodeError due to the entire
        # file being read when it shouldn't have been.
        self.assertEqual(fi.readline(), u'A\n')
        self.assertEqual(fi.readline(), u'B\r\n')
        self.assertEqual(fi.readline(), u'C\r')
        with self.assertRaises(UnicodeDecodeError):
            # Read to the end of file.
            list(fi)
        fi.close()
Example #8
0
    def test_readline(self):
        with open(TESTFN, 'wb') as f:
            f.write('A\nB\r\nC\r')
            # Fill TextIOWrapper buffer.
            f.write('123456789\n' * 1000)
            # Issue #20501: readline() shouldn't read whole file.
            f.write('\x80')
        self.addCleanup(safe_unlink, TESTFN)

        fi = FileInput(files=TESTFN, openhook=hook_encoded('ascii'), bufsize=8)
        # The most likely failure is a UnicodeDecodeError due to the entire
        # file being read when it shouldn't have been.
        self.assertEqual(fi.readline(), u'A\n')
        self.assertEqual(fi.readline(), u'B\r\n')
        self.assertEqual(fi.readline(), u'C\r')
        with self.assertRaises(UnicodeDecodeError):
            # Read to the end of file.
            list(fi)
        fi.close()
Example #9
0
    def test_zero_byte_files(self):
        t1 = self.writeTmp("")
        t2 = self.writeTmp("")
        t3 = self.writeTmp("The only line there is.\n")
        t4 = self.writeTmp("")
        fi = FileInput(files=(t1, t2, t3, t4))

        line = fi.readline()
        self.assertEqual(line, 'The only line there is.\n')
        self.assertEqual(fi.lineno(), 1)
        self.assertEqual(fi.filelineno(), 1)
        self.assertEqual(fi.filename(), t3)

        line = fi.readline()
        self.assertFalse(line)
        self.assertEqual(fi.lineno(), 1)
        self.assertEqual(fi.filelineno(), 0)
        self.assertEqual(fi.filename(), t4)
        fi.close()
 def test_zero_byte_files(self):
     t1 = t2 = t3 = t4 = None
     try:
         t1 = writeTmp(1, [''])
         t2 = writeTmp(2, [''])
         t3 = writeTmp(3, ['The only line there is.\n'])
         t4 = writeTmp(4, [''])
         fi = FileInput(files=(t1, t2, t3, t4))
         line = fi.readline()
         self.assertEqual(line, 'The only line there is.\n')
         self.assertEqual(fi.lineno(), 1)
         self.assertEqual(fi.filelineno(), 1)
         self.assertEqual(fi.filename(), t3)
         line = fi.readline()
         self.assertFalse(line)
         self.assertEqual(fi.lineno(), 1)
         self.assertEqual(fi.filelineno(), 0)
         self.assertEqual(fi.filename(), t4)
         fi.close()
     finally:
         remove_tempfiles(t1, t2, t3, t4)
Example #11
0
    def test_zero_byte_files(self):
        try:
            t1 = writeTmp(1, [""])
            t2 = writeTmp(2, [""])
            t3 = writeTmp(3, ["The only line there is.\n"])
            t4 = writeTmp(4, [""])
            fi = FileInput(files=(t1, t2, t3, t4))

            line = fi.readline()
            self.assertEqual(line, 'The only line there is.\n')
            self.assertEqual(fi.lineno(), 1)
            self.assertEqual(fi.filelineno(), 1)
            self.assertEqual(fi.filename(), t3)

            line = fi.readline()
            self.failIf(line)
            self.assertEqual(fi.lineno(), 1)
            self.assertEqual(fi.filelineno(), 0)
            self.assertEqual(fi.filename(), t4)
            fi.close()
        finally:
            remove_tempfiles(t1, t2, t3, t4)
Example #12
0
class ManyFile(object):
    """A generalized version of file input that allows for stopping
    reading the current file and switching to a different file.
    Reading the old file is resumed once the current file is complete.
    Useful for #include or similiar constructs.
    """

    def __init__(self, files=None, hook = None):
        self.fi = [] # an array of file input objects.  We push and pop onto this so we can resume.
        self.files = [] # an array of files that need to be read later.
        self.hook = None
        self.current = None # the current file input object.
        self.line = 0
        if files is not None:
            self.current = FileInput(files, openhook = self.hook)

    def include(self, files):
        if self.current is not None:
            self.fi.push(self.current)
        self.current = FileInput(files, openhook = self.hook)
            
    def fileno(self):
        if self.current is None:
            return None
        return self.current.fileno()

    def filename(self):
        if self.current is None:
            return None
        return self.current.filename()

    def lineno(self):
        return self.line

    def filelineno(self):
        if self.current is None:
            return None
        return self.current.filelineno()

    def isfirstline(self):
        if self.current is None:
            return None
        return self.current.isfirstline()

    def isstdin(self):
        return False

    def nextfile(self):
        if self.current is not None:
            self.current.nextfile()

    def close(self):
        if self.current:
            self.current.close()
        self.fi = []
        self.files = []

    def readline(self):
        if self.current is not None:
            l = self.current.readline()
            if len(l) > 0:
                self.line = self.line + 1
                return l

        # Advance to the next file
        if len (self.fi) > 0:
            self.current = self.fi.pop()
            return self.readline()

        # see if there are any files left to open
        if len (self.files) > 0:
            self.current = FileInput(self.files, openhook = fileinput.hook_compressed)
            self.files = []
            return self.readline()

        return ""

    def __getimem__ (self):
        return self.readline()

    def __iter__(self):
        return self

    def next(self):
        l = self.__getimem__()
        if l != '':
            return l
        raise StopIteration
Example #13
0
'''
Example #14
0
def runTests(t1, t2, t3, t4, bs=0, round=0):
    start = 1 + round * 6
    if verbose:
        print '%s. Simple iteration (bs=%s)' % (start + 0, bs)
    fi = FileInput(files=(t1, t2, t3, t4), bufsize=bs)
    lines = list(fi)
    fi.close()
    verify(len(lines) == 31)
    verify(lines[4] == 'Line 5 of file 1\n')
    verify(lines[30] == 'Line 1 of file 4\n')
    verify(fi.lineno() == 31)
    verify(fi.filename() == t4)

    if verbose:
        print '%s. Status variables (bs=%s)' % (start + 1, bs)
    fi = FileInput(files=(t1, t2, t3, t4), bufsize=bs)
    s = "x"
    while s and s != 'Line 6 of file 2\n':
        s = fi.readline()
    verify(fi.filename() == t2)
    verify(fi.lineno() == 21)
    verify(fi.filelineno() == 6)
    verify(not fi.isfirstline())
    verify(not fi.isstdin())

    if verbose:
        print '%s. Nextfile (bs=%s)' % (start + 2, bs)
    fi.nextfile()
    verify(fi.readline() == 'Line 1 of file 3\n')
    verify(fi.lineno() == 22)
    fi.close()

    if verbose:
        print '%s. Stdin (bs=%s)' % (start + 3, bs)
    fi = FileInput(files=(t1, t2, t3, t4, '-'), bufsize=bs)
    savestdin = sys.stdin
    try:
        sys.stdin = StringIO("Line 1 of stdin\nLine 2 of stdin\n")
        lines = list(fi)
        verify(len(lines) == 33)
        verify(lines[32] == 'Line 2 of stdin\n')
        verify(fi.filename() == '<stdin>')
        fi.nextfile()
    finally:
        sys.stdin = savestdin

    if verbose:
        print '%s. Boundary conditions (bs=%s)' % (start + 4, bs)
    fi = FileInput(files=(t1, t2, t3, t4), bufsize=bs)
    verify(fi.lineno() == 0)
    verify(fi.filename() == None)
    fi.nextfile()
    verify(fi.lineno() == 0)
    verify(fi.filename() == None)

    if verbose:
        print '%s. Inplace (bs=%s)' % (start + 5, bs)
    savestdout = sys.stdout
    try:
        fi = FileInput(files=(t1, t2, t3, t4), inplace=1, bufsize=bs)
        for line in fi:
            line = line[:-1].upper()
            print line
        fi.close()
    finally:
        sys.stdout = savestdout

    fi = FileInput(files=(t1, t2, t3, t4), bufsize=bs)
    for line in fi:
        verify(line[-1] == '\n')
        m = pat.match(line[:-1])
        verify(m != None)
        verify(int(m.group(1)) == fi.filelineno())
    fi.close()
Example #15
0
    try:
        writeFiles()
        runTests(t1, t2, t3, t4, bs, round)
    finally:
        remove_tempfiles(t1, t2, t3, t4)

# Next, check for proper behavior with 0-byte files.
if verbose:
    print "13. 0-byte files"
try:
    t1 = writeTmp(1, [""])
    t2 = writeTmp(2, [""])
    t3 = writeTmp(3, ["The only line there is.\n"])
    t4 = writeTmp(4, [""])
    fi = FileInput(files=(t1, t2, t3, t4))
    line = fi.readline()
    verify(line == 'The only line there is.\n')
    verify(fi.lineno() == 1)
    verify(fi.filelineno() == 1)
    verify(fi.filename() == t3)
    line = fi.readline()
    verify(not line)
    verify(fi.lineno() == 1)
    verify(fi.filelineno() == 0)
    verify(fi.filename() == t4)
    fi.close()
finally:
    remove_tempfiles(t1, t2, t3, t4)

if verbose:
    print "14. Files that don't end with newline"
Example #16
0
        return None

    t0 = (a1 * c2 - a2 * c1) / float(denominator)
    if t0 < 0 or t0 > 1:
        return None

    p = (x1 + s0 * (x2 - x1), y1 + s0 * (y2 - y1))
    #q = (x3 + t0 * (x4 - x3), y3 + t0 * (y4 - y3))

    return p


def intersections(path1, path2):
    results = []
    for seg1 in path1:
        for seg2 in path2:
            cross = intersection(seg1, seg2)
            if cross and cross != (0, 0):
                results.append(cross)
    return results


if __name__ == "__main__":
    fi = FileInput()
    path1 = decode(fi.readline())
    path2 = decode(fi.readline())
    results = intersections(path1, path2)
    results.sort(key=manhattan)
    print results
    print manhattan(results[0])
Example #17
0
class ManyFile(object):
    """A generalized version of file input that allows for stopping
    reading the current file and switching to a different file.
    Reading the old file is resumed once the current file is complete.
    Useful for #include or similiar constructs.
    """
    def __init__(self, files=None, hook=None):
        self.fi = [
        ]  # an array of file input objects.  We push and pop onto this so we can resume.
        self.files = []  # an array of files that need to be read later.
        self.hook = None
        self.current = None  # the current file input object.
        self.line = 0
        if files is not None:
            self.current = FileInput(files, openhook=self.hook)

    def include(self, files):
        if self.current is not None:
            self.fi.push(self.current)
        self.current = FileInput(files, openhook=self.hook)

    def fileno(self):
        if self.current is None:
            return None
        return self.current.fileno()

    def filename(self):
        if self.current is None:
            return None
        return self.current.filename()

    def lineno(self):
        return self.line

    def filelineno(self):
        if self.current is None:
            return None
        return self.current.filelineno()

    def isfirstline(self):
        if self.current is None:
            return None
        return self.current.isfirstline()

    def isstdin(self):
        return False

    def nextfile(self):
        if self.current is not None:
            self.current.nextfile()

    def close(self):
        if self.current:
            self.current.close()
        self.fi = []
        self.files = []

    def readline(self):
        if self.current is not None:
            l = self.current.readline()
            if len(l) > 0:
                self.line = self.line + 1
                return l

        # Advance to the next file
        if len(self.fi) > 0:
            self.current = self.fi.pop()
            return self.readline()

        # see if there are any files left to open
        if len(self.files) > 0:
            self.current = FileInput(self.files,
                                     openhook=fileinput.hook_compressed)
            self.files = []
            return self.readline()

        return ""

    def __getimem__(self):
        return self.readline()

    def __iter__(self):
        return self

    def next(self):
        l = self.__getimem__()
        if l != '':
            return l
        raise StopIteration
Example #18
0
'''
Example #19
0
    def test_buffer_sizes(self):

        t1 = self.writeTmp(''.join("Line %s of file 1\n" % (i + 1)
                                   for i in range(15)))
        t2 = self.writeTmp(''.join("Line %s of file 2\n" % (i + 1)
                                   for i in range(10)))
        t3 = self.writeTmp(''.join("Line %s of file 3\n" % (i + 1)
                                   for i in range(5)))
        t4 = self.writeTmp(''.join("Line %s of file 4\n" % (i + 1)
                                   for i in range(1)))

        pat = re.compile(r'LINE (\d+) OF FILE (\d+)')

        if verbose:
            print('1. Simple iteration')
        fi = FileInput(files=(t1, t2, t3, t4))
        lines = list(fi)
        fi.close()
        self.assertEqual(len(lines), 31)
        self.assertEqual(lines[4], 'Line 5 of file 1\n')
        self.assertEqual(lines[30], 'Line 1 of file 4\n')
        self.assertEqual(fi.lineno(), 31)
        self.assertEqual(fi.filename(), t4)

        if verbose:
            print('2. Status variables')
        fi = FileInput(files=(t1, t2, t3, t4))
        s = "x"
        while s and s != 'Line 6 of file 2\n':
            s = fi.readline()
        self.assertEqual(fi.filename(), t2)
        self.assertEqual(fi.lineno(), 21)
        self.assertEqual(fi.filelineno(), 6)
        self.assertFalse(fi.isfirstline())
        self.assertFalse(fi.isstdin())

        if verbose:
            print('3. Nextfile')
        fi.nextfile()
        self.assertEqual(fi.readline(), 'Line 1 of file 3\n')
        self.assertEqual(fi.lineno(), 22)
        fi.close()

        if verbose:
            print('4. Stdin')
        fi = FileInput(files=(t1, t2, t3, t4, '-'))
        savestdin = sys.stdin
        try:
            sys.stdin = StringIO("Line 1 of stdin\nLine 2 of stdin\n")
            lines = list(fi)
            self.assertEqual(len(lines), 33)
            self.assertEqual(lines[32], 'Line 2 of stdin\n')
            self.assertEqual(fi.filename(), '<stdin>')
            fi.nextfile()
        finally:
            sys.stdin = savestdin

        if verbose:
            print('5. Boundary conditions')
        fi = FileInput(files=(t1, t2, t3, t4))
        self.assertEqual(fi.lineno(), 0)
        self.assertEqual(fi.filename(), None)
        fi.nextfile()
        self.assertEqual(fi.lineno(), 0)
        self.assertEqual(fi.filename(), None)

        if verbose:
            print('6. Inplace')
        savestdout = sys.stdout
        try:
            fi = FileInput(files=(t1, t2, t3, t4), inplace=1)
            for line in fi:
                line = line[:-1].upper()
                print(line)
            fi.close()
        finally:
            sys.stdout = savestdout

        fi = FileInput(files=(t1, t2, t3, t4))
        for line in fi:
            self.assertEqual(line[-1], '\n')
            m = pat.match(line[:-1])
            self.assertNotEqual(m, None)
            self.assertEqual(int(m.group(1)), fi.filelineno())
        fi.close()
Example #20
0
 def __init__(self, source: FileInput):
     self.source = source
     self.firstLine = source.readline()
     self.table = SymbolTable(source.filename())
Example #21
0
    def buffer_size_test(self, t1, t2, t3, t4, bs=0, round=0):
        pat = re.compile(r'LINE (\d+) OF FILE (\d+)')

        start = 1 + round*6
        if verbose:
            print('%s. Simple iteration (bs=%s)' % (start+0, bs))
        fi = FileInput(files=(t1, t2, t3, t4), bufsize=bs)
        lines = list(fi)
        fi.close()
        self.assertEqual(len(lines), 31)
        self.assertEqual(lines[4], 'Line 5 of file 1\n')
        self.assertEqual(lines[30], 'Line 1 of file 4\n')
        self.assertEqual(fi.lineno(), 31)
        self.assertEqual(fi.filename(), t4)

        if verbose:
            print('%s. Status variables (bs=%s)' % (start+1, bs))
        fi = FileInput(files=(t1, t2, t3, t4), bufsize=bs)
        s = "x"
        while s and s != 'Line 6 of file 2\n':
            s = fi.readline()
        self.assertEqual(fi.filename(), t2)
        self.assertEqual(fi.lineno(), 21)
        self.assertEqual(fi.filelineno(), 6)
        self.assertFalse(fi.isfirstline())
        self.assertFalse(fi.isstdin())

        if verbose:
            print('%s. Nextfile (bs=%s)' % (start+2, bs))
        fi.nextfile()
        self.assertEqual(fi.readline(), 'Line 1 of file 3\n')
        self.assertEqual(fi.lineno(), 22)
        fi.close()

        if verbose:
            print('%s. Stdin (bs=%s)' % (start+3, bs))
        fi = FileInput(files=(t1, t2, t3, t4, '-'), bufsize=bs)
        savestdin = sys.stdin
        try:
            sys.stdin = StringIO("Line 1 of stdin\nLine 2 of stdin\n")
            lines = list(fi)
            self.assertEqual(len(lines), 33)
            self.assertEqual(lines[32], 'Line 2 of stdin\n')
            self.assertEqual(fi.filename(), '<stdin>')
            fi.nextfile()
        finally:
            sys.stdin = savestdin

        if verbose:
            print('%s. Boundary conditions (bs=%s)' % (start+4, bs))
        fi = FileInput(files=(t1, t2, t3, t4), bufsize=bs)
        self.assertEqual(fi.lineno(), 0)
        self.assertEqual(fi.filename(), None)
        fi.nextfile()
        self.assertEqual(fi.lineno(), 0)
        self.assertEqual(fi.filename(), None)

        if verbose:
            print('%s. Inplace (bs=%s)' % (start+5, bs))
        savestdout = sys.stdout
        try:
            fi = FileInput(files=(t1, t2, t3, t4), inplace=1, bufsize=bs)
            for line in fi:
                line = line[:-1].upper()
                print(line)
            fi.close()
        finally:
            sys.stdout = savestdout

        fi = FileInput(files=(t1, t2, t3, t4), bufsize=bs)
        for line in fi:
            self.assertEqual(line[-1], '\n')
            m = pat.match(line[:-1])
            self.assertNotEqual(m, None)
            self.assertEqual(int(m.group(1)), fi.filelineno())
        fi.close()
Example #22
0
    try:
        writeFiles()
        runTests(t1, t2, t3, t4, bs, round)
    finally:
        remove_tempfiles(t1, t2, t3, t4)

# Next, check for proper behavior with 0-byte files.
if verbose:
    print "13. 0-byte files"
try:
    t1 = writeTmp(1, [""])
    t2 = writeTmp(2, [""])
    t3 = writeTmp(3, ["The only line there is.\n"])
    t4 = writeTmp(4, [""])
    fi = FileInput(files=(t1, t2, t3, t4))
    line = fi.readline()
    verify(line == 'The only line there is.\n')
    verify(fi.lineno() == 1)
    verify(fi.filelineno() == 1)
    verify(fi.filename() == t3)
    line = fi.readline()
    verify(not line)
    verify(fi.lineno() == 1)
    verify(fi.filelineno() == 0)
    verify(fi.filename() == t4)
    fi.close()
finally:
    remove_tempfiles(t1, t2, t3, t4)

if verbose:
    print "14. Files that don't end with newline"
Example #23
0
    def buffer_size_test(self, t1, t2, t3, t4, bs=0, round=0):
        pat = re.compile(r'LINE (\d+) OF FILE (\d+)')

        start = 1 + round * 6
        if verbose:
            print('%s. Simple iteration (bs=%s)' % (start + 0, bs))
        fi = FileInput(files=(t1, t2, t3, t4), bufsize=bs)
        lines = list(fi)
        fi.close()
        self.assertEqual(len(lines), 31)
        self.assertEqual(lines[4], 'Line 5 of file 1\n')
        self.assertEqual(lines[30], 'Line 1 of file 4\n')
        self.assertEqual(fi.lineno(), 31)
        self.assertEqual(fi.filename(), t4)

        if verbose:
            print('%s. Status variables (bs=%s)' % (start + 1, bs))
        fi = FileInput(files=(t1, t2, t3, t4), bufsize=bs)
        s = "x"
        while s and s != 'Line 6 of file 2\n':
            s = fi.readline()
        self.assertEqual(fi.filename(), t2)
        self.assertEqual(fi.lineno(), 21)
        self.assertEqual(fi.filelineno(), 6)
        self.assertFalse(fi.isfirstline())
        self.assertFalse(fi.isstdin())

        if verbose:
            print('%s. Nextfile (bs=%s)' % (start + 2, bs))
        fi.nextfile()
        self.assertEqual(fi.readline(), 'Line 1 of file 3\n')
        self.assertEqual(fi.lineno(), 22)
        fi.close()

        if verbose:
            print('%s. Boundary conditions (bs=%s)' % (start + 4, bs))
        fi = FileInput(files=(t1, t2, t3, t4), bufsize=bs)
        self.assertEqual(fi.lineno(), 0)
        self.assertEqual(fi.filename(), None)
        fi.nextfile()
        self.assertEqual(fi.lineno(), 0)
        self.assertEqual(fi.filename(), None)

        if verbose:
            print('%s. Inplace (bs=%s)' % (start + 5, bs))
        savestdout = sys.stdout
        try:
            fi = FileInput(files=(t1, t2, t3, t4), inplace=1, bufsize=bs)
            for line in fi:
                line = line[:-1].upper()
                print(line)
            fi.close()
        finally:
            pass  ###

        fi = FileInput(files=(t1, t2, t3, t4), bufsize=bs)
        for line in fi:
            self.assertEqual(line[-1], '\n')
            m = pat.match(line[:-1])
            self.assertNotEqual(m, None)
            self.assertEqual(int(m.group(1)), fi.filelineno())
        fi.close()
Example #24
0
def runTests(t1, t2, t3, t4, bs=0, round=0):
    start = 1 + round*6
    if verbose:
        print '%s. Simple iteration (bs=%s)' % (start+0, bs)
    fi = FileInput(files=(t1, t2, t3, t4), bufsize=bs)
    lines = list(fi)
    fi.close()
    verify(len(lines) == 31)
    verify(lines[4] == 'Line 5 of file 1\n')
    verify(lines[30] == 'Line 1 of file 4\n')
    verify(fi.lineno() == 31)
    verify(fi.filename() == t4)

    if verbose:
        print '%s. Status variables (bs=%s)' % (start+1, bs)
    fi = FileInput(files=(t1, t2, t3, t4), bufsize=bs)
    s = "x"
    while s and s != 'Line 6 of file 2\n':
        s = fi.readline()
    verify(fi.filename() == t2)
    verify(fi.lineno() == 21)
    verify(fi.filelineno() == 6)
    verify(not fi.isfirstline())
    verify(not fi.isstdin())

    if verbose:
        print '%s. Nextfile (bs=%s)' % (start+2, bs)
    fi.nextfile()
    verify(fi.readline() == 'Line 1 of file 3\n')
    verify(fi.lineno() == 22)
    fi.close()

    if verbose:
        print '%s. Stdin (bs=%s)' % (start+3, bs)
    fi = FileInput(files=(t1, t2, t3, t4, '-'), bufsize=bs)
    savestdin = sys.stdin
    try:
        sys.stdin = StringIO("Line 1 of stdin\nLine 2 of stdin\n")
        lines = list(fi)
        verify(len(lines) == 33)
        verify(lines[32] == 'Line 2 of stdin\n')
        verify(fi.filename() == '<stdin>')
        fi.nextfile()
    finally:
        sys.stdin = savestdin

    if verbose:
        print '%s. Boundary conditions (bs=%s)' % (start+4, bs)
    fi = FileInput(files=(t1, t2, t3, t4), bufsize=bs)
    verify(fi.lineno() == 0)
    verify(fi.filename() == None)
    fi.nextfile()
    verify(fi.lineno() == 0)
    verify(fi.filename() == None)

    if verbose:
        print '%s. Inplace (bs=%s)' % (start+5, bs)
    savestdout = sys.stdout
    try:
        fi = FileInput(files=(t1, t2, t3, t4), inplace=1, bufsize=bs)
        for line in fi:
            line = line[:-1].upper()
            print line
        fi.close()
    finally:
        sys.stdout = savestdout

    fi = FileInput(files=(t1, t2, t3, t4), bufsize=bs)
    for line in fi:
        verify(line[-1] == '\n')
        m = pat.match(line[:-1])
        verify(m != None)
        verify(int(m.group(1)) == fi.filelineno())
    fi.close()