comparison tests/test-parseindex2.py @ 20742:3681de20b0a7

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.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Wed, 04 Dec 2013 20:38:27 -0800
parents 7eda5bb9ec8f
children b502138f5faa
comparison
equal deleted inserted replaced
20741:f1dfef0a9352 20742:3681de20b0a7
1 """This unit test tests parsers.parse_index2().""" 1 """This unit test primarily tests parsers.parse_index2().
2
3 It also checks certain aspects of the parsers module as a whole.
4 """
2 5
3 from mercurial import parsers 6 from mercurial import parsers
4 from mercurial.node import nullid, nullrev 7 from mercurial.node import nullid, nullrev
5 import struct 8 import struct
9 import subprocess
10 import sys
6 11
7 # original python implementation 12 # original python implementation
8 def gettype(q): 13 def gettype(q):
9 return int(q & 0xFFFF) 14 return int(q & 0xFFFF)
10 15
93 98
94 def parse_index2(data, inline): 99 def parse_index2(data, inline):
95 index, chunkcache = parsers.parse_index2(data, inline) 100 index, chunkcache = parsers.parse_index2(data, inline)
96 return list(index), chunkcache 101 return list(index), chunkcache
97 102
103 def importparsers(hexversion):
104 """Import mercurial.parsers with the given sys.hexversion."""
105 # The file parsers.c inspects sys.hexversion to determine the version
106 # of the currently-running Python interpreter, so we monkey-patch
107 # sys.hexversion to simulate using different versions.
108 code = ("import sys; sys.hexversion=%s; "
109 "import mercurial.parsers" % hexversion)
110 cmd = "python -c \"%s\"" % code
111 # We need to do these tests inside a subprocess because parser.c's
112 # version-checking code happens inside the module init function, and
113 # when using reload() to reimport an extension module, "The init function
114 # of extension modules is not called a second time"
115 # (from http://docs.python.org/2/library/functions.html?#reload).
116 p = subprocess.Popen(cmd, shell=True,
117 stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
118 return p.communicate() # returns stdout, stderr
119
120 def printhexfail(testnumber, hexversion, stdout, expected):
121 try:
122 hexstring = hex(hexversion)
123 except TypeError:
124 hexstring = None
125 print ("FAILED: version test #%s with Python %s and patched "
126 "sys.hexversion %r (%r):\n Expected %s but got:\n-->'%s'\n" %
127 (testnumber, sys.version_info, hexversion, hexstring, expected,
128 stdout))
129
130 def testversionokay(testnumber, hexversion):
131 stdout, stderr = importparsers(hexversion)
132 if stdout:
133 printhexfail(testnumber, hexversion, stdout, expected="no stdout")
134
135 def testversionfail(testnumber, hexversion):
136 stdout, stderr = importparsers(hexversion)
137 # We include versionerrortext to distinguish from other ImportErrors.
138 errtext = "ImportError: %s" % parsers.versionerrortext
139 if errtext not in stdout:
140 printhexfail(testnumber, hexversion, stdout,
141 expected="stdout to contain %r" % errtext)
142
143 def makehex(major, minor, micro):
144 return int("%x%02x%02x00" % (major, minor, micro), 16)
145
146 def runversiontests():
147 """Check the version-detection logic when importing parsers."""
148 info = sys.version_info
149 major, minor, micro = info[0], info[1], info[2]
150 # Test same major-minor versions.
151 testversionokay(1, makehex(major, minor, micro))
152 testversionokay(2, makehex(major, minor, micro + 1))
153 # Test different major-minor versions.
154 testversionfail(3, makehex(major + 1, minor, micro))
155 testversionfail(4, makehex(major, minor + 1, micro))
156 testversionfail(5, "'foo'")
157
98 def runtest() : 158 def runtest() :
159 # Only test the version-detection logic if it is present.
160 try:
161 parsers.versionerrortext
162 except AttributeError:
163 pass
164 else:
165 runversiontests()
166
99 # Check that parse_index2() raises TypeError on bad arguments. 167 # Check that parse_index2() raises TypeError on bad arguments.
100 try: 168 try:
101 parse_index2(0, True) 169 parse_index2(0, True)
102 except TypeError: 170 except TypeError:
103 pass 171 pass