hg
author Chris Jerdonek <chris.jerdonek@gmail.com>
Wed, 04 Dec 2013 20:38:27 -0800
changeset 20742 3681de20b0a7
parent 14233 659f34b833b9
child 21812 73e4a02e6d23
permissions -rwxr-xr-x
parsers: fail fast if Python has wrong minor version (issue4110) This change causes an informative ImportError to be raised when importing the parsers extension module if the minor version of the currently-running Python interpreter doesn't match that of the Python used when compiling the extension module. This change also exposes a parsers.versionerrortext constant in the C implementation of the module. Its presence can be used to determine whether this behavior is present in a version of the module. The value of the constant is the leading text of the ImportError raised and is set to "Python minor version mismatch". Here is an example of what the new error looks like: Traceback (most recent call last): File "test.py", line 1, in <module> import mercurial.parsers ImportError: Python minor version mismatch: The Mercurial extension modules were compiled with Python 2.7.6, but Mercurial is currently using Python with sys.hexversion=33883888: Python 2.5.6 (r256:88840, Nov 18 2012, 05:37:10) [GCC 4.2.1 Compatible Apple Clang 4.1 ((tags/Apple/clang-421.11.66))] at: /opt/local/Library/Frameworks/Python.framework/Versions/2.5/Resources/ Python.app/Contents/MacOS/Python The reason for raising an error in this scenario is that Python's C API is known not to be compatible from minor version to minor version, even if sys.api_version is the same. See for example this Python bug report about incompatibilities between 2.5 and 2.6+: http://bugs.python.org/issue8118 These incompatibilities can cause Mercurial to break in mysterious, unforeseen ways. For example, when Mercurial compiled with Python 2.7 was run with 2.5, the following crash occurred when running "hg status": http://bz.selenic.com/show_bug.cgi?id=4110 After this crash was fixed, running with Python 2.5 no longer crashes, but the following puzzling behavior still occurs: $ hg status ... File ".../mercurial/changelog.py", line 123, in __init__ revlog.revlog.__init__(self, opener, "00changelog.i") File ".../mercurial/revlog.py", line 251, in __init__ d = self._io.parseindex(i, self._inline) File ".../mercurial/revlog.py", line 158, in parseindex index, cache = parsers.parse_index2(data, inline) TypeError: data is not a string which can be reproduced more simply with: import mercurial.parsers as parsers parsers.parse_index2("", True) Both the crash and the TypeError occurred because the Python C API's PyString_Check() returns the wrong value when the C header files from Python 2.7 are run with Python 2.5. This is an example of an incompatibility of the sort mentioned in the Python bug report above. Failing fast with an informative error message results in a better user experience in cases like the above. The information in the ImportError also simplifies troubleshooting for those on Mercurial mailing lists, the bug tracker, etc. This patch only adds the version check to parsers.c, which is sufficient to affect command-line commands like "hg status" and "hg summary". An idea for a future improvement is to move the version-checking C code to a more central location, and have it run when importing all Mercurial extension modules and not just parsers.c.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
     1
#!/usr/bin/env python
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
     2
#
1698
ad4a2eefe4d7 Update copyright notice
Matt Mackall <mpm@selenic.com>
parents: 515
diff changeset
     3
# mercurial - scalable distributed SCM
0
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
     4
#
4635
63b9d2deed48 Updated copyright notices and add "and others" to "hg version"
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3877
diff changeset
     5
# Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
0
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
     6
#
8225
46293a0c7e9f updated license to be explicit about GPL version 2
Martin Geisler <mg@lazybytes.net>
parents: 7672
diff changeset
     7
# This software may be used and distributed according to the terms of the
10263
25e572394f5c Update license to GPLv2+
Matt Mackall <mpm@selenic.com>
parents: 8225
diff changeset
     8
# GNU General Public License version 2 or any later version.
0
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
     9
12661
10da5a1f25dd setup/hg: always load Mercurial from where it was installed.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 10263
diff changeset
    10
import os
10da5a1f25dd setup/hg: always load Mercurial from where it was installed.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 10263
diff changeset
    11
import sys
10da5a1f25dd setup/hg: always load Mercurial from where it was installed.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 10263
diff changeset
    12
10da5a1f25dd setup/hg: always load Mercurial from where it was installed.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 10263
diff changeset
    13
libdir = '@LIBDIR@'
10da5a1f25dd setup/hg: always load Mercurial from where it was installed.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 10263
diff changeset
    14
10da5a1f25dd setup/hg: always load Mercurial from where it was installed.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 10263
diff changeset
    15
