Skip to content

Commit d09f030

Browse files
committed
templates: fix issues with spaces
I have seen different scenarios regarding spaces in templates. On some platforms there might be spaces, on some others not. The biggest changes are in: src/pygccxml/parser/scanner.py -> replace spaces that are actually coming from the xml file src/pygccxml/declarations/pattern_parser.py -> remove spaces Of course all the tests had to be adjusted This "might" be a breaking change if you used to compare declarations manually, but from a pygccxml point of view all comparisons / search functions are still working as before.
1 parent 9ee6e29 commit d09f030

9 files changed

+117
-115
lines changed

Diff for: src/pygccxml/declarations/container_traits.py

+31-32
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def erase_allocator(self, cls_name, default_allocator='std::allocator'):
105105
return
106106
value_type = c_args[0]
107107
tmpl = string.Template(
108-
"$container< $value_type, $allocator<$value_type> >")
108+
"$container<$value_type, $allocator<$value_type>>")
109109
tmpl = tmpl.substitute(
110110
container=c_name,
111111
value_type=value_type,
@@ -122,11 +122,10 @@ def erase_container(self, cls_name, default_container_name='std::deque'):
122122
return
123123
value_type = c_args[0]
124124
dc_no_defaults = self.erase_recursive(c_args[1])
125-
if self.normalize(dc_no_defaults) != self.normalize(
125+
if self.normalize(dc_no_defaults) == self.normalize(
126126
templates.join(default_container_name, [value_type])):
127-
return
128-
return templates.join(
129-
c_name, [self.erase_recursive(value_type)])
127+
return templates.join(
128+
c_name, [self.erase_recursive(value_type)])
130129

131130
def erase_container_compare(
132131
self,
@@ -159,8 +158,8 @@ def erase_compare_allocator(
159158
return
160159
value_type = c_args[0]
161160
tmpl = string.Template(
162-
"$container< $value_type, $compare<$value_type>, " +
163-
"$allocator<$value_type> >")
161+
"$container<$value_type, $compare<$value_type>, " +
162+
"$allocator<$value_type>>")
164163
tmpl = tmpl.substitute(
165164
container=c_name,
166165
value_type=value_type,
@@ -184,14 +183,14 @@ def erase_map_compare_allocator(
184183
mapped_type = c_args[1]
185184
tmpls = [
186185
string.Template(
187-
"$container< $key_type, $mapped_type, $compare<$key_type>, " +
188-
"$allocator< std::pair< const $key_type, $mapped_type> > >"),
186+
"$container<$key_type, $mapped_type, $compare<$key_type>, " +
187+
"$allocator<std::pair<const $key_type, $mapped_type>>>"),
189188
string.Template(
190-
"$container< $key_type, $mapped_type, $compare<$key_type>, " +
191-
"$allocator< std::pair< $key_type const, $mapped_type> > >"),
189+
"$container<$key_type, $mapped_type, $compare<$key_type>, " +
190+
"$allocator<std::pair< $key_type const, $mapped_type>>>"),
192191
string.Template(
193-
"$container< $key_type, $mapped_type, $compare<$key_type>, " +
194-
"$allocator< std::pair< $key_type, $mapped_type> > >")]
192+
"$container<$key_type, $mapped_type, $compare<$key_type>, " +
193+
"$allocator<std::pair<$key_type, $mapped_type>>>")]
195194
for tmpl in tmpls:
196195
tmpl = tmpl.substitute(
197196
container=c_name,
@@ -218,13 +217,13 @@ def erase_hash_allocator(self, cls_name):
218217
if len(c_args) == 3:
219218
default_hash = 'hash_compare'
220219
tmpl = (
221-
"$container< $value_type, $hash<$value_type, " +
222-
"$less<$value_type> >, $allocator<$value_type> >")
220+
"$container<$value_type, $hash<$value_type, " +
221+
"$less<$value_type>>, $allocator<$value_type>>")
223222
elif len(c_args) == 4:
224223
default_hash = 'hash'
225224
tmpl = (
226-
"$container< $value_type, $hash<$value_type >, " +
227-
"$equal_to<$value_type >, $allocator<$value_type> >")
225+
"$container<$value_type, $hash<$value_type>, " +
226+
"$equal_to<$value_type>, $allocator<$value_type>>")
228227
else:
229228
return
230229

