comparison mercurial/dispatch.py @ 25660:328739ea70c3

global: mass rewrite to use modern exception syntax Python 2.6 introduced the "except type as instance" syntax, replacing the "except type, instance" syntax that came before. Python 3 dropped support for the latter syntax. Since we no longer support Python 2.4 or 2.5, we have no need to continue supporting the "except type, instance". This patch mass rewrites the exception syntax to be Python 2.6+ and Python 3 compatible. This patch was produced by running `2to3 -f except -w -n .`.
author Gregory Szorc <gregory.szorc@gmail.com>
date Tue, 23 Jun 2015 22:20:08 -0700
parents 101e84121c13
children 8243e999f22b
comparison
equal deleted inserted replaced
25659:d60678a567a9 25660:328739ea70c3
74 req.ui.fin = req.fin 74 req.ui.fin = req.fin
75 if req.fout: 75 if req.fout:
76 req.ui.fout = req.fout 76 req.ui.fout = req.fout
77 if req.ferr: 77 if req.ferr:
78 req.ui.ferr = req.ferr 78 req.ui.ferr = req.ferr
79 except util.Abort, inst: 79 except util.Abort as inst:
80 ferr.write(_("abort: %s\n") % inst) 80 ferr.write(_("abort: %s\n") % inst)
81 if inst.hint: 81 if inst.hint:
82 ferr.write(_("(%s)\n") % inst.hint) 82 ferr.write(_("(%s)\n") % inst.hint)
83 return -1 83 return -1
84 except error.ParseError, inst: 84 except error.ParseError as inst:
85 _formatparse(ferr.write, inst) 85 _formatparse(ferr.write, inst)
86 return -1 86 return -1
87 87
88 msg = ' '.join(' ' in a and repr(a) or a for a in req.args) 88 msg = ' '.join(' ' in a and repr(a) or a for a in req.args)
89 starttime = time.time() 89 starttime = time.time()
170 ui.traceback() 170 ui.traceback()
171 raise 171 raise
172 172
173 # Global exception handling, alphabetically 173 # Global exception handling, alphabetically
174 # Mercurial-specific first, followed by built-in and library exceptions 174 # Mercurial-specific first, followed by built-in and library exceptions
175 except error.AmbiguousCommand, inst: 175 except error.AmbiguousCommand as inst:
176 ui.warn(_("hg: command '%s' is ambiguous:\n %s\n") % 176 ui.warn(_("hg: command '%s' is ambiguous:\n %s\n") %
177 (inst.args[0], " ".join(inst.args[1]))) 177 (inst.args[0], " ".join(inst.args[1])))
178 except error.ParseError, inst: 178 except error.ParseError as inst:
179 _formatparse(ui.warn, inst) 179 _formatparse(ui.warn, inst)
180 return -1 180 return -1
181 except error.LockHeld, inst: 181 except error.LockHeld as inst:
182 if inst.errno == errno.ETIMEDOUT: 182 if inst.errno == errno.ETIMEDOUT:
183 reason = _('timed out waiting for lock held by %s') % inst.locker 183 reason = _('timed out waiting for lock held by %s') % inst.locker
184 else: 184 else:
185 reason = _('lock held by %s') % inst.locker 185 reason = _('lock held by %s') % inst.locker
186 ui.warn(_("abort: %s: %s\n") % (inst.desc or inst.filename, reason)) 186 ui.warn(_("abort: %s: %s\n") % (inst.desc or inst.filename, reason))
187 except error.LockUnavailable, inst: 187 except error.LockUnavailable as inst:
188 ui.warn(_("abort: could not lock %s: %s\n") % 188 ui.warn(_("abort: could not lock %s: %s\n") %
189 (inst.desc or inst.filename, inst.strerror)) 189 (inst.desc or inst.filename, inst.strerror))
190 except error.CommandError, inst: 190 except error.CommandError as inst:
191 if inst.args[0]: 191 if inst.args[0]:
192 ui.warn(_("hg %s: %s\n") % (inst.args[0], inst.args[1])) 192 ui.warn(_("hg %s: %s\n") % (inst.args[0], inst.args[1]))
193 commands.help_(ui, inst.args[0], full=False, command=True) 193 commands.help_(ui, inst.args[0], full=False, command=True)
194 else: 194 else:
195 ui.warn(_("hg: %s\n") % inst.args[1]) 195 ui.warn(_("hg: %s\n") % inst.args[1])
196 commands.help_(ui, 'shortlist') 196 commands.help_(ui, 'shortlist')
197 except error.OutOfBandError, inst: 197 except error.OutOfBandError as inst:
198 if inst.args: 198 if inst.args:
199 msg = _("abort: remote error:\n") 199 msg = _("abort: remote error:\n")
200 else: 200 else:
201 msg = _("abort: remote error\n") 201 msg = _("abort: remote error\n")
202 ui.warn(msg) 202 ui.warn(msg)
203 if inst.args: 203 if inst.args:
204 ui.warn(''.join(inst.args)) 204 ui.warn(''.join(inst.args))
205 if inst.hint: 205 if inst.hint:
206 ui.warn('(%s)\n' % inst.hint) 206 ui.warn('(%s)\n' % inst.hint)
207 except error.RepoError, inst: 207 except error.RepoError as inst:
208 ui.warn(_("abort: %s!\n") % inst) 208 ui.warn(_("abort: %s!\n") % inst)
209 if inst.hint: 209 if inst.hint:
210 ui.warn(_("(%s)\n") % inst.hint) 210 ui.warn(_("(%s)\n") % inst.hint)
211 except error.ResponseError, inst: 211 except error.ResponseError as inst:
212 ui.warn(_("abort: %s") % inst.args[0]) 212 ui.warn(_("abort: %s") % inst.args[0])
213 if not isinstance(inst.args[1], basestring): 213 if not isinstance(inst.args[1], basestring):
214 ui.warn(" %r\n" % (inst.args[1],)) 214 ui.warn(" %r\n" % (inst.args[1],))
215 elif not inst.args[1]: 215 elif not inst.args[1]:
216 ui.warn(_(" empty string\n")) 216 ui.warn(_(" empty string\n"))
217 else: 217 else:
218 ui.warn("\n%r\n" % util.ellipsis(inst.args[1])) 218 ui.warn("\n%r\n" % util.ellipsis(inst.args[1]))
219 except error.CensoredNodeError, inst: 219 except error.CensoredNodeError as inst:
220 ui.warn(_("abort: file censored %s!\n") % inst) 220 ui.warn(_("abort: file censored %s!\n") % inst)
221 except error.RevlogError, inst: 221 except error.RevlogError as inst:
222 ui.warn(_("abort: %s!\n") % inst) 222 ui.warn(_("abort: %s!\n") % inst)
223 except error.SignalInterrupt: 223 except error.SignalInterrupt:
224 ui.warn(_("killed!\n")) 224 ui.warn(_("killed!\n"))
225 except error.UnknownCommand, inst: 225 except error.UnknownCommand as inst:
226 ui.warn(_("hg: unknown command '%s'\n") % inst.args[0]) 226 ui.warn(_("hg: unknown command '%s'\n") % inst.args[0])
227 try: 227 try:
228 # check if the command is in a disabled extension 228 # check if the command is in a disabled extension
229 # (but don't check for extensions themselves) 229 # (but don't check for extensions themselves)
230 commands.help_(ui, inst.args[0], unknowncmd=True) 230 commands.help_(ui, inst.args[0], unknowncmd=True)
236 ui.warn(_('(did you mean one of %s?)\n') % 236 ui.warn(_('(did you mean one of %s?)\n') %
237 ', '.join(sorted(sim))) 237 ', '.join(sorted(sim)))
238 suggested = True 238 suggested = True
239 if not suggested: 239 if not suggested:
240 commands.help_(ui, 'shortlist') 240 commands.help_(ui, 'shortlist')
241 except error.InterventionRequired, inst: 241 except error.InterventionRequired as inst:
242 ui.warn("%s\n" % inst) 242 ui.warn("%s\n" % inst)
243 return 1 243 return 1
244 except util.Abort, inst: 244 except util.Abort as inst:
245 ui.warn(_("abort: %s\n") % inst) 245 ui.warn(_("abort: %s\n") % inst)
246 if inst.hint: 246 if inst.hint:
247 ui.warn(_("(%s)\n") % inst.hint) 247 ui.warn(_("(%s)\n") % inst.hint)
248 except ImportError, inst: 248 except ImportError as inst:
249 ui.warn(_("abort: %s!\n") % inst) 249 ui.warn(_("abort: %s!\n") % inst)
250 m = str(inst).split()[-1] 250 m = str(inst).split()[-1]
251 if m in "mpatch bdiff".split(): 251 if m in "mpatch bdiff".split():
252 ui.warn(_("(did you forget to compile extensions?)\n")) 252 ui.warn(_("(did you forget to compile extensions?)\n"))
253 elif m in "zlib".split(): 253 elif m in "zlib".split():
254 ui.warn(_("(is your Python install correct?)\n")) 254 ui.warn(_("(is your Python install correct?)\n"))
255 except IOError, inst: 255 except IOError as inst:
256 if util.safehasattr(inst, "code"): 256 if util.safehasattr(inst, "code"):
257 ui.warn(_("abort: %s\n") % inst) 257 ui.warn(_("abort: %s\n") % inst)
258 elif util.safehasattr(inst, "reason"): 258 elif util.safehasattr(inst, "reason"):
259 try: # usually it is in the form (errno, strerror) 259 try: # usually it is in the form (errno, strerror)
260 reason = inst.reason.args[1] 260 reason = inst.reason.args[1]
274 ui.warn(_("abort: %s: %s\n") % (inst.strerror, inst.filename)) 274 ui.warn(_("abort: %s: %s\n") % (inst.strerror, inst.filename))
275 else: 275 else:
276 ui.warn(_("abort: %s\n") % inst.strerror) 276 ui.warn(_("abort: %s\n") % inst.strerror)
277 else: 277 else:
278 raise 278 raise
279 except OSError, inst: 279 except OSError as inst:
280 if getattr(inst, "filename", None) is not None: 280 if getattr(inst, "filename", None) is not None:
281 ui.warn(_("abort: %s: '%s'\n") % (inst.strerror, inst.filename)) 281 ui.warn(_("abort: %s: '%s'\n") % (inst.strerror, inst.filename))
282 else: 282 else:
283 ui.warn(_("abort: %s\n") % inst.strerror) 283 ui.warn(_("abort: %s\n") % inst.strerror)
284 except KeyboardInterrupt: 284 except KeyboardInterrupt:
285 try: 285 try:
286 ui.warn(_("interrupted!\n")) 286 ui.warn(_("interrupted!\n"))
287 except IOError, inst: 287 except IOError as inst:
288 if inst.errno == errno.EPIPE: 288 if inst.errno == errno.EPIPE:
289 if ui.debugflag: 289 if ui.debugflag:
290 ui.warn(_("\nbroken pipe\n")) 290 ui.warn(_("\nbroken pipe\n"))
291 else: 291 else:
292 raise 292 raise
293 except MemoryError: 293 except MemoryError:
294 ui.warn(_("abort: out of memory\n")) 294 ui.warn(_("abort: out of memory\n"))
295 except SystemExit, inst: 295 except SystemExit as inst:
296 # Commands shouldn't sys.exit directly, but give a return code. 296 # Commands shouldn't sys.exit directly, but give a return code.
297 # Just in case catch this and and pass exit code to caller. 297 # Just in case catch this and and pass exit code to caller.
298 return inst.code 298 return inst.code
299 except socket.error, inst: 299 except socket.error as inst:
300 ui.warn(_("abort: %s\n") % inst.args[-1]) 300 ui.warn(_("abort: %s\n") % inst.args[-1])
301 except: # re-raises 301 except: # re-raises
302 myver = util.version() 302 myver = util.version()
303 # For compatibility checking, we discard the portion of the hg 303 # For compatibility checking, we discard the portion of the hg
304 # version after the + on the assumption that if a "normal 304 # version after the + on the assumption that if a "normal
450 self.fn = fn 450 self.fn = fn
451 return 451 return
452 452
453 try: 453 try:
454 args = shlex.split(self.definition) 454 args = shlex.split(self.definition)
455 except ValueError, inst: 455 except ValueError as inst:
456 self.badalias = (_("error in definition for alias '%s': %s") 456 self.badalias = (_("error in definition for alias '%s': %s")
457 % (self.name, inst)) 457 % (self.name, inst))
458 return 458 return
459 self.cmdname = cmd = args.pop(0) 459 self.cmdname = cmd = args.pop(0)
460 args = map(util.expandpath, args) 460 args = map(util.expandpath, args)
541 options = {} 541 options = {}
542 cmdoptions = {} 542 cmdoptions = {}
543 543
544 try: 544 try:
545 args = fancyopts.fancyopts(args, commands.globalopts, options) 545 args = fancyopts.fancyopts(args, commands.globalopts, options)
546 except fancyopts.getopt.GetoptError, inst: 546 except fancyopts.getopt.GetoptError as inst:
547 raise error.CommandError(None, inst) 547 raise error.CommandError(None, inst)
548 548
549 if args: 549 if args:
550 cmd, args = args[0], args[1:] 550 cmd, args = args[0], args[1:]
551 aliases, entry = cmdutil.findcmd(cmd, commands.table, 551 aliases, entry = cmdutil.findcmd(cmd, commands.table,
564 for o in commands.globalopts: 564 for o in commands.globalopts:
565 c.append((o[0], o[1], options[o[1]], o[3])) 565 c.append((o[0], o[1], options[o[1]], o[3]))
566 566
567 try: 567 try:
568 args = fancyopts.fancyopts(args, c, cmdoptions, True) 568 args = fancyopts.fancyopts(args, c, cmdoptions, True)
569 except fancyopts.getopt.GetoptError, inst: 569 except fancyopts.getopt.GetoptError as inst:
570 raise error.CommandError(cmd, inst) 570 raise error.CommandError(cmd, inst)
571 571
572 # separate global options back out 572 # separate global options back out
573 for o in commands.globalopts: 573 for o in commands.globalopts:
574 n = o[1] 574 n = o[1]
663 663
664 Takes paths in [cwd]/.hg/hgrc into account." 664 Takes paths in [cwd]/.hg/hgrc into account."
665 """ 665 """
666 try: 666 try:
667 wd = os.getcwd() 667 wd = os.getcwd()
668 except OSError, e: 668 except OSError as e:
669 raise util.Abort(_("error getting current working directory: %s") % 669 raise util.Abort(_("error getting current working directory: %s") %
670 e.strerror) 670 e.strerror)
671 path = cmdutil.findrepo(wd) or "" 671 path = cmdutil.findrepo(wd) or ""
672 if not path: 672 if not path:
673 lui = ui 673 lui = ui