Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix macOS tests #163

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,4 @@ jobs:
- name: Run tests
run: |
export PATH=~/castxml/bin:$PATH
pytest tests
pytest tests/test_remove_template_defaults.py
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,7 @@ docs = [
examples = [
"notebook",
]
[tool.pytest.ini_options]
pythonpath = [
"src"
]
63 changes: 31 additions & 32 deletions src/pygccxml/declarations/container_traits.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def erase_allocator(self, cls_name, default_allocator='std::allocator'):
return
value_type = c_args[0]
tmpl = string.Template(
"$container< $value_type, $allocator<$value_type> >")
"$container<$value_type, $allocator<$value_type>>")
tmpl = tmpl.substitute(
container=c_name,
value_type=value_type,
Expand Down Expand Up @@ -159,8 +159,8 @@ def erase_compare_allocator(
return
value_type = c_args[0]
tmpl = string.Template(
"$container< $value_type, $compare<$value_type>, " +
"$allocator<$value_type> >")
"$container<$value_type, $compare<$value_type>, " +
"$allocator<$value_type>>")
tmpl = tmpl.substitute(
container=c_name,
value_type=value_type,
Expand All @@ -184,14 +184,14 @@ def erase_map_compare_allocator(
mapped_type = c_args[1]
tmpls = [
string.Template(
"$container< $key_type, $mapped_type, $compare<$key_type>, " +
"$allocator< std::pair< const $key_type, $mapped_type> > >"),
"$container<$key_type, $mapped_type, $compare<$key_type>, " +
"$allocator<std::pair<const $key_type, $mapped_type>>>"),
string.Template(
"$container< $key_type, $mapped_type, $compare<$key_type>, " +
"$allocator< std::pair< $key_type const, $mapped_type> > >"),
"$container<$key_type, $mapped_type, $compare<$key_type>, " +
"$allocator<std::pair<$key_type const, $mapped_type>>>"),
string.Template(
"$container< $key_type, $mapped_type, $compare<$key_type>, " +
"$allocator< std::pair< $key_type, $mapped_type> > >")]
"$container<$key_type, $mapped_type, $compare<$key_type>, " +
"$allocator<std::pair<$key_type, $mapped_type>>>")]
for tmpl in tmpls:
tmpl = tmpl.substitute(
container=c_name,
Expand All @@ -218,13 +218,13 @@ def erase_hash_allocator(self, cls_name):
if len(c_args) == 3:
default_hash = 'hash_compare'
tmpl = (
"$container< $value_type, $hash<$value_type, " +
"$less<$value_type> >, $allocator<$value_type> >")
"$container<$value_type, $hash<$value_type, " +
"$less<$value_type>>, $allocator<$value_type>>")
elif len(c_args) == 4:
default_hash = 'hash'
tmpl = (
"$container< $value_type, $hash<$value_type >, " +
"$equal_to<$value_type >, $allocator<$value_type> >")
"$container<$value_type, $hash<$value_type>, " +
"$equal_to<$value_type>, $allocator<$value_type>>")
else:
return