@@ -263,14 +262,14 @@ def erase_hashmap_compare_allocator(self, cls_name):
263262
if len(c_args) == 4:
264263
default_hash = 'hash_compare'
265264
tmpl = string.Template(
266-
"$container< $key_type, $mapped_type, " +
267-
"$hash<$key_type, $less<$key_type> >, " +
268-
"$allocator< std::pair< const $key_type, $mapped_type> > >")
265+
"$container<$key_type, $mapped_type, " +
266+
"$hash<$key_type, $less<$key_type>>, " +
267+
"$allocator<std::pair<const $key_type, $mapped_type>>>")
269268
if key_type.startswith('const ') or key_type.endswith(' const'):
270269
tmpl = string.Template(
271-
"$container< $key_type, $mapped_type, $hash<$key_type, " +
272-
"$less<$key_type> >, $allocator< std::pair< $key_type, " +
273-
"$mapped_type> > >")
270+
"$container<$key_type, $mapped_type, $hash<$key_type, " +
271+
"$less<$key_type>>, $allocator<std::pair<$key_type, " +
272+
"$mapped_type>>>")
274273
elif len(c_args) == 5:
275274
default_hash = 'hash'
276275
if self.unordered_maps_and_sets:
@@ -279,31 +278,31 @@ def erase_hashmap_compare_allocator(self, cls_name):
279278
"$hash<$key_type>, " +
280279
"$equal_to<$key_type>, " +
281280
"$allocator<std::pair<const$key_type, " +
282-
"$mapped_type> > >")
281+
"$mapped_type>>>")
283282
if key_type.startswith('const ') or \
284283
key_type.endswith(' const'):
285284
tmpl = string.Template(
286285
"$container<$key_type, $mapped_type, " +
287-
"$hash<$key_type >, " +
288-
"$equal_to<$key_type >, " +
286+
"$hash<$key_type>, " +
287+
"$equal_to<$key_type>, " +
289288
"$allocator<std::pair<$key_type, " +
290-
"$mapped_type> > >")
289+
"$mapped_type>>>")
291290
else:
292291
tmpl = string.Template(
293-
"$container< $key_type, $mapped_type, "
292+
"$container<$key_type, $mapped_type, "
294293
"$hash<$key_type >, " +
295294
"$equal_to<$key_type>, "
296-
"$allocator< $mapped_type> >")
295+
"$allocator<$mapped_type>>")
297296
if key_type.startswith('const ') or \
298297
key_type.endswith(' const'):
299298
# TODO: this template is the same than above.
300299
# Make sure why this was needed and if this is
301300
# tested. There may be a const missing somewhere.
302301
tmpl = string.Template(
303-
"$container< $key_type, $mapped_type, " +
304-
"$hash<$key_type >, " +
302+
"$container<$key_type, $mapped_type, " +
303+
"$hash<$key_type>, " +
305304
"$equal_to<$key_type>, " +
306-
"$allocator< $mapped_type > >")
305+
"$allocator<$mapped_type>>")
307306
else:
308307
return
309308

Diff for: src/pygccxml/declarations/pattern_parser.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,11 @@ def join(self, name, args, arg_separator=None):
192192
args = [_f for _f in args if _f]
193193

194194
if not args:
195-
args_str = ' '
195+
args_str = ''
196196
elif len(args) == 1:
197-
args_str = ' ' + args[0] + ' '
197+
args_str = args[0]
198198
else:
199-
args_str = ' ' + arg_separator.join(args) + ' '
199+
args_str = arg_separator.join(args)
200200

201201
return ''.join([name, self.__begin, args_str, self.__end])
202202

Diff for: src/pygccxml/parser/scanner.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,6 @@ def members(self):
285285
return self.__members
286286

287287
def startElement(self, name, attrs):
288-
289288
try:
290289
if name not in self.__readers:
291290
return
@@ -656,6 +655,12 @@ def __read_class_impl(self, class_type, attrs):
656655
name = attrs.get(XML_AN_NAME, '')
657656
if '$' in name or '.' in name:
658657
name = ''
658+
if "<" in name and " >" in name:
659+
# Name with template. In some rare cases there
660+
# is a space before > (and only there), so remove
661+
# it to be consistent with the other names that
662+
# have no space there
663+
name = name.replace(" >", ">")
659664
if XML_AN_INCOMPLETE in attrs:
660665
decl = self.__decl_factory.create_class_declaration(name=name)
661666
else:

Diff for: tests/test_call_invocation.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def test_split_on_map():
6262

6363

6464
def test_join_on_vector():
65-
assert "vector( int, std::allocator(int) )" == \
65+
assert "vector(int, std::allocator(int))" == \
6666
declarations.call_invocation.join(
6767
"vector", ("int", "std::allocator(int)"))
6868

Diff for: tests/test_find_container_traits.py

