Skip to content

Commit 539b136

Browse files
committedFeb 13, 2025··
tests: extract dynamic exception tests
Fix failures when tested with c++17 and above See #227 (comment) https://developers.redhat.com/articles/2021/08/06/porting-your-code-c17-gcc-11#exception_specification_changes From what I read here, there is no way to add exception specifications anymore (unless you use noexcept).
1 parent c98759f commit 539b136

7 files changed

+98
-34
lines changed
 

‎src/pygccxml/declarations/calldef.py

+14-2
Original file line numberDiff line numberDiff line change
@@ -266,12 +266,24 @@ def does_throw(self, does_throw):
266266

267267
@property
268268
def exceptions(self):
269-
"""The list of exceptions.
270-
@type: list of :class:`declaration_t`"""
269+
"""
270+
The list of exceptions.
271+
272+
Useless with c++17 and above as dynamich exceptions
273+
are not allowed anymore:
274+
https://developers.redhat.com/articles/2021/08/06/porting-your-code-c17-gcc-11#exception_specification_changes # noqa
275+
276+
@type: list of :class:`declaration_t`
277+
"""
271278
return self._exceptions
272279

273280
@exceptions.setter
274281
def exceptions(self, exceptions):
282+
"""
283+
Useless with c++17 and above as dynamich exceptions
284+
are not allowed anymore:
285+
https://developers.redhat.com/articles/2021/08/06/porting-your-code-c17-gcc-11#exception_specification_changes # noqa
286+
"""
275287
self._exceptions = exceptions
276288

277289
@property

‎tests/data/declarations_calldef.hpp

-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ int return_default_args( int arg=1, bool flag=false );
2222

2323
extern void static_call();
2424

25-
void calldef_with_throw() throw( some_exception_t, other_exception_t );
26-
2725
struct calldefs_t{
2826
calldefs_t();
2927

‎tests/data/test_dynamic_exception.hpp

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2014-2017 Insight Software Consortium.
2+
// Copyright 2004-2009 Roman Yakovenko.
3+
// Distributed under the Boost Software License, Version 1.0.
4+
// See http://www.boost.org/LICENSE_1_0.txt
5+
6+
7+
namespace declarations {
8+
namespace calldef {
9+
10+
class some_exception_t{};
11+
12+
class other_exception_t{};
13+
14+
void calldef_with_throw() throw( some_exception_t, other_exception_t );
15+
16+
}
17+
}

‎tests/test_declarations.py

-8
Original file line numberDiff line numberDiff line change
@@ -171,14 +171,6 @@ def test_calldef_free_functions(global_ns, helpers):
171171
)
172172
helpers._test_calldef_exceptions(global_ns, return_default_args, [])
173173

174-
calldef_with_throw = ns.free_function("calldef_with_throw")
175-
assert calldef_with_throw is not None
176-
helpers._test_calldef_exceptions(
177-
global_ns, calldef_with_throw,
178-
["some_exception_t", "other_exception_t"]
179-
)
180-
# from now there is no need to check exception specification
181-
182174

183175
@pytest.mark.parametrize(
184176
"global_ns",

‎tests/test_dependencies.py

-22
Original file line numberDiff line numberDiff line change
@@ -143,28 +143,6 @@ def test_calldefs(global_ns):
143143
for dependency in dependencies_new]
144144
assert used_types == ['int', 'int', 'bool']
145145

146-
some_exception = ns.class_('some_exception_t')
147-
other_exception = ns.class_('other_exception_t')
148-
calldef_with_throw = ns.calldef('calldef_with_throw')
149-
150-
# Legacy way of fetching dependencies. Is still valid but deprecated
151-
warnings.simplefilter("ignore", Warning)
152-
dependencies_old = calldef_with_throw.i_depend_on_them()
153-
warnings.simplefilter("error", Warning)
154-
assert len(dependencies_old) == 3
155-
dependencies_old = [
156-
dependency for dependency in dependencies_old if
157-
dependency.depend_on_it in (some_exception, other_exception)]
158-
assert len(dependencies_old) == 2
159-
160-
dependencies_new = declarations.get_dependencies_from_decl(
161-
calldef_with_throw)
162-
assert len(dependencies_new) == 3
163-
dependencies_new = [
164-
dependency for dependency in dependencies_new if
165-
dependency.depend_on_it in (some_exception, other_exception)]
166-
assert len(dependencies_new) == 2
167-
168146

