Esempio n. 1
0
 def test_wrong_term(self):
     s = StringIO('3.1. term2.')
     s.name = 'terms'
     s.fileno = lambda: 999
     stream = prologio.Stream(s)
     old_current_input_stream = prologio.current_input_stream
     prologio.current_input_stream = stream
     self.assertFalse(self.engine.solve('read(4.1).'))
     self.assertEquals('term2.', s.read())
     prologio.current_input_stream = old_current_input_stream
Esempio n. 2
0
def test_null_fileno():
    """Make sure ``Terminal`` works when ``fileno`` is ``None``.

    This simulates piping output to another program.

    """
    out = StringIO()
    out.fileno = None
    t = TestTerminal(stream=out)
    eq_(t.save, '')
Esempio n. 3
0
 def test_term_from_stream(self):
     s = StringIO('term1. term2.')
     s.name = 'terms'
     s.fileno = lambda: 999
     stream = prologio.Stream(s)
     prologio.stream_terms[str(stream.stream_term)] = stream
     # FIXME Single quotes are not preserved in atoms
     self.assertTrue(self.engine.solve("read('$stream'(999), term1)."))
     self.assertEquals('term2.', s.read())
     del prologio.stream_terms[str(stream.stream_term)]
Esempio n. 4
0
def test_null_fileno():
    """Make sure ``Terminal`` works when ``fileno`` is ``None``.

    This simulates piping output to another program.

    """
    out = StringIO()
    out.fileno = None
    t = TestTerminal(stream=out)
    eq_(t.save, '')
Esempio n. 5
0
 def test_quoted_atom_with_quotes(self):
     s = StringIO()
     s.name = 'quoted_atom'
     s.fileno = lambda: 999
     stream = prologio.Stream(s, mode='w')
     prologio.stream_terms[str(stream.stream_term)] = stream
     # FIXME Single quotes are not preserved in atoms
     self.assertTrue(self.engine.solve("writeq('$stream'(999), '1<2')."))
     s.seek(0)
     self.assertEquals("'1<2'", s.read())
     del prologio.stream_terms[str(stream.stream_term)]
Esempio n. 6
0
 def test_list(self):
     s = StringIO()
     s.name = 'list'
     s.fileno = lambda: 999
     stream = prologio.Stream(s, mode='w')
     prologio.stream_terms[str(stream.stream_term)] = stream
     # FIXME Single quotes are not preserved in atoms
     self.assertTrue(self.engine.solve("write('$stream'(999), [1,2,3])."))
     s.seek(0)
     self.assertEquals('[1,2,3]', s.read())
     del prologio.stream_terms[str(stream.stream_term)]
Esempio n. 7
0
 def test_term_from_current_input_stream(self):
     s = StringIO('term1. term2.')
     s.name = 'terms'
     s.fileno = lambda: 999
     stream = prologio.Stream(s)
     old_current_input_stream = prologio.current_input_stream
     prologio.current_input_stream = stream
     self.assertTrue(self.engine.solve('read(T).'))
     self.assertEquals(Atomic('term1'), self.engine.currsubst()['T'])
     self.assertEquals('term2.', s.read())
     prologio.current_input_stream = old_current_input_stream
Esempio n. 8
0
 def test_term_without_end_token(self):
     s = StringIO('3.1')
     s.name = 'terms'
     s.fileno = lambda: 999
     stream = prologio.Stream(s)
     old_current_input_stream = prologio.current_input_stream
     prologio.current_input_stream = stream
     goal = "read(T)."
     caught = self.assertRaises(PrologError, self.engine.solve, goal)
     self.assertEquals('syntax_error', caught.error_term().value[0])
     self.assertEquals('', s.read())
     prologio.current_input_stream = old_current_input_stream
Esempio n. 9
0
 def test_stream_code(self):
     s = StringIO('qwer')
     s.seek(4)
     s.name = 'qwer'
     s.fileno = lambda: 999
     stream = prologio.Stream(s, mode='w')
     prologio.stream_terms[str(stream.stream_term)] = stream
     # FIXME Single quotes are not preserved in atoms
     self.assertTrue(self.engine.solve("put_code('$stream'(999), 0't)."))
     s.seek(0)
     self.assertEquals('qwert', s.read())
     del prologio.stream_terms[str(stream.stream_term)]
Esempio n. 10
0
 def test_current_output_stream_code(self):
     s = StringIO('qwer')
     s.seek(4)
     s.name = 'qwer'
     s.fileno = lambda: 999
     stream = prologio.Stream(s, mode='w')
     old_current_output_stream = prologio.current_output_stream
     prologio.current_output_stream = stream
     self.assertTrue(self.engine.solve("put_code(0't)."))
     s.seek(0)
     self.assertEquals('qwert', s.read())
     prologio.current_output_stream = old_current_output_stream
Esempio n. 11
0
 def test_current_output_stream_newline_and_char(self):
     s = StringIO('qwer')
     s.seek(4)
     s.name = 'qwer'
     s.fileno = lambda: 999
     stream = prologio.Stream(s, mode='w')
     old_current_output_stream = prologio.current_output_stream
     prologio.current_output_stream = stream
     self.assertTrue(self.engine.solve('nl, put_char(a).'))
     s.seek(0)
     self.assertEquals('qwer\na', s.read())
     prologio.current_output_stream = old_current_output_stream