+18-18
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ def __cmp_traits(global_ns, typedef, expected, partial_name, key_type=None):
3333
assert declarations.find_container_traits(cls) == expected
3434
assert cls.partial_name == partial_name
3535
cls = traits.class_declaration(cls)
36-
print("xxxx", traits, typedef)
3736
assert traits.element_type(typedef) is not None
3837
assert cls.cache.container_element_type is not None
3938

@@ -51,91 +50,92 @@ def test_find_traits(global_ns):
5150
global_ns,
5251
"v_int",
5352
declarations.vector_traits,
54-
"vector< int >"
53+
"vector<int>"
5554
)
5655
__cmp_traits(
5756
global_ns,
5857
"l_int",
5958
declarations.list_traits,
60-
"list< int >"
59+
"list<int>"
6160
)
6261
__cmp_traits(
63-
global_ns, "d_v_int",
62+
global_ns,
63+
"d_v_int",
6464
declarations.deque_traits,
65-
"deque< std::vector< int > >"
65+
"deque<std::vector<int>>"
6666
)
6767
__cmp_traits(
6868
global_ns, "q_int",
6969
declarations.queue_traits,
70-
"queue< int >"
70+
"queue<int>"
7171
)
7272
__cmp_traits(
7373
global_ns, "pq_int",
7474
declarations.priority_queue_traits,
75-
"priority_queue< int >"
75+
"priority_queue<int>"
7676
)
7777
__cmp_traits(
7878
global_ns, "s_v_int",
7979
declarations.set_traits,
80-
"set< std::vector< int > >"
80+
"set<std::vector<int>>"
8181
)
8282
__cmp_traits(
8383
global_ns,
8484
"ms_v_int",
8585
declarations.multiset_traits,
86-
"multiset< std::vector< int > >",
86+
"multiset<std::vector<int>>",
8787
)
8888
__cmp_traits(
8989
global_ns, "m_i2d",
9090
declarations.map_traits,
91-
"map< int, double >",
91+
"map<int, double>",
9292
"int"
9393
)
9494
__cmp_traits(
9595
global_ns,
9696
"mm_i2d",
9797
declarations.multimap_traits,
98-
"multimap< int, double >",
98+
"multimap<int, double>",
9999
"int",
100100
)
101101
__cmp_traits(
102102
global_ns,
103103
"hs_v_int",
104104
declarations.unordered_set_traits,
105-
"unordered_set< std::vector< int > >",
105+
"unordered_set<std::vector<int>>",
106106
)
107107
__cmp_traits(
108108
global_ns,
109109
"mhs_v_int",
110110
declarations.unordered_multiset_traits,
111-
"unordered_multiset< std::vector< int > >",
111+
"unordered_multiset<std::vector<int>>",
112112
)
113113
__cmp_traits(
114114
global_ns,
115115
"hm_i2d",
116116
declarations.unordered_map_traits,
117-
"unordered_map< int, double >",
117+
"unordered_map<int, double>",
118118
"int",
119119
)
120120
__cmp_traits(
121121
global_ns,
122122
"hmm_i2d",
123123
declarations.unordered_multimap_traits,
124-
"unordered_multimap< int, double >",
124+
"unordered_multimap<int, double>",
125125
"int",
126126
)
127127

128128

129129
def test_multimap(global_ns):
130130
m = global_ns.class_(lambda decl: decl.name.startswith("multimap"))
131131
declarations.find_container_traits(m)
132-
assert m.partial_name == "multimap< int, int >"
132+
assert m.partial_name == "multimap<int, int>"
133133

134134

135135
def test_recursive_partial_name(global_ns):
136136
f1 = global_ns.free_function("f1")
137137
t1 = declarations.class_traits.get_declaration(f1.arguments[0].decl_type)
138-
assert "type< std::set< std::vector< int > > >" == t1.partial_name
138+
assert "type<std::set<std::vector<int>>>" == t1.partial_name
139139

140140

141141
def test_remove_defaults_partial_name_namespace(global_ns):
@@ -154,7 +154,7 @@ def test_from_ogre():
154154
"map<std::string, bool (*)(std::string&, "
155155
+ "Ogre::MaterialScriptContext&), std::less<std::string>, "
156156
+ "std::allocator<std::pair<std::string const, bool (*)"
157-
+ "(std::string&, Ogre::MaterialScriptContext&)> > >"
157+
+ "(std::string&, Ogre::MaterialScriptContext&)>>>"
158158
)
159159
ct = declarations.find_container_traits(x)
160160
ct.remove_defaults(x)

0 commit comments

Comments
 (0)