if libdir != '@' 'LIBDIR' '@':
10da5a1f25dd setup/hg: always load Mercurial from where it was installed.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 10263
diff changeset
    16
    if not os.path.isabs(libdir):
12805
cae1c187abd4 setup/hg: handle hg being a symlink when appending relative libdir to sys.path
L. David Baron <dbaron@dbaron.org>
parents: 12661
diff changeset
    17
        libdir = os.path.join(os.path.dirname(os.path.realpath(__file__)),
cae1c187abd4 setup/hg: handle hg being a symlink when appending relative libdir to sys.path
L. David Baron <dbaron@dbaron.org>
parents: 12661
diff changeset
    18
                              libdir)
12661
10da5a1f25dd setup/hg: always load Mercurial from where it was installed.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 10263
diff changeset
    19
        libdir = os.path.abspath(libdir)
10da5a1f25dd setup/hg: always load Mercurial from where it was installed.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 10263
diff changeset
    20
    sys.path.insert(0, libdir)
10da5a1f25dd setup/hg: always load Mercurial from where it was installed.
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 10263
diff changeset
    21
5197
55860a45bbf2 Enable demandimport only in scripts, not in importable modules (issue605)
Thomas Arendsen Hein <thomas@intevation.de>
parents: 5178
diff changeset
    22
# enable importing on demand to reduce startup time
7672
523c7816c33a Give a useful message about PYTHONPATH if startup fails
Matt Mackall <mpm@selenic.com>
parents: 5531
diff changeset
    23
try:
523c7816c33a Give a useful message about PYTHONPATH if startup fails
Matt Mackall <mpm@selenic.com>
parents: 5531
diff changeset
    24
    from mercurial import demandimport; demandimport.enable()
523c7816c33a Give a useful message about PYTHONPATH if startup fails
Matt Mackall <mpm@selenic.com>
parents: 5531
diff changeset
    25
except ImportError:
523c7816c33a Give a useful message about PYTHONPATH if startup fails
Matt Mackall <mpm@selenic.com>
parents: 5531
diff changeset
    26
    import sys
523c7816c33a Give a useful message about PYTHONPATH if startup fails
Matt Mackall <mpm@selenic.com>
parents: 5531
diff changeset
    27
    sys.stderr.write("abort: couldn't find mercurial libraries in [%s]\n" %
523c7816c33a Give a useful message about PYTHONPATH if startup fails
Matt Mackall <mpm@selenic.com>
parents: 5531
diff changeset
    28
                     ' '.join(sys.path))
523c7816c33a Give a useful message about PYTHONPATH if startup fails
Matt Mackall <mpm@selenic.com>
parents: 5531
diff changeset
    29
    sys.stderr.write("(check your install and PYTHONPATH)\n")
523c7816c33a Give a useful message about PYTHONPATH if startup fails
Matt Mackall <mpm@selenic.com>
parents: 5531
diff changeset
    30
    sys.exit(-1)
5197
55860a45bbf2 Enable demandimport only in scripts, not in importable modules (issue605)
Thomas Arendsen Hein <thomas@intevation.de>
parents: 5178
diff changeset
    31
5531
a3fe91b4f6eb Change standard streams mode to binary at hg startup
Patrick Mezard <pmezard@gmail.com>
parents: 5197
diff changeset
    32
import mercurial.util
5178
18a9fbb5cd78 dispatch: move command dispatching into its own module
Matt Mackall <mpm@selenic.com>
parents: 4635
diff changeset
    33
import mercurial.dispatch
5531
a3fe91b4f6eb Change standard streams mode to binary at hg startup
Patrick Mezard <pmezard@gmail.com>
parents: 5197
diff changeset
    34
a3fe91b4f6eb Change standard streams mode to binary at hg startup
Patrick Mezard <pmezard@gmail.com>
parents: 5197
diff changeset
    35
for fp in (sys.stdin, sys.stdout, sys.stderr):
14233
659f34b833b9 rename util.set_binary to setbinary
Adrian Buehlmann <adrian@cadifra.com>
parents: 12805
diff changeset
    36
    mercurial.util.setbinary(fp)
5531
a3fe91b4f6eb Change standard streams mode to binary at hg startup
Patrick Mezard <pmezard@gmail.com>
parents: 5197
diff changeset
    37
5178
18a9fbb5cd78 dispatch: move command dispatching into its own module
Matt Mackall <mpm@selenic.com>
parents: 4635
diff changeset
    38
mercurial.dispatch.run()