Esempio n. 12
0
 def test_stream_newline_and_char(self):
     s = StringIO('qwer')
     s.seek(4)
     s.name = 'qwer'
     s.fileno = lambda: 999
     stream = prologio.Stream(s, mode='w')
     prologio.stream_terms[str(stream.stream_term)] = stream
     # FIXME Single quotes are not preserved in atoms
     goal = "nl('$stream'(999)), put_char('$stream'(999), a)."
     self.assertTrue(self.engine.solve(goal))
     s.seek(0)
     self.assertEquals('qwer\na', s.read())
     del prologio.stream_terms[str(stream.stream_term)]
Esempio n. 13
0
    def test_constructor_bad_file(self, mmap_file):
        non_file = StringIO("I am not a file")
        non_file.fileno = lambda: -1

        # the error raised is different on Windows
        if is_platform_windows():
            msg = "The parameter is incorrect"
            err = OSError
        else:
            msg = "[Errno 22]"
            err = mmap.error

        with pytest.raises(err, match=msg):
            icom._MMapWrapper(non_file)

        target = open(mmap_file)
        target.close()

        msg = "I/O operation on closed file"
        with pytest.raises(ValueError, match=msg):
            icom._MMapWrapper(target)
Esempio n. 14
0
    def test_constructor_bad_file(self, mmap_file):
        non_file = StringIO('I am not a file')
        non_file.fileno = lambda: -1

        # the error raised is different on Windows
        if is_platform_windows():
            msg = "The parameter is incorrect"
            err = OSError
        else:
            msg = "[Errno 22]"
            err = mmap.error

        with pytest.raises(err, match=msg):
            icom.MMapWrapper(non_file)

        target = open(mmap_file, 'r')
        target.close()

        msg = "I/O operation on closed file"
        with pytest.raises(ValueError, match=msg):
            icom.MMapWrapper(target)
Esempio n. 15
0
class TeeInput(object):

    CHUNK_SIZE = conn.CHUNK_SIZE

    def __init__(self, stream):
        self.buf = StringIO()
        self.eof = False

        if isinstance(stream, str):
            stream = StringIO(stream)
            self.tmp = StringIO()
        else:
            self.tmp = tempfile.TemporaryFile()

        self.stream = stream

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_val, traceback):
        return

    def seek(self, offset, whence=0):
        """ naive implementation of seek """
        current_size = self._tmp_size()
        diff = 0
        if whence == 0:
            diff = offset - current_size
        elif whence == 2:
            diff = (self.tmp.tell() + offset) - current_size
        elif whence == 3 and not self.eof:
            # we read until the end
            while True:
                self.tmp.seek(0, 2)
                if not self._tee(self.CHUNK_SIZE):
                    break

        if not self.eof and diff > 0:
            self._ensure_length(StringIO(), diff)
        self.tmp.seek(offset, whence)

    def flush(self):
        self.tmp.flush()

    def read(self, length=-1):
        """ read """
        if self.eof:
            return self.tmp.read(length)

        if length < 0:
            buf = StringIO()
            buf.write(self.tmp.read())
            while True:
                chunk = self._tee(self.CHUNK_SIZE)
                if not chunk:
                    break
                buf.write(chunk)
            return buf.getvalue()
        else:
            dest = StringIO()
            diff = self._tmp_size() - self.tmp.tell()
            if not diff:
                dest.write(self._tee(length))
                return self._ensure_length(dest, length)
            else:
                l = min(diff, length)
                dest.write(self.tmp.read(l))
                return self._ensure_length(dest, length)

    def readline(self, size=-1):
        if self.eof:
            return self.tmp.readline()

        orig_size = self._tmp_size()
        if self.tmp.tell() == orig_size:
            if not self._tee(self.CHUNK_SIZE):
                return ''
            self.tmp.seek(orig_size)

        # now we can get line
        line = self.tmp.readline()
        if line.find("\n") >= 0:
            return line

        buf = StringIO()
        buf.write(line)
        while True:
            orig_size = self.tmp.tell()
            data = self._tee(self.CHUNK_SIZE)
            if not data:
                break
            self.tmp.seek(orig_size)
            buf.write(self.tmp.readline())
            if data.find("\n") >= 0:
                break
        return buf.getvalue()

    def readlines(self, sizehint=0):
        total = 0
        lines = []
        line = self.readline()
        while line:
            lines.append(line)
            total += len(line)
            if 0 < sizehint <= total:
                break
            line = self.readline()
        return lines

    def close(self):
        if not self.eof:
            # we didn't read until the end
            self._close_unreader()
        return self.tmp.close()

    def __next__(self):
        r = self.readline()
        if not r:
            raise StopIteration
        return r

    __next__ = next

    def __iter__(self):
        return self

    def _tee(self, length):
        """ fetch partial body"""
        buf2 = self.buf
        buf2.seek(0, 2)
        chunk = self.stream.read(length)
        if chunk:
            self.tmp.write(chunk)
            self.tmp.flush()
            self.tmp.seek(0, 2)
            return chunk

        self._finalize()
        return ""

    def _finalize(self):
        """ here we wil fetch final trailers
        if any."""
        self.eof = True

    def _tmp_size(self):
        if hasattr(self.tmp, 'fileno'):
            return int(os.fstat(self.tmp.fileno())[6])
        else:
            return len(self.tmp.getvalue())

    def _ensure_length(self, dest, length):
        if len(dest.getvalue()) < length:
            data = self._tee(length - len(dest.getvalue()))
            dest.write(data)
        return dest.getvalue()