# HG changeset patch # User Matt Mackall # Date 2009-01-12 04:48:28 # Node ID 08cabecfa8a89f14b17dd5b68ef297f9ab6f653a # Parent 9626819b2e3df2d575f9b2269ea1f2fe96247e9f errors: move revlog errors - create error.py for exception classes to reduce demandloading - move revlog exceptions to it - change users to import error and drop revlog import if possible diff --git a/hgext/convert/hg.py b/hgext/convert/hg.py --- a/hgext/convert/hg.py +++ b/hgext/convert/hg.py @@ -17,7 +17,7 @@ import os, time from mercurial.i18n import _ from mercurial.repo import RepoError from mercurial.node import bin, hex, nullid -from mercurial import hg, revlog, util, context +from mercurial import hg, util, context, error from common import NoRepo, commit, converter_source, converter_sink @@ -244,7 +244,7 @@ class mercurial_source(converter_source) def getfile(self, name, rev): try: return self.changectx(rev)[name].data() - except revlog.LookupError, err: + except error.LookupError, err: raise IOError(err) def getmode(self, name, rev): @@ -283,7 +283,7 @@ class mercurial_source(converter_source) copies[name] = copysource except TypeError: pass - except revlog.LookupError, e: + except error.LookupError, e: if not self.ignoreerrors: raise self.ignored[name] = 1 diff --git a/hgext/imerge.py b/hgext/imerge.py --- a/hgext/imerge.py +++ b/hgext/imerge.py @@ -8,7 +8,7 @@ imerge - interactive merge from mercurial.i18n import _ from mercurial.node import hex, short from mercurial import commands, cmdutil, dispatch, fancyopts -from mercurial import hg, filemerge, util, revlog +from mercurial import hg, filemerge, util, error import os, tarfile class InvalidStateFileException(Exception): pass @@ -78,7 +78,7 @@ class Imerge(object): try: parents = [self.repo.changectx(n) for n in status[:2]] - except revlog.LookupError, e: + except error.LookupError, e: raise util.Abort(_('merge parent %s not in repository') % short(e.name)) diff --git a/hgext/transplant.py b/hgext/transplant.py --- a/hgext/transplant.py +++ b/hgext/transplant.py @@ -16,7 +16,7 @@ from a changeset hash to its hash in the from mercurial.i18n import _ import os, tempfile from mercurial import bundlerepo, changegroup, cmdutil, hg, merge -from mercurial import patch, revlog, util +from mercurial import patch, revlog, util, error class transplantentry: def __init__(self, lnode, rnode): @@ -380,7 +380,7 @@ class transplanter: def hasnode(repo, node): try: return repo.changelog.rev(node) != None - except revlog.RevlogError: + except error.RevlogError: return False def browserevs(ui, repo, nodes, opts): diff --git a/mercurial/bundlerepo.py b/mercurial/bundlerepo.py --- a/mercurial/bundlerepo.py +++ b/mercurial/bundlerepo.py @@ -13,7 +13,7 @@ of the GNU General Public License, incor from node import hex, nullid, short from i18n import _ import changegroup, util, os, struct, bz2, zlib, tempfile, shutil, mdiff -import repo, localrepo, changelog, manifest, filelog, revlog, context +import repo, localrepo, changelog, manifest, filelog, revlog, context, error class bundlerevlog(revlog.revlog): def __init__(self, opener, indexfile, bundlefile, @@ -48,8 +48,8 @@ class bundlerevlog(revlog.revlog): continue for p in (p1, p2): if not p in self.nodemap: - raise revlog.LookupError(p1, self.indexfile, - _("unknown parent")) + raise error.LookupError(p1, self.indexfile, + _("unknown parent")) if linkmapper is None: link = n else: @@ -119,7 +119,7 @@ class bundlerevlog(revlog.revlog): p1, p2 = self.parents(node) if node != revlog.hash(text, p1, p2): - raise revlog.RevlogError(_("integrity check failed on %s:%d") + raise error.RevlogError(_("integrity check failed on %s:%d") % (self.datafile, self.rev(node))) self._cache = (node, self.rev(node), text) diff --git a/mercurial/changelog.py b/mercurial/changelog.py --- a/mercurial/changelog.py +++ b/mercurial/changelog.py @@ -8,7 +8,7 @@ from node import bin, hex, nullid from revlog import revlog, RevlogError from i18n import _ -import util +import util, error def _string_escape(text): """ @@ -179,7 +179,8 @@ class changelog(revlog): user = user.strip() if "\n" in user: - raise RevlogError(_("username %s contains a newline") % repr(user)) + raise error.RevlogError(_("username %s contains a newline") + % repr(user)) user, desc = util.fromlocal(user), util.fromlocal(desc) if date: diff --git a/mercurial/commands.py b/mercurial/commands.py --- a/mercurial/commands.py +++ b/mercurial/commands.py @@ -9,7 +9,7 @@ from node import hex, nullid, nullrev, s from repo import RepoError, NoCapability from i18n import _, gettext import os, re, sys -import hg, util, revlog, bundlerepo, extensions, copies, context +import hg, util, revlog, bundlerepo, extensions, copies, context, error import difflib, patch, time, help, mdiff, tempfile, url import archival, changegroup, cmdutil, hgweb.server, sshserver, hbisect import merge as merge_ @@ -1214,7 +1214,7 @@ def grep(ui, repo, pattern, *pats, **opt copied = getfile(fn).renamed(ctx.filenode(fn)) if copied: copies.setdefault(rev, {})[fn] = copied[0] - except revlog.LookupError: + except error.LookupError: pass elif st == 'iter': for fn, m in util.sort(matches[rev].items()): @@ -1887,7 +1887,7 @@ def log(ui, repo, *pats, **opts): try: return repo[rev][fn].renamed() - except revlog.LookupError: + except error.LookupError: pass return None @@ -2086,7 +2086,7 @@ def parents(ui, repo, file_=None, **opts continue try: filenodes.append(cp.filenode(file_)) - except revlog.LookupError: + except error.LookupError: pass if not filenodes: raise util.Abort(_("'%s' not found in manifest!") % file_) @@ -2857,7 +2857,7 @@ def tags(ui, repo): try: hn = hexfunc(n) r = "%5d:%s" % (repo.changelog.rev(n), hn) - except revlog.LookupError: + except error.LookupError: r = " ?:%s" % hn else: spaces = " " * (30 - util.locallen(t)) diff --git a/mercurial/context.py b/mercurial/context.py --- a/mercurial/context.py +++ b/mercurial/context.py @@ -7,7 +7,7 @@ from node import nullid, nullrev, short, hex from i18n import _ -import ancestor, bdiff, revlog, util, os, errno +import ancestor, bdiff, error, util, os, errno class propertycache(object): def __init__(self, func): @@ -125,15 +125,15 @@ class changectx(object): try: return self._manifest[path], self._manifest.flags(path) except KeyError: - raise revlog.LookupError(self._node, path, - _('not found in manifest')) + raise error.LookupError(self._node, path, + _('not found in manifest')) if '_manifestdelta' in self.__dict__ or path in self.files(): if path in self._manifestdelta: return self._manifestdelta[path], self._manifestdelta.flags(path) node, flag = self._repo.manifest.find(self._changeset[0], path) if not node: - raise revlog.LookupError(self._node, path, - _('not found in manifest')) + raise error.LookupError(self._node, path, + _('not found in manifest')) return node, flag @@ -143,7 +143,7 @@ class changectx(object): def flags(self, path): try: return self._fileinfo(path)[1] - except revlog.LookupError: + except error.LookupError: return '' def filectx(self, path, fileid=None, filelog=None): @@ -235,7 +235,7 @@ class filectx(object): try: n = self._filenode return True - except revlog.LookupError: + except error.LookupError: # file is missing return False @@ -316,7 +316,7 @@ class filectx(object): try: if fnode == p.filenode(name): return None - except revlog.LookupError: + except error.LookupError: pass return renamed diff --git a/mercurial/dispatch.py b/mercurial/dispatch.py --- a/mercurial/dispatch.py +++ b/mercurial/dispatch.py @@ -8,7 +8,7 @@ from i18n import _ from repo import RepoError import os, sys, atexit, signal, pdb, socket, errno, shlex, time -import util, commands, hg, lock, fancyopts, revlog, extensions, hook +import util, commands, hg, lock, fancyopts, extensions, hook, error import cmdutil import ui as _ui @@ -76,7 +76,7 @@ def _runcatch(ui, args): except lock.LockUnavailable, inst: ui.warn(_("abort: could not lock %s: %s\n") % (inst.desc or inst.filename, inst.strerror)) - except revlog.RevlogError, inst: + except error.RevlogError, inst: ui.warn(_("abort: %s!\n") % inst) except util.SignalInterrupt: ui.warn(_("killed!\n")) diff --git a/mercurial/error.py b/mercurial/error.py new file mode 100644 --- /dev/null +++ b/mercurial/error.py @@ -0,0 +1,26 @@ +""" +error.py - Mercurial exceptions + +This allows us to catch exceptions at higher levels without forcing imports + +Copyright 2005-2008 Matt Mackall + +This software may be used and distributed according to the terms +of the GNU General Public License, incorporated herein by reference. +""" + +# Do not import anything here, please + +class RevlogError(Exception): + pass + +class LookupError(RevlogError, KeyError): + def __init__(self, name, index, message): + self.name = name + if isinstance(name, str) and len(name) == 20: + from node import short + name = short(name) + RevlogError.__init__(self, '%s@%s: %s' % (index, name, message)) + + def __str__(self): + return RevlogError.__str__(self) diff --git a/mercurial/hgweb/hgweb_mod.py b/mercurial/hgweb/hgweb_mod.py --- a/mercurial/hgweb/hgweb_mod.py +++ b/mercurial/hgweb/hgweb_mod.py @@ -9,8 +9,8 @@ import os, mimetypes from mercurial.node import hex, nullid from mercurial.repo import RepoError -from mercurial import ui, hg, util, hook -from mercurial import revlog, templater, templatefilters +from mercurial import ui, hg, util, hook, error +from mercurial import templater, templatefilters from common import get_mtime, style_map, ErrorResponse from common import HTTP_OK, HTTP_BAD_REQUEST, HTTP_NOT_FOUND, HTTP_SERVER_ERROR from common import HTTP_UNAUTHORIZED, HTTP_METHOD_NOT_ALLOWED @@ -185,13 +185,13 @@ class hgweb(object): return content - except revlog.LookupError, err: + except error.LookupError, err: req.respond(HTTP_NOT_FOUND, ctype) msg = str(err) if 'manifest' not in msg: msg = 'revision not found: %s' % err.name return tmpl('error', error=msg) - except (RepoError, revlog.RevlogError), inst: + except (RepoError, error.RevlogError), inst: req.respond(HTTP_SERVER_ERROR, ctype) return tmpl('error', error=str(inst)) except ErrorResponse, inst: diff --git a/mercurial/hgweb/webcommands.py b/mercurial/hgweb/webcommands.py --- a/mercurial/hgweb/webcommands.py +++ b/mercurial/hgweb/webcommands.py @@ -7,7 +7,7 @@ import os, mimetypes, re, cgi, copy import webutil -from mercurial import revlog, archival, templatefilters +from mercurial import error, archival, templatefilters from mercurial.node import short, hex, nullid from mercurial.util import binary, datestr from mercurial.repo import RepoError @@ -39,7 +39,7 @@ def rawfile(web, req, tmpl): try: fctx = webutil.filectx(web.repo, req) - except revlog.LookupError, inst: + except error.LookupError, inst: try: content = manifest(web, req, tmpl) req.respond(HTTP_OK, web.ctype) @@ -93,7 +93,7 @@ def file(web, req, tmpl): return manifest(web, req, tmpl) try: return _filerevision(web, tmpl, webutil.filectx(web.repo, req)) - except revlog.LookupError, inst: + except error.LookupError, inst: try: return manifest(web, req, tmpl) except ErrorResponse: @@ -521,7 +521,7 @@ def filelog(web, req, tmpl): fctx = webutil.filectx(web.repo, req) f = fctx.path() fl = fctx.filelog() - except revlog.LookupError: + except error.LookupError: f = webutil.cleanpath(web.repo, req.form['file'][0]) fl = web.repo.file(f) numrevs = len(fl) diff --git a/mercurial/localrepo.py b/mercurial/localrepo.py --- a/mercurial/localrepo.py +++ b/mercurial/localrepo.py @@ -10,7 +10,7 @@ from i18n import _ import repo, changegroup import changelog, dirstate, filelog, manifest, context, weakref import lock, transaction, stat, errno, ui, store -import os, revlog, time, util, extensions, hook, inspect +import os, time, util, extensions, hook, inspect, error import match as match_ import merge as merge_ @@ -177,7 +177,7 @@ class localrepository(repo.repository): else: try: prevtags = self.filectx('.hgtags', parent).data() - except revlog.LookupError: + except error.LookupError: pass fp = self.wfile('.hgtags', 'wb') if prevtags: @@ -332,7 +332,7 @@ class localrepository(repo.repository): rev = c.rev() try: fnode = c.filenode('.hgtags') - except revlog.LookupError: + except error.LookupError: continue ret.append((rev, node, fnode)) if fnode in last: diff --git a/mercurial/manifest.py b/mercurial/manifest.py --- a/mercurial/manifest.py +++ b/mercurial/manifest.py @@ -6,9 +6,9 @@ # of the GNU General Public License, incorporated herein by reference. from node import bin, hex, nullid -from revlog import revlog, RevlogError +from revlog import revlog from i18n import _ -import array, struct, mdiff, parsers, util +import array, struct, mdiff, parsers, util, error class manifestdict(dict): def __init__(self, mapping=None, flags=None): @@ -125,7 +125,8 @@ class manifest(revlog): def checkforbidden(l): for f in l: if '\n' in f or '\r' in f: - raise RevlogError(_("'\\n' and '\\r' disallowed in filenames")) + raise error.RevlogError( + _("'\\n' and '\\r' disallowed in filenames")) # if we're using the listcache, make sure it is valid and # parented by the same node we're diffing against diff --git a/mercurial/revlog.py b/mercurial/revlog.py --- a/mercurial/revlog.py +++ b/mercurial/revlog.py @@ -13,7 +13,7 @@ of the GNU General Public License, incor from node import bin, hex, nullid, nullrev, short from i18n import _ import changegroup, errno, ancestor, mdiff, parsers -import struct, util, zlib +import struct, util, zlib, error _pack = struct.pack _unpack = struct.unpack @@ -29,18 +29,8 @@ REVLOG_DEFAULT_FLAGS = REVLOGNGINLINEDAT REVLOG_DEFAULT_FORMAT = REVLOGNG REVLOG_DEFAULT_VERSION = REVLOG_DEFAULT_FORMAT | REVLOG_DEFAULT_FLAGS -class RevlogError(Exception): - pass - -class LookupError(RevlogError, KeyError): - def __init__(self, name, index, message): - self.name = name - if isinstance(name, str) and len(name) == 20: - name = short(name) - RevlogError.__init__(self, _('%s@%s: %s') % (index, name, message)) - - def __str__(self): - return RevlogError.__str__(self) +RevlogError = error.RevlogError +LookupError = error.LookupError def getoffset(q): return int(q >> 16)