~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/errors.py

  • Committer: wang
  • Date: 2006-10-29 13:41:32 UTC
  • mto: (2104.4.1 wang_65714)
  • mto: This revision was merged to the branch mainline in revision 2109.
  • Revision ID: wang@ubuntu-20061029134132-3d7f4216f20c4aef
Replace python's difflib by patiencediff because the worst case 
performance is cubic for difflib and people commiting large data 
files are often hurt by this. The worst case performance of patience is 
quadratic. Fix bug 65714.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006 Canonical
 
1
# Copyright (C) 2005, 2006 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
44
44
>>> try:
45
45
...   raise NotBranchError(path='/foo/bar')
46
46
... except:
47
 
...   print sys.exc_type
 
47
...   print '%s.%s' % (sys.exc_type.__module__, sys.exc_type.__name__)
48
48
...   print sys.exc_value
49
49
...   path = getattr(sys.exc_value, 'path', None)
50
50
...   if path is not None:
96
96
class BzrError(StandardError):
97
97
    
98
98
    is_user_error = True
99
 
    
 
99
 
100
100
    def __str__(self):
101
101
        # XXX: Should we show the exception class in 
102
102
        # exceptions that don't provide their own message?  
120
120
    # base classes should override the docstring with their human-
121
121
    # readable explanation
122
122
 
123
 
    def __init__(self, **kwds):
 
123
    def __init__(self, *args, **kwds):
 
124
        # XXX: Use the underlying BzrError to always generate the args attribute
 
125
        # if it doesn't exist.  We can't use super here, because exceptions are
 
126
        # old-style classes in python2.4 (but new in 2.5).  --bmc, 20060426
 
127
        BzrError.__init__(self, *args)
124
128
        for key, value in kwds.items():
125
129
            setattr(self, key, value)
126
130
 
127
131
    def __str__(self):
128
132
        try:
129
 
            return self.__doc__ % self.__dict__
130
 
        except (NameError, ValueError, KeyError), e:
131
 
            return 'Unprintable exception %s: %s' \
132
 
                % (self.__class__.__name__, str(e))
 
133
            # __str__() should always return a 'str' object
 
134
            # never a 'unicode' object.
 
135
            s = self.__doc__ % self.__dict__
 
136
            if isinstance(s, unicode):
 
137
                return s.encode('utf8')
 
138
            return s
 
139
        except (TypeError, NameError, ValueError, KeyError), e:
 
140
            return 'Unprintable exception %s(%r): %s' \
 
141
                % (self.__class__.__name__,
 
142
                   self.__dict__, str(e))
 
143
 
 
144
 
 
145
class AlreadyBuilding(BzrNewError):
 
146
    """The tree builder is already building a tree."""
133
147
 
134
148
 
135
149
class BzrCheckError(BzrNewError):
161
175
 
162
176
class InvalidRevisionId(BzrNewError):
163
177
    """Invalid revision-id {%(revision_id)s} in %(branch)s"""
 
178
 
164
179
    def __init__(self, revision_id, branch):
165
180
        # branch can be any string or object with __str__ defined
166
181
        BzrNewError.__init__(self)
168
183
        self.branch = branch
169
184
 
170
185
 
 
186
class InventoryModified(BzrNewError):
 
187
    """The current inventory for the tree %(tree)r has been modified, so a clean inventory cannot be read without data loss."""
 
188
 
 
189
    def __init__(self, tree):
 
190
        BzrNewError.__init__(self)
 
191
        self.tree = tree
 
192
 
 
193
 
 
194
class NoSuchId(BzrNewError):
 
195
    """The file id %(file_id)s is not present in the tree %(tree)s."""
 
196
    
 
197
    def __init__(self, tree, file_id):
 
198
        BzrNewError.__init__(self)
 
199
        self.file_id = file_id
 
200
        self.tree = tree
 
201
 
 
202
 
171
203
class NoWorkingTree(BzrNewError):
172
204
    """No WorkingTree exists for %(base)s."""
173
205
    
176
208
        self.base = base
177
209
 
178
210
 
 
211
class NotBuilding(BzrNewError):
 
212
    """Not currently building a tree."""
 
213
 
 
214
 
179
215
class NotLocalUrl(BzrNewError):
180
216
    """%(url)s is not a local path."""
181
217
    
184
220
        self.url = url
185
221
 
186
222
 
 
223
class NotWriteLocked(BzrNewError):
 
224
    """%(not_locked)r is not write locked but needs to be."""
 
225
 
 
226
    def __init__(self, not_locked):
 
227
        BzrNewError.__init__(self)
 
228
        self.not_locked = not_locked
 
229
 
 
230
 
187
231
class BzrCommandError(BzrNewError):
188
232
    """Error from user command"""
