~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/knit.py

Utf8 safety in knit indexes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
777
777
                result.append(value[1:])
778
778
            else:
779
779
                assert isinstance(value, str)
780
 
                result.append(self._history[int(value)])
 
780
                try:
 
781
                    result.append(self._history[int(value)])
 
782
                except ValueError:
 
783
                    import pdb;pdb.set_trace()
781
784
        return result
782
785
 
783
786
    def get_graph(self):
853
856
        """Add a version record to the index."""
854
857
        self._cache_version(version_id, options, pos, size, parents)
855
858
 
856
 
        content = "%s %s %s %s %s\n" % (version_id,
 
859
        content = "%s %s %s %s %s\n" % (version_id.encode('utf-8'),
857
860
                                        ','.join(options),
858
861
                                        pos,
859
862
                                        size,
860
863
                                        self._version_list_to_index(parents))
 
864
        assert isinstance(content, str), 'content must be utf-8 encoded'
861
865
        self._transport.append(self._filename, StringIO(content))
862
866
 
863
867
    def has_version(self, version_id):
932
936
        """
933
937
        sio = StringIO()
934
938
        data_file = GzipFile(None, mode='wb', fileobj=sio)
935
 
        print >>data_file, "version %s %d %s" % (version_id, len(lines), digest)
 
939
        print >>data_file, "version %s %d %s" % (version_id.encode('utf-8'), len(lines), digest)
936
940
        data_file.writelines(lines)
937
 
        print >>data_file, "end %s\n" % version_id
 
941
        print >>data_file, "end %s\n" % version_id.encode('utf-8')
938
942
        data_file.close()
939
943
        length= sio.tell()
940
944
        sio.seek(0)
942
946
 
943
947
    def add_raw_record(self, raw_data):
944
948
        """Append a prepared record to the data file."""
 
949
        assert isinstance(raw_data, str), 'data must be plain bytes'
945
950
        start_pos = self._transport.append(self._filename, StringIO(raw_data))
946
951
        return start_pos, len(raw_data)
947
952
        
965
970
        rec = df.readline().split()
966
971
        if len(rec) != 4:
967
972
            raise KnitCorrupt(self._filename, 'unexpected number of elements in record header')
968
 
        if rec[1] != version_id:
 
973
        if rec[1].decode('utf-8')!= version_id:
969
974
            raise KnitCorrupt(self._filename, 
970
975
                              'unexpected version, wanted %r, got %r' % (
971
976
                                version_id, rec[1]))
976
981
        lines = int(rec[2])
977
982
        record_contents = self._read_record_contents(df, lines)
978
983
        l = df.readline()
979
 
        if l != 'end %s\n' % version_id:
 
984
        if l.decode('utf-8') != 'end %s\n' % version_id:
980
985
            raise KnitCorrupt(self._filename, 'unexpected version end line %r, wanted %r' 
981
986
                        % (l, version_id))
982
987
        df.close()