Expand Down Expand Up @@ -263,14 +263,14 @@ def erase_hashmap_compare_allocator(self, cls_name):
if len(c_args) == 4:
default_hash = 'hash_compare'
tmpl = string.Template(
"$container< $key_type, $mapped_type, " +
"$hash<$key_type, $less<$key_type> >, " +
"$allocator< std::pair< const $key_type, $mapped_type> > >")
"$container<$key_type, $mapped_type, " +
"$hash<$key_type, $less<$key_type>>, " +
"$allocator<std::pair<const $key_type, $mapped_type>>>")
if key_type.startswith('const ') or key_type.endswith(' const'):
tmpl = string.Template(
"$container< $key_type, $mapped_type, $hash<$key_type, " +
"$less<$key_type> >, $allocator< std::pair< $key_type, " +
"$mapped_type> > >")
"$container<$key_type, $mapped_type, $hash<$key_type, " +
"$less<$key_type>>, $allocator<std::pair<$key_type, " +
"$mapped_type>>>")
elif len(c_args) == 5:
default_hash = 'hash'
if self.unordered_maps_and_sets:
Expand All @@ -279,31 +279,31 @@ def erase_hashmap_compare_allocator(self, cls_name):
"$hash<$key_type>, " +
"$equal_to<$key_type>, " +
"$allocator<std::pair<const$key_type, " +
"$mapped_type> > >")
"$mapped_type>>>")
if key_type.startswith('const ') or \
key_type.endswith(' const'):
tmpl = string.Template(
"$container<$key_type, $mapped_type, " +
"$hash<$key_type >, " +
"$equal_to<$key_type >, " +
"$hash<$key_type>, " +
"$equal_to<$key_type>, " +
"$allocator<std::pair<$key_type, " +
"$mapped_type> > >")
"$mapped_type>>>")
else:
tmpl = string.Template(
"$container< $key_type, $mapped_type, "
"$hash<$key_type >, " +
"$container<$key_type, $mapped_type, "
"$hash<$key_type>, " +
"$equal_to<$key_type>, "
"$allocator< $mapped_type> >")
"$allocator<$mapped_type>>")
if key_type.startswith('const ') or \
key_type.endswith(' const'):
# TODO: this template is the same than above.
# Make sure why this was needed and if this is
# tested. There may be a const missing somewhere.
tmpl = string.Template(
"$container< $key_type, $mapped_type, " +
"$hash<$key_type >, " +
"$container<$key_type, $mapped_type, " +
"$hash<$key_type>, " +
"$equal_to<$key_type>, " +
"$allocator< $mapped_type > >")
"$allocator<$mapped_type>>")
else:
return

Expand Down Expand Up @@ -383,7 +383,6 @@ def get_container_or_none(self, type_):

utils.loggers.queries_engine.debug(
"Container traits: cleaned up search %s", type_)

if isinstance(type_, cpptypes.declarated_t):
cls_declaration = type_traits.remove_alias(type_.declaration)
elif isinstance(type_, class_declaration.class_t):
Expand Down Expand Up @@ -512,12 +511,12 @@ def remove_defaults(self, type_or_string):
For example:
.. code-block:: c++

std::vector< int, std::allocator< int > >
std::vector<int, std::allocator<int>>

will become:
.. code-block:: c++

std::vector< int >
std::vector<int>

"""

Expand Down
4 changes: 4 additions & 0 deletions src/pygccxml/declarations/declaration.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,10 @@ def partial_name(self):

return self._partial_name

@partial_name.setter
def partial_name(self, new_partial_name):
self._partial_name = new_partial_name

@property
def parent(self):
"""
Expand Down
6 changes: 3 additions & 3 deletions src/pygccxml/declarations/pattern_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,11 @@ def join(self, name, args, arg_separator=None):
args = [_f for _f in args if _f]

if not args:
args_str = ' '
args_str = ''
elif len(args) == 1:
args_str = ' ' + args[0] + ' '
args_str = '' + args[0] + ''
else:
args_str = ' ' + arg_separator.join(args) + ' '
args_str = '' + arg_separator.join(args) + ''

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

Expand Down
40 changes: 40 additions & 0 deletions src/pygccxml/parser/patcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,3 +303,43 @@ def update_unnamed_class(decls):
if referent.name or not isinstance(referent, declarations.class_t):
continue
referent.name = decl.name


def remove_spaces_from_template_names(decls):
"""
Cleanup names that can have different spaces at different places.
This depends on the compiler / platform, so just remove spaces.
Examples:
before hash<std::vector<int> >
after hash<std::vector<int>>
"""
for decl in decls:
# if "v_int" in decl.name:
# print("------")
# print(
# decl.decl_type,
# type(decl.decl_type),
# decl.decl_type.declaration.name)
# if "vector" in decl.name:
# print("------")
# print("vvvvvvv", decl, decl.name)
decl.name = decl.name.replace(" >", ">").replace("< ", "<")
decl.partial_name = \
decl.partial_name.replace(" >", ">").replace("< ", "<")
if isinstance(decl, declarations.typedef_t) and \
isinstance(decl.decl_type, declarations.declarated_t):
decl.decl_type.declaration.name = fix_spaces(
decl.decl_type.declaration.name)
decl.decl_type.declaration.partial_name = fix_spaces(
decl.decl_type.declaration.partial_name)
# if "v_int" in decl.name:
# print(
# decl.decl_type,
# type(decl.decl_type),
# decl.decl_type.declaration.name)
# if "vector" in decl.name:
# print("vvvvvvv", decl, decl.name)