189
233
 
197
241
    # BzrCommandError, and non-UI code should not throw a subclass of
198
242
    # BzrCommandError.  ADHB 20051211
199
243
    def __init__(self, msg):
200
 
        self.msg = msg
 
244
        # Object.__str__() must return a real string
 
245
        # returning a Unicode string is a python error.
 
246
        if isinstance(msg, unicode):
 
247
            self.msg = msg.encode('utf8')
 
248
        else:
 
249
            self.msg = msg
201
250
 
202
251
    def __str__(self):
203
252
        return self.msg
237
286
    """Directory not empty: %(path)r%(extra)s"""
238
287
 
239
288
 
 
289
class ReadingCompleted(BzrNewError):
 
290
    """The MediumRequest '%(request)s' has already had finish_reading called upon it - the request has been completed and no more data may be read."""
 
291
 
 
292
    is_user_error = False
 
293
 
 
294
    def __init__(self, request):
 
295
        BzrNewError.__init__(self)
 
296
        self.request = request
 
297
 
 
298
 
240
299
class ResourceBusy(PathError):
241
300
    """Device or resource busy: %(path)r%(extra)s"""
242
301
 
254
313
 
255
314
    def __init__(self, msg, base, args):
256
315
        PathError.__init__(self, base, msg)
257
 
        self.args = [base]
258
 
        self.args.extend(args)
 
316
        self.args = [base] + list(args)
259
317
 
260
318
 
261
319
class UnsupportedProtocol(PathError):
265
323
        PathError.__init__(self, url, extra=extra)
266
324
 
267
325
 
 
326
class ShortReadvError(PathError):
 
327
    """readv() read %(actual)s bytes rather than %(length)s bytes at %(offset)s for %(path)s%(extra)s"""
 
328
 
 
329
    is_user_error = False
 
330
 
 
331
    def __init__(self, path, offset, length, actual, extra=None):
 
332
        PathError.__init__(self, path, extra=extra)
 
333
        self.offset = offset
 
334
        self.length = length
 
335
        self.actual = actual
 
336
 
 
337
 
268
338
class PathNotChild(BzrNewError):
269
339
    """Path %(path)r is not a child of path %(base)r%(extra)s"""
270
340
 
354
424
        self.bzrdir = bzrdir_format
355
425
 
356
426
 
 
427
class IncompatibleRevision(BzrNewError):
 
428
    """Revision is not compatible with %(repo_format)s"""
 
429
 
 
430
    def __init__(self, repo_format):
 
431
        BzrNewError.__init__(self)
 
432
        self.repo_format = repo_format
 
433
 
 
434
 
357
435
class NotVersionedError(BzrNewError):
358
436
    """%(path)s is not versioned"""
359
437
    def __init__(self, path):
494
572
        self.format = format
495
573
 
496
574
 
497
 
 
498
575
class StrictCommitFailed(Exception):
499
576
    """Commit refused because there are unknowns in the tree."""
500
577
 
505
582
    is_user_error = False
506
583
 
507
584
    def __init__(self, branch, revision):
508
 
        self.branch = branch
509
 
        self.revision = revision
 
585
        BzrNewError.__init__(self, branch=branch, revision=revision)
 
586
 
 
587
 
 
588
class NoSuchRevisionSpec(BzrNewError):
 
589
    """No namespace registered for string: %(spec)r"""
 
590
 
 
591
    def __init__(self, spec):
 
592
        BzrNewError.__init__(self, spec=spec)
 
593
 
 
594
 
 
595
class InvalidRevisionSpec(BzrNewError):
 
596
    """Requested revision: '%(spec)s' does not exist in branch:
 
597
%(branch)s%(extra)s"""
 
598
 
 
599
    def __init__(self, spec, branch, extra=None):
 
600
        BzrNewError.__init__(self, branch=branch, spec=spec)
 
601
        if extra:
 
602
            self.extra = '\n' + str(extra)
 
603
        else:
 
604
            self.extra = ''
510
605
 
511
606
 
512
607
class HistoryMissing(BzrError):
575
670
        self.bases = bases
576
671
 
577
672
 
578
 
class NoCommits(BzrError):
 
673
class NoCommits(BzrNewError):
 
674
    """Branch %(branch)s has no commits."""
 
675
 
579
676
    def __init__(self, branch):
580
 
        msg = "Branch %s has no commits." % branch
581
 
        BzrError.__init__(self, msg)
 
677
        BzrNewError.__init__(self, branch=branch)
582
678
 
583
679
 
584
680
class UnlistableStore(BzrError):
732
828
 
733
829
class NoSuchExportFormat(BzrNewError):
734
830
    """Export format %(format)r not supported"""
 
831
 
735
832
    def __init__(self, format):
