comparison tests/test-parseindex2.py @ 20159:96b2dd77c85d

parsers: backout version mismatch detection from 21dafd8546d1 This introduced mandatory recompilations and breaks pure mode in tests
author Matt Mackall <mpm@selenic.com>
date Sun, 01 Dec 2013 20:46:36 -0600
parents 21dafd8546d1
children 7eda5bb9ec8f
comparison
equal deleted inserted replaced
20156:28fe5abc906f 20159:96b2dd77c85d
1 from mercurial import parsers 1 from mercurial import parsers
2 from mercurial.node import nullid, nullrev 2 from mercurial.node import nullid, nullrev
3 import struct 3 import struct
4 import subprocess
5 import sys
6 4
7 # This unit test compares the return value of the original Python 5 # This unit test compares the return value of the original Python
8 # implementation of parseindex and the new C implementation for 6 # implementation of parseindex and the new C implementation for
9 # an index file with and without inlined data 7 # an index file with and without inlined data
10 8
97 95
98 def parse_index2(data, inline): 96 def parse_index2(data, inline):
99 index, chunkcache = parsers.parse_index2(data, inline) 97 index, chunkcache = parsers.parse_index2(data, inline)
100 return list(index), chunkcache 98 return list(index), chunkcache
101 99
102 def importparsers(hexversion):
103 """Import mercurial.parsers with the given sys.hexversion."""
104 # The file parsers.c inspects sys.hexversion to determine the version
105 # of the currently-running Python interpreter, so we monkey-patch
106 # sys.hexversion to simulate using different versions.
107 code = ("import sys; sys.hexversion=%s; "
108 "import mercurial.parsers" % hexversion)
109 cmd = "python -c \"%s\"" % code
110 # We need to do these tests inside a subprocess because parser.c's
111 # version-checking code happens inside the module init function, and
112 # when using reload() to reimport an extension module, "The init function
113 # of extension modules is not called a second time"
114 # (from http://docs.python.org/2/library/functions.html?#reload).
115 p = subprocess.Popen(cmd, shell=True,
116 stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
117 return p.communicate() # returns stdout, stderr
118
119 def printhexfail(testnumber, hexversion, msg):
120 try:
121 hexstring = hex(hexversion)
122 except TypeError:
123 hexstring = None
124 print ("%s) using Python %s and patched sys.hexversion %r (%r): %s" %
125 (testnumber, sys.version_info, hexversion, hexstring, msg))
126
127 def testversionokay(testnumber, hexversion):
128 stdout, stderr = importparsers(hexversion)
129 if stdout:
130 printhexfail(testnumber, hexversion,
131 "Expected no stdout but got: %r" % stdout)
132
133 def testversionfail(testnumber, hexversion):
134 stdout, stderr = importparsers(hexversion)
135 if not "ImportError: Python minor version mismatch" in stdout:
136 printhexfail(testnumber, hexversion,
137 "Expected stdout to contain %r but got: %r" %
138 (errstring, stdout))
139
140 def makehex(major, minor, micro):
141 return int("%x%02x%02x00" % (major, minor, micro), 16)
142
143 def runversiontests():
144 """Test importing parsers using different Python versions."""
145 info = sys.version_info
146 major, minor, micro = info[0], info[1], info[2]
147 # Test same major-minor versions.
148 testversionokay(1, makehex(major, minor, micro))
149 testversionokay(2, makehex(major, minor, micro + 1))
150 # Test different major-minor versions.
151 testversionfail(3, makehex(major + 1, minor, micro))
152 testversionfail(4, makehex(major, minor + 1, micro))
153 testversionfail(5, "'foo'")
154
155 def runtest() : 100 def runtest() :
156 runversiontests()
157
158 # Check that parse_index2() raises TypeError on bad arguments. 101 # Check that parse_index2() raises TypeError on bad arguments.
159 try: 102 try:
160 parse_index2(0, True) 103 parse_index2(0, True)
161 except TypeError: 104 except TypeError:
162 pass 105 pass