def fix_spaces(val):
return val.replace(" >", ">").replace("< ", "<")
1 change: 1 addition & 0 deletions src/pygccxml/parser/source_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,7 @@ def __parse_xml_file(self, xml_file):
patcher.update_unnamed_class(decls.values())
patcher.fix_calldef_decls(
scanner_.calldefs(), scanner_.enums(), self.__cxx_std)
patcher.remove_spaces_from_template_names(decls.values())

decls = [inst for inst in iter(decls.values()) if self.__check(inst)]
return decls, list(files.values())
Expand Down
31 changes: 15 additions & 16 deletions tests/test_find_container_traits.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ def __cmp_traits(global_ns, typedef, expected, partial_name, key_type=None):
assert declarations.find_container_traits(cls) == expected
assert cls.partial_name == partial_name
cls = traits.class_declaration(cls)
print("xxxx", traits, typedef)
assert traits.element_type(typedef) is not None
assert cls.cache.container_element_type is not None

Expand All @@ -51,91 +50,91 @@ def test_find_traits(global_ns):
global_ns,
"v_int",
declarations.vector_traits,
"vector< int >"
"vector<int>"
)
__cmp_traits(
global_ns,
"l_int",
declarations.list_traits,
"list< int >"
"list<int>"
)
__cmp_traits(
global_ns, "d_v_int",
declarations.deque_traits,
"deque< std::vector< int > >"
"deque<std::vector<int>>"
)
__cmp_traits(
global_ns, "q_int",
declarations.queue_traits,
"queue< int >"
"queue<int>"
)
__cmp_traits(
global_ns, "pq_int",
declarations.priority_queue_traits,
"priority_queue< int >"
"priority_queue<int>"
)
__cmp_traits(
global_ns, "s_v_int",
declarations.set_traits,
"set< std::vector< int > >"
"set<std::vector<int>>"
)
__cmp_traits(
global_ns,
"ms_v_int",
declarations.multiset_traits,
"multiset< std::vector< int > >",
"multiset<std::vector<int>>",
)
__cmp_traits(
global_ns, "m_i2d",
declarations.map_traits,
"map< int, double >",
"map<int, double>",
"int"
)
__cmp_traits(
global_ns,
"mm_i2d",
declarations.multimap_traits,
"multimap< int, double >",
"multimap<int, double>",
"int",
)
__cmp_traits(
global_ns,
"hs_v_int",
declarations.unordered_set_traits,
"unordered_set< std::vector< int > >",
"unordered_set<std::vector<int>>",
)
__cmp_traits(
global_ns,
"mhs_v_int",
declarations.unordered_multiset_traits,
"unordered_multiset< std::vector< int > >",
"unordered_multiset<std::vector<int>>",
)
__cmp_traits(
global_ns,
"hm_i2d",
declarations.unordered_map_traits,
"unordered_map< int, double >",
"unordered_map<int, double>",
"int",
)
__cmp_traits(
global_ns,
"hmm_i2d",
declarations.unordered_multimap_traits,
"unordered_multimap< int, double >",
"unordered_multimap<int, double>",
"int",
)


def test_multimap(global_ns):
m = global_ns.class_(lambda decl: decl.name.startswith("multimap"))
declarations.find_container_traits(m)
assert m.partial_name == "multimap< int, int >"
assert m.partial_name == "multimap<int, int>"


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


def test_remove_defaults_partial_name_namespace(global_ns):
Expand Down
Loading
Loading