Skip to content

Commit 262630c

Browse files
committed
Merge branch 'FixBug27' into hotfix/v1.7.2
2 parents 040b486 + 54a8238 commit 262630c

File tree

5 files changed

+86
-0
lines changed

5 files changed

+86
-0
lines changed

pygccxml/parser/declarations_cache.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,13 @@ class record_t(object):
102102

103103
def __init__(
104104
self,
105+
xml_generator,
105106
source_signature,
106107
config_signature,
107108
included_files,
108109
included_files_signature,
109110
declarations):
111+
self.__xml_generator = xml_generator
110112
self.__source_signature = source_signature
111113
self.__config_signature = config_signature
112114
self.__included_files = included_files
@@ -151,6 +153,10 @@ def included_files_signature(self):
151153
def declarations(self):
152154
return self.__declarations
153155

156+
@property
157+
def xml_generator(self):
158+
return self.__xml_generator
159+
154160

155161
class file_cache_t(cache_base_t):
156162

@@ -174,6 +180,23 @@ def __init__(self, name):
174180
self.__cache) # If empty then we need to flush
175181
for entry in self.__cache.values(): # Clear hit flags
176182
entry.was_hit = False
183+
try:
184+
# Make sure the xml_generator variable is defined, else it
185+
# will stay None.
186+
xml_generator = entry.xml_generator
187+
except AttributeError:
188+
msg = (
189+
"The %s cache file is not compatible with this version " +
190+
"of pygccxml. Please regenerate it.") % name
191+
raise RuntimeError(msg)
192+
if utils.xml_generator is None:
193+
# Set the xml_generator to the one read in the cache file
194+
utils.xml_generator = xml_generator
195+
elif utils.xml_generator != xml_generator:
196+
msg = (
197+
"The %s cache file was generated with a different xml " +
198+
"generator. Please regenerate it.") % name
199+
raise RuntimeError(msg)
177200

178201
@staticmethod
179202
def __load(file_name):
@@ -231,6 +254,7 @@ def update(self, source_file, configuration, declarations, included_files):
231254
""" Update a cached record with the current key and value contents. """
232255

233256
record = record_t(
257+
xml_generator=utils.xml_generator,
234258
source_signature=file_signature(source_file),
235259
config_signature=configuration_signature(configuration),
236260
included_files=included_files,

unittests/data/new_cache.cache

2.32 KB
Binary file not shown.

unittests/data/old_cache.cache

2.29 KB
Binary file not shown.

unittests/file_cache_tester.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
# See http://www.boost.org/LICENSE_1_0.txt
55

66
import os
7+
import sys
78
import unittest
89
import autoconfig
10+
import subprocess
911
import parser_test_case
1012
from pygccxml import parser
1113

@@ -78,6 +80,16 @@ def test_from_file(self):
7880
("cached declarations and source declarations are different, " +
7981
"after pickling"))
8082

83+
def test_reopen_cache(self):
84+
"""
85+
Test opening cache files in a subprocess (with a clean environment).
86+
87+
"""
88+
p = subprocess.Popen(
89+
[sys.executable, "unittests/reopen_cache_tester.py"],
90+
stdout=subprocess.PIPE)
91+
print(p.stdout.read())
92+
8193

8294
def create_suite():
8395
suite = unittest.TestSuite()

unittests/reopen_cache_tester.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Copyright 2014-2015 Insight Software Consortium.
2+
# Copyright 2004-2008 Roman Yakovenko.
3+
# Distributed under the Boost Software License, Version 1.0.
4+
# See http://www.boost.org/LICENSE_1_0.txt
5+
6+
import os
7+
try:
8+
import unittest2 as unittest
9+
except ImportError:
10+
import unittest
11+
import autoconfig
12+
from pygccxml import parser
13+
from pygccxml import utils
14+
15+
16+
def test_re_opening_cache_file():
17+
"""
18+
Test re-oping cache files.
19+
20+
This test is run by file_cache_tester.py in a subprocess.
21+
22+
"""
23+
24+
data = autoconfig.data_directory
25+
26+
# xml_generator has not been set
27+
if utils.xml_generator is not None:
28+
raise Exception
29+
30+
# Try to reopen an old cache file and check if there is an exception
31+
# These old files do not know about the xml generator; a RuntimeError
32+
# should be thrown, asking to regenerate the cache file.
33+
c_file = os.path.join(data, 'old_cache.cache')
34+
error = False
35+
try:
36+
parser.file_cache_t(c_file)
37+
except RuntimeError:
38+
error = True
39+
if error is False:
40+
raise Exception
41+
42+
# This cache file knows about the xml generator, and was generated
43+
# with CastXML. Loading the cache should set the utils.xml_generator.
44+
c_file = os.path.join(data, 'new_cache.cache')
45+
parser.file_cache_t(c_file)
46+
if "CastXML" not in utils.xml_generator:
47+
raise Exception
48+
49+
if __name__ == "__main__":
50+
test_re_opening_cache_file()

0 commit comments

Comments
 (0)