Mercurial > public > mercurial-scm > hg
comparison mercurial/hook.py @ 18111:d7c28954d901
hook: disable demandimport before importing hooks
This solved an obscure bug for me. In upgrading Distribute on the server, a
patch was added to has a try: import a except ImportError thing that's only
supposed to work with Python 3.3. I'm using 2.7. My hook failed with an
ImportError because of this. It seems kind of sensible to turn off
demandimport before importing the hook, since the except ImportError pattern
is used quite a bit in Python code (including in other Distribute code).
author | Dirkjan Ochtman <dirkjan@ochtman.nl> |
---|---|
date | Thu, 20 Dec 2012 21:26:30 +0100 |
parents | 2c63896783e3 |
children | 1c305128e5b9 |
comparison
equal
deleted
inserted
replaced
18110:acfc6fab1361 | 18111:d7c28954d901 |
---|---|
5 # This software may be used and distributed according to the terms of the | 5 # This software may be used and distributed according to the terms of the |
6 # GNU General Public License version 2 or any later version. | 6 # GNU General Public License version 2 or any later version. |
7 | 7 |
8 from i18n import _ | 8 from i18n import _ |
9 import os, sys | 9 import os, sys |
10 import extensions, util | 10 import extensions, util, demandimport |
11 | 11 |
12 def _pythonhook(ui, repo, name, hname, funcname, args, throw): | 12 def _pythonhook(ui, repo, name, hname, funcname, args, throw): |
13 '''call python hook. hook is callable object, looked up as | 13 '''call python hook. hook is callable object, looked up as |
14 name in python module. if callable returns "true", hook | 14 name in python module. if callable returns "true", hook |
15 fails, else passes. if hook raises exception, treated as | 15 fails, else passes. if hook raises exception, treated as |
33 modpath, modfile = os.path.split(modname) | 33 modpath, modfile = os.path.split(modname) |
34 if modpath and modfile: | 34 if modpath and modfile: |
35 sys.path = sys.path[:] + [modpath] | 35 sys.path = sys.path[:] + [modpath] |
36 modname = modfile | 36 modname = modfile |
37 try: | 37 try: |
38 demandimport.disable() | |
38 obj = __import__(modname) | 39 obj = __import__(modname) |
40 demandimport.enable() | |
39 except ImportError: | 41 except ImportError: |
40 e1 = sys.exc_type, sys.exc_value, sys.exc_traceback | 42 e1 = sys.exc_type, sys.exc_value, sys.exc_traceback |
41 try: | 43 try: |
42 # extensions are loaded with hgext_ prefix | 44 # extensions are loaded with hgext_ prefix |
43 obj = __import__("hgext_%s" % modname) | 45 obj = __import__("hgext_%s" % modname) |
46 demandimport.enable() | |
44 except ImportError: | 47 except ImportError: |
48 demandimport.enable() | |
45 e2 = sys.exc_type, sys.exc_value, sys.exc_traceback | 49 e2 = sys.exc_type, sys.exc_value, sys.exc_traceback |
46 if ui.tracebackflag: | 50 if ui.tracebackflag: |
47 ui.warn(_('exception from first failed import attempt:\n')) | 51 ui.warn(_('exception from first failed import attempt:\n')) |
48 ui.traceback(e1) | 52 ui.traceback(e1) |
49 if ui.tracebackflag: | 53 if ui.tracebackflag: |