'Read a string from the given stream that does not contain any
un-closed tags.  In particular, this function first reads a
block from the stream of size ``self._BLOCK_SIZE``.  It then
checks if that block contains an un-closed tag.  If it does,
then this function either backtracks to the last \'<\', or reads
another block.'

def _read_xml_fragment(self, stream):

    fragment = u''
    if isinstance(stream, SeekableUnicodeStreamReader):
        startpos = stream.tell()
    while True:
        xml_block = stream.read(self._BLOCK_SIZE)
        fragment += xml_block
        if self._VALID_XML_RE.match(fragment):
            return fragment
        if (re.search(u'[<>]', fragment).group(0) == u'>'):
            pos = (stream.tell() - (len(fragment) - re.search(u'[<>]', fragment).end()))
            raise ValueError((u'Unexpected    ">"    near    char    %s' % pos))
        if (not xml_block):
            raise ValueError(u'Unexpected    end    of    file:    tag    not    closed')
        last_open_bracket = fragment.rfind(u'<')
        if (last_open_bracket > 0):
            if self._VALID_XML_RE.match(fragment[:last_open_bracket]):
                if isinstance(stream, SeekableUnicodeStreamReader):
                    stream.seek(startpos)
                    stream.char_seek_forward(last_open_bracket)
                else:
                    stream.seek((- (len(fragment) - last_open_bracket)), 1)
                return fragment[:last_open_bracket]
