Beispiel #1
0
 def test_matryoshka(self):
     obj = cStringIO()
     ns = util.NamedStream(obj, 'r')
     with pytest.warns(RuntimeWarning):
         ns2 = util.NamedStream(ns, 'f')
     assert not isinstance(ns2.stream, util.NamedStream)
     assert ns2.name == 'f'
Beispiel #2
0
 def test_closing_force(self):
     obj = cStringIO("".join(self.text))
     ns = util.NamedStream(obj, self.textname)
     assert_equal(ns.closed, False)
     ns.close()
     assert_equal(ns.closed, False)
     ns.close(force=True)
     assert_equal(ns.closed, True)
Beispiel #3
0
 def test_named_stream(self):
     ns = util.NamedStream(StringIO(), self.filename)
     fn = util.filename(ns, ext=self.ext)
     # assert_equal replace by this if loop to avoid segfault on some systems
     if fn != ns:
         pytest.fail("fn and ns are different")
     assert str(fn) == self.filename2
     assert ns.name == self.filename2
Beispiel #4
0
 def testNamedStream(self):
     ns = util.NamedStream(StringIO(), self.filename)
     fn = util.filename(ns, ext=self.ext)
     # assert_equal replace by this if loop to avoid segfault on some systems
     if fn != ns:
         raise AssertionError("fn and ns are different")
     assert_equal(str(fn), self.filename2)
     assert_equal(ns.name, self.filename2)
Beispiel #5
0
 def test_File_read(self):
     obj = open(self.filename, 'r')
     ns = util.NamedStream(obj, self.filename)
     assert_equal(ns.name, self.filename)
     assert_equal(str(ns), self.filename)
     assert_equal(len(ns.readlines()), self.numlines)
     ns.reset()
     assert_equal(len(ns.readlines()), self.numlines)
     ns.close(force=True)
Beispiel #6
0
 def test_cStringIO_read(self):
     obj = cStringIO("".join(self.text))
     ns = util.NamedStream(obj, self.textname)
     assert_equal(ns.name, self.textname)
     assert_equal(str(ns), self.textname)
     assert_equal(len(ns.readlines()), len(self.text))
     ns.reset()
     assert_equal(len(ns.readlines()), len(self.text))
     ns.close(force=True)
Beispiel #7
0
 def testcStringIO_write(self):
     obj = cStringIO.StringIO()
     ns = util.NamedStream(obj, self.textname)
     ns.writelines(self.text)
     assert_equal(ns.name, self.textname)
     assert_equal(str(ns), self.textname)
     ns.reset()
     assert_equal(len(ns.readlines()), len(self.text))
     ns.reset()
     assert_equal(ns.read(20), "".join(self.text)[:20])
     ns.close(force=True)
Beispiel #8
0
    def test_File_write(self):
        with tempdir.in_tempdir():
            outfile = "lookingglas.txt"
            with open(outfile, "w") as obj:
                with util.NamedStream(obj, outfile, close=True) as ns:
                    assert_equal(ns.name, outfile)
                    assert_equal(str(ns), outfile)
                    ns.writelines(self.text)

            with open(outfile) as fh:
                text = fh.readlines()

            assert_equal(len(text), len(self.text))
            assert_equal("".join(text), "".join(self.text))
    def test_File_write(self):
        with tempdir.in_tempdir():
            outfile = "lookingglas.txt"
            try:
                obj = open(outfile, "w")
                ns = util.NamedStream(obj, outfile, close=True)
                ns.writelines(self.text)
                ns.close()
                text = open(outfile).readlines()

                assert_equal(ns.name, outfile)
                assert_equal(str(ns), outfile)
                assert_equal(len(text), len(self.text))
                assert_equal("".join(text), "".join(self.text))
            finally:
                ns.close()
                obj.close()
Beispiel #10
0
    def testFile_write(self):
        fd, outfile = tempfile.mkstemp(suffix=".txt")
        os.close(fd)
        try:
            obj = open(outfile, "w")
            ns = util.NamedStream(obj, outfile, close=True)
            ns.writelines(self.text)
            ns.close()
            text = open(outfile).readlines()

            assert_equal(ns.name, outfile)
            assert_equal(str(ns), outfile)
            assert_equal(len(text), len(self.text))
            assert_equal("".join(text), "".join(self.text))
        finally:
            ns.close()
            obj.close()
            try:
                os.unlink(outfile)
            except OSError:
                pass
Beispiel #11
0
 def as_NamedStream(self, name):
     return util.NamedStream(self.as_cStringIO(name), self.filenames[name])
Beispiel #12
0
 def create_NamedStream(self, name=None):
     if name is None:
         name = self.textname
     obj = cStringIO()
     return util.NamedStream(obj, name)