diff --git a/hg b/hg --- a/hg +++ b/hg @@ -26,20 +26,12 @@ libdir = os.path.abspath(libdir) sys.path.insert(0, libdir) -# enable importing on demand to reduce startup time try: - if sys.version_info[0] < 3 or sys.version_info >= (3, 6): - import hgdemandimport; hgdemandimport.enable() + import mercurial.dispatch except ImportError: sys.stderr.write("abort: couldn't find mercurial libraries in [%s]\n" % ' '.join(sys.path)) sys.stderr.write("(check your install and PYTHONPATH)\n") sys.exit(-1) -import mercurial.util -import mercurial.dispatch - -for fp in (sys.stdin, sys.stdout, sys.stderr): - mercurial.util.setbinary(fp) - mercurial.dispatch.run() diff --git a/mercurial/dispatch.py b/mercurial/dispatch.py --- a/mercurial/dispatch.py +++ b/mercurial/dispatch.py @@ -75,6 +75,8 @@ def run(): "run the command in sys.argv" + _enabledemandimport() + _setbinary() req = request(pycompat.sysargv[1:]) err = None try: @@ -92,6 +94,16 @@ req.ui.ferr.flush() sys.exit(status & 255) +def _enabledemandimport(): + """enable importing on demand to reduce startup time""" + if sys.version_info[0] < 3 or sys.version_info >= (3, 6): + import hgdemandimport; hgdemandimport.enable() + +def _setbinary(): + """use binary mode for standard streams""" + for fp in (sys.stdin, sys.stdout, sys.stderr): + util.setbinary(fp) + def _getsimilar(symbols, value): sim = lambda x: difflib.SequenceMatcher(None, value, x).ratio() # The cutoff for similarity here is pretty arbitrary. It should