736
833
        BzrNewError.__init__(self)
737
834
        self.format = format
738
835
 
739
836
 
 
837
 
 
838
class TooManyConcurrentRequests(BzrNewError):
 
839
    """The medium '%(medium)s' has reached its concurrent request limit. Be sure to finish_writing and finish_reading on the current request that is open."""
 
840
 
 
841
    def __init__(self, medium):
 
842
        BzrNewError.__init__(self)
 
843
        self.medium = medium
 
844
 
 
845
 
740
846
class TransportError(BzrNewError):
741
847
    """Transport error: %(msg)s %(orig_error)s"""
742
848
 
752
858
        BzrNewError.__init__(self)
753
859
 
754
860
 
 
861
class SmartProtocolError(TransportError):
 
862
    """Generic bzr smart protocol error: %(details)s"""
 
863
 
 
864
    def __init__(self, details):
 
865
        self.details = details
 
866
 
 
867
 
755
868
# A set of semi-meaningful errors which can be thrown
756
869
class TransportNotPossible(TransportError):
757
 
    """Transport operation not possible: %(msg)s %(orig_error)%"""
 
870
    """Transport operation not possible: %(msg)s %(orig_error)s"""
758
871
 
759
872
 
760
873
class ConnectionError(TransportError):
766
879
 
767
880
 
768
881
class InvalidRange(TransportError):
769
 
    """Invalid range access."""
 
882
    """Invalid range access in %(path)s at %(offset)s."""
770
883
    
771
884
    def __init__(self, path, offset):
772
885
        TransportError.__init__(self, ("Invalid range access in %s at %d"
773
886
                                       % (path, offset)))
 
887
        self.path = path
 
888
        self.offset = offset
774
889
 
775
890
 
776
891
class InvalidHttpResponse(TransportError):
811
926
        BzrError.__init__(self, message)
812
927
 
813
928
 
 
929
class NoEmailInUsername(BzrNewError):
 
930
    """%(username)r does not seem to contain a reasonable email address"""
 
931
 
 
932
    def __init__(self, username):
 
933
        BzrNewError.__init__(self)
 
934
        self.username = username
 
935
 
 
936
 
814
937
class SigningFailed(BzrError):
815
938
    def __init__(self, command_line):
816
939
        BzrError.__init__(self, "Failed to gpg sign data with command '%s'"
824
947
                          " unchanged." % tree.basedir)
825
948
 
826
949
 
 
950
class WritingCompleted(BzrNewError):
 
951
    """The MediumRequest '%(request)s' has already had finish_writing called upon it - accept bytes may not be called anymore."""
 
952
 
 
953
    is_user_error = False
 
954
 
 
955
    def __init__(self, request):
 
956
        BzrNewError.__init__(self)
 
957
        self.request = request
 
958
 
 
959
 
 
960
class WritingNotComplete(BzrNewError):
 
961
    """The MediumRequest '%(request)s' has not has finish_writing called upon it - until the write phase is complete no data may be read."""
 
962
 
 
963
    is_user_error = False
 
964
 
 
965
    def __init__(self, request):
 
966
        BzrNewError.__init__(self)
 
967
        self.request = request
 
968
 
 
969
 
827
970
class CantReprocessAndShowBase(BzrNewError):
828
971
    """Can't reprocess and show base.
829
972
Reprocessing obscures relationship of conflicting lines to base."""
844
987
        self.filename = filename
845
988
 
846
989
 
 
990
class MediumNotConnected(BzrNewError):
 
991
    """The medium '%(medium)s' is not connected."""
 
992
 
 
993
    def __init__(self, medium):
 
994
        BzrNewError.__init__(self)
 
995
        self.medium = medium
 
996
 
 
997
 
847
998
class MustUseDecorated(Exception):
848
999
    """A decorating function has requested its original command be used.
849
1000
    
885
1036
    """Tree transform is malformed %(conflicts)r"""
886
1037
 
887
1038
 
 
1039
class NoFinalPath(BzrNewError):
 
1040
    """No final name for trans_id %(trans_id)r
 
1041
    file-id: %(file_id)r"
 
1042
    root trans-id: %(root_trans_id)r 
 
1043
    """
 
1044
 
 
1045
    def __init__(self, trans_id, transform):
 
1046
        self.trans_id = trans_id
 
1047
        self.file_id = transform.final_file_id(trans_id)
 
1048
        self.root_trans_id = transform.root
 
1049
 
 
1050
 
888
1051
class BzrBadParameter(BzrNewError):
889
1052
    """A bad parameter : %(param)s is not usable.
890
1053
    
938
1101
        DependencyNotPresent.__init__(self, 'paramiko', error)
939
1102
 
940
1103
 
 
1104
class PointlessMerge(BzrNewError):
 