169147
def test_coverage(global_ns):
170148
declarations.get_dependencies_from_decl(global_ns)

‎tests/test_dynamic_exception.py

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Copyright 2014-2017 Insight Software Consortium.
2+
# Copyright 2004-2009 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 pytest
7+
8+
import warnings
9+
10+
from . import autoconfig
11+
12+
from pygccxml import parser
13+
from pygccxml import declarations
14+
15+
16+
TEST_FILES = [
17+
"test_dynamic_exception.hpp",
18+
]
19+
20+
21+
@pytest.fixture
22+
def global_ns():
23+
COMPILATION_MODE = parser.COMPILATION_MODE.ALL_AT_ONCE
24+
config = autoconfig.cxx_parsers_cfg.config.clone()
25+
# This test does not work with c++17 and above
26+
# See https://developers.redhat.com/articles/2021/08/06/porting-your-code-c17-gcc-11#exception_specification_changes # noqa
27+
# This test is thus excpected to use -std=c++14 forever
28+
config.cflags = "-std=c++14"
29+
decls = parser.parse(TEST_FILES, config, COMPILATION_MODE)
30+
global_ns = declarations.get_global_namespace(decls)
31+
global_ns.init_optimizer()
32+
return global_ns
33+
34+
35+
def test_calldef_with_throw(global_ns, helpers):
36+
calldef_with_throw = global_ns.free_function("calldef_with_throw")
37+
assert calldef_with_throw is not None
38+
helpers._test_calldef_exceptions(
39+
global_ns, calldef_with_throw,
40+
["some_exception_t", "other_exception_t"]
41+
)
42+
43+
calldef_with_throw = global_ns.calldef('calldef_with_throw')
44+
some_exception = global_ns.class_('some_exception_t')
45+
other_exception = global_ns.class_('other_exception_t')
46+
47+
# Legacy way of fetching dependencies. Is still valid but deprecated
48+
warnings.simplefilter("ignore", Warning)
49+
dependencies_old = calldef_with_throw.i_depend_on_them()
50+
warnings.simplefilter("error", Warning)
51+
assert len(dependencies_old) == 3
52+
dependencies_old = [
53+
dependency for dependency in dependencies_old if
54+
dependency.depend_on_it in (some_exception, other_exception)]
55+
assert len(dependencies_old) == 2
56+
57+
dependencies_new = declarations.get_dependencies_from_decl(
58+
calldef_with_throw)
59+
assert len(dependencies_new) == 3
60+
dependencies_new = [
61+
dependency for dependency in dependencies_new if
62+
dependency.depend_on_it in (some_exception, other_exception)]
63+
assert len(dependencies_new) == 2

‎tests/test_type_as_exception_bug.py

+4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
def global_ns():
2121
COMPILATION_MODE = parser.COMPILATION_MODE.ALL_AT_ONCE
2222
config = autoconfig.cxx_parsers_cfg.config.clone()
23+
# This test does not work with c++17 and above
24+
# See https://developers.redhat.com/articles/2021/08/06/porting-your-code-c17-gcc-11#exception_specification_changes # noqa
25+
# This test is thus excpected to use -std=c++14 forever
26+
config.cflags = "-std=c++14"
2327
decls = parser.parse(TEST_FILES, config, COMPILATION_MODE)
2428
global_ns = declarations.get_global_namespace(decls)
2529
return global_ns

0 commit comments

Comments
 (0)
Please sign in to comment.