1105
    """Nothing to merge."""
 
1106
 
 
1107
 
941
1108
class UninitializableFormat(BzrNewError):
942
1109
    """Format %(format)s cannot be initialised by this version of bzr."""
943
1110
 
946
1113
        self.format = format
947
1114
 
948
1115
 
 
1116
class BadConversionTarget(BzrNewError):
 
1117
    """Cannot convert to format %(format)s.  %(problem)s"""
 
1118
 
 
1119
    def __init__(self, problem, format):
 
1120
        BzrNewError.__init__(self)
 
1121
        self.problem = problem
 
1122
        self.format = format
 
1123
 
 
1124
 
949
1125
class NoDiff(BzrNewError):
950
1126
    """Diff is not installed on this machine: %(msg)s"""
951
1127
 
1096
1272
        BzrNewError.__init__(self)
1097
1273
        self.text = text
1098
1274
 
 
1275
 
1099
1276
class UnsupportedEOLMarker(BadBundle):
1100
1277
    """End of line marker was not \\n in bzr revision-bundle"""    
1101
1278
 
1102
1279
    def __init__(self):
1103
 
        BzrNewError.__init__(self)    
 
1280
        BzrNewError.__init__(self)
 
1281
 
 
1282
 
 
1283
class IncompatibleFormat(BzrNewError):
 
1284
    """Bundle format %(bundle_format)s is incompatible with %(other)s"""
 
1285
 
 
1286
    def __init__(self, bundle_format, other):
 
1287
        BzrNewError.__init__(self)
 
1288
        self.bundle_format = bundle_format
 
1289
        self.other = other
 
1290
 
 
1291
 
 
1292
class BadInventoryFormat(BzrNewError):
 
1293
    """Root class for inventory serialization errors"""
 
1294
 
 
1295
 
 
1296
class UnexpectedInventoryFormat(BadInventoryFormat):
 
1297
    """The inventory was not in the expected format:\n %(msg)s"""
 
1298
 
 
1299
    def __init__(self, msg):
 
1300
        BadInventoryFormat.__init__(self, msg=msg)
 
1301
 
 
1302
 
 
1303
class NoSmartMedium(BzrNewError):
 
1304
    """The transport '%(transport)s' cannot tunnel the smart protocol."""
 
1305
 
 
1306
    def __init__(self, transport):
 
1307
        BzrNewError.__init__(self)
 
1308
        self.transport = transport
 
1309
 
 
1310
 
 
1311
class NoSmartServer(NotBranchError):
 
1312
    """No smart server available at %(url)s"""
 
1313
 
 
1314
    def __init__(self, url):
 
1315
        self.url = url
 
1316
 
 
1317
 
 
1318
class UnknownSSH(BzrNewError):
 
1319
    """Unrecognised value for BZR_SSH environment variable: %(vendor)s"""
 
1320
 
 
1321
    def __init__(self, vendor):
 
1322
        BzrNewError.__init__(self)
 
1323
        self.vendor = vendor
 
1324
 
 
1325
 
 
1326
class GhostRevisionUnusableHere(BzrNewError):
 
1327
    """Ghost revision {%(revision_id)s} cannot be used here."""
 
1328
 
 
1329
    def __init__(self, revision_id):
 
1330
        BzrNewError.__init__(self)
 
1331
        self.revision_id = revision_id
 
1332
 
 
1333
 
 
1334
class IllegalUseOfScopeReplacer(BzrNewError):
 
1335
    """ScopeReplacer object %(name)r was used incorrectly: %(msg)s%(extra)s"""
 
1336
 
 
1337
    is_user_error = False
 
1338
 
 
1339
    def __init__(self, name, msg, extra=None):
 
1340
        BzrNewError.__init__(self)
 
1341
        self.name = name
 
1342
        self.msg = msg
 
1343
        if extra:
 
1344
            self.extra = ': ' + str(extra)
 
1345
        else:
 
1346
            self.extra = ''
 
1347
 
 
1348
 
 
1349
class InvalidImportLine(BzrNewError):
 
1350
    """Not a valid import statement: %(msg)\n%(text)s"""
 
1351
 
 
1352
    is_user_error = False
 
1353
 
 
1354
    def __init__(self, text, msg):
 
1355
        BzrNewError.__init__(self)
 
1356
        self.text = text
 
1357
        self.msg = msg
 
1358
 
 
1359
 
 
1360
class ImportNameCollision(BzrNewError):
 
1361
    """Tried to import an object to the same name as an existing object. %(name)s"""
 
1362
 
 
1363
    is_user_error = False
 
1364
 
 
1365
    def __init__(self, name):
 
1366
        BzrNewError.__init__(self)
 
1367
